From a84e108a236c9c8e6990f7e01e156dcf949554fb Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Apr 2022 09:55:42 +0000 Subject: [PATCH 001/137] Auto-generated commit 2624c7dc4b3b0e9dc35233c11f2a977a3e1d0208 --- CONTRIBUTORS | 28 + LICENSE | 481 +++++++++ NOTICE | 1 + README.md | 331 ++++++ index.d.ts | 130 +++ index.mjs | 4 + index.mjs.map | 1 + package.json | 42 + stats.html | 2689 +++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 3707 insertions(+) create mode 100644 CONTRIBUTORS create mode 100644 LICENSE create mode 100644 NOTICE create mode 100644 README.md create mode 100644 index.d.ts create mode 100644 index.mjs create mode 100644 index.mjs.map create mode 100644 package.json create mode 100644 stats.html diff --git a/CONTRIBUTORS b/CONTRIBUTORS new file mode 100644 index 0000000..e5c896d --- /dev/null +++ b/CONTRIBUTORS @@ -0,0 +1,28 @@ +# This file is generated by tools/scripts/update_contributors. +# +# Contributors listed in alphabetical order. + +Ali Salesi +Athan Reines +Brendan Graetz +Bruno Fenzl +Christopher Dambamuromo +Dominik Moritz +Frank Kovacs +James +Jithin KS +Joey Reed +Joris Labie +Justin Dennison +Marcus +Matt Cochrane +Milan Raj +Momtchil Momtchev +Ognjen Jevremović +Philipp Burckhardt +Ricky Reusser +Ryan Seal +Seyyed Parsa Neshaei +Shraddheya Shendre +dorrin-sot <59933477+dorrin-sot@users.noreply.github.com> +rei2hu diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fcc9934 --- /dev/null +++ b/LICENSE @@ -0,0 +1,481 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + + +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by this +license (the "Software") to use, reproduce, display, distribute, execute, and +transmit the Software, and to prepare derivative works of the Software, and to +permit third-parties to whom the Software is furnished to do so, all subject to +the following: + +The copyright notices in the Software and this entire statement, including the +above license grant, this restriction and the following disclaimer, must be +included in all copies of the Software, in whole or in part, and all derivative +works of the Software, unless such copies or derivative works are solely in the +form of machine-executable object code generated by a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES +OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + + + +DEPENDENCIES + +The library links against the following external libraries, which have their own +licenses: + +* OpenBLAS + +Copyright (c) 2011-2014, The OpenBLAS Project +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + 3. Neither the name of the OpenBLAS project nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +* Electron + +Copyright (c) 2013-2017 GitHub Inc. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +* Boost + +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + + +* Cephes + +Copyright (c) 1984-2000 Stephen L. Moshier + +Some software in this archive may be from the book _Methods and Programs for +Mathematical Functions_ (Prentice-Hall or Simon & Schuster International, 1989) +or from the Cephes Mathematical Library, a commercial product. In either event, +it is copyrighted by the author. What you see here may be used freely but it +comes with no support or guarantee. + +Stephen L. Moshier +moshier@na-net.ornl.gov + + + +ATTRIBUTION + +The library contains implementations from the following external libraries, +which have their own licenses: + +* FreeBSD + +Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + + +* FDLIBM + +Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + + +* Go + +Copyright (c) 2009 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +* SLATEC Common Mathematical Library + +Public domain. + + +* ESLint + +Copyright JS Foundation and other contributors, https://js.foundation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + +* StatsFuns.jl + +Copyright (c) 2015: Dahua Lin. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +* SpecialFunctions.jl + +The MIT License (MIT) + +Copyright (c) 2017 Jeff Bezanson, Stefan Karpinski, Viral B. Shah, and others: + +https://github.com/JuliaMath/SpecialFunctions.jl/graphs/contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + +* MT19937 + +Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..10e6ba1 --- /dev/null +++ b/NOTICE @@ -0,0 +1 @@ +Copyright (c) 2016-2022 The Stdlib Authors. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8ec7ccd --- /dev/null +++ b/README.md @@ -0,0 +1,331 @@ + + +# sub2ind + +[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] + +> Convert subscripts to a linear index. + + + +
+ +
+ + + + + + + +
+ +## Usage + +```javascript +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +``` + +#### sub2ind( shape, strides, offset, ...subscripts, mode ) + +Converts subscripts to a linear index. + +```javascript +var shape = [ 2, 2 ]; +var strides = [ 2, 1 ]; +var offset = 0; +var mode = [ 'throw' ]; + +var idx = sub2ind( shape, strides, offset, 1, 0, mode ); +// returns 2 +``` + +The function supports the following `modes`: + +- `throw`: specifies that the function should throw an error when a subscript exceeds array dimensions. +- `wrap`: specifies that the function should wrap around subscripts exceeding array dimensions using modulo arithmetic. +- `clamp`: specifies that the function should set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. + +```javascript +var shape = [ 2, 2 ]; +var strides = [ 2, 1 ]; +var offset = 0; + +var mode = [ 'wrap' ]; +var idx = sub2ind( shape, strides, offset, -2, 0, mode ); +// returns 0 + +mode = [ 'clamp' ]; +idx = sub2ind( shape, strides, offset, 10, 10, mode ); +// returns 3 +``` + +
+ + + + + +
+ +## Notes + +- When provided fewer `modes` than dimensions, the function recycles `modes` using modulo arithmetic. + + ```javascript + var shape = [ 2, 2, 2 ]; + var strides = [ 4, 2, 1 ]; + var offset = 0; + var mode = [ 'wrap', 'clamp' ]; + + var idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); + // returns 3 + ``` + +- When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. + + ```text + Dims: 2x2 + Buffer: [ 1, 2, 3, 4 ] + + View = [ a00, a01, + a10, a11 ] + + Strides: 2,1 + Offset: 0 + + View = [ 1, 2, + 3, 4 ] + + Strides: 2,-1 + Offset: 1 + + View = [ 2, 1, + 4, 3 ] + + Strides: -2,1 + Offset: 2 + + View = [ 3, 4, + 1, 2 ] + + Strides: -2,-1 + Offset: 3 + + View = [ 4, 3, + 2, 1 ] + ``` + + ```javascript + var shape = [ 2, 2 ]; + var strides = [ -2, 1 ]; + var offset = 2; + var mode = [ 'throw' ]; + + // From the perspective of a view... + var idx = sub2ind( shape, strides, 0, 0, 0, mode ); + // returns 0 + + idx = sub2ind( shape, strides, 0, 0, 1, mode ); + // returns 1 + + idx = sub2ind( shape, strides, 0, 1, 0, mode ); + // returns 2 + + idx = sub2ind( shape, strides, 0, 1, 1, mode ); + // returns 3 + + // From the perspective of an underlying buffer... + idx = sub2ind( shape, strides, offset, 0, 0, mode ); + // returns 2 + + idx = sub2ind( shape, strides, offset, 0, 1, mode ); + // returns 3 + + idx = sub2ind( shape, strides, offset, 1, 0, mode ); + // returns 0 + + idx = sub2ind( shape, strides, offset, 1, 1, mode ); + // returns 1 + ``` + + In short, from the perspective of a view, view data is always ordered. + +
+ + + + + +
+ +## Examples + + + +```html + + + + + + +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + +
+ +* * * + +## Notice + +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. + +For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. + +#### Community + +[![Chat][chat-image]][chat-url] + +--- + +## License + +See [LICENSE][stdlib-license]. + + +## Copyright + +Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. + +
+ + + + + + + + diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..96feb4b --- /dev/null +++ b/index.d.ts @@ -0,0 +1,130 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 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: 2.0 + +/// + +import { ArrayLike } from '@stdlib/types/array'; + +/** +* Converts subscripts to a linear index. +* +* ## Notes +* +* - The function accepts the following "modes": +* +* - `throw`: throws an error when a subscript exceeds array dimensions. +* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. +* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. +* +* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. +* +* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. +* +* ```text +* Dims: 2x2 +* Buffer: [ 1, 2, 3, 4 ] +* +* View = [ a00, a01, +* a10, a11 ] +* +* Strides: 2,1 +* Offset: 0 +* +* View = [ 1, 2, +* 3, 4 ] +* +* Strides: 2,-1 +* Offset: 1 +* +* View = [ 2, 1, +* 4, 3 ] +* +* Strides: -2,1 +* Offset: 2 +* +* View = [ 3, 4, +* 1, 2 ] +* +* Strides: -2,-1 +* Offset: 3 +* +* View = [ 4, 3, +* 2, 1 ] +* ``` +* +* ```javascript +* var shape = [ 2, 2 ]; +* var strides = [ -2, 1 ]; +* var offset = 2; +* var mode = [ 'throw' ]; +* +* // From the perspective of a view... +* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); +* // returns 0 +* +* idx = sub2ind( shape, strides, 0, 0, 1, mode ); +* // returns 1 +* +* idx = sub2ind( shape, strides, 0, 1, 0, mode ); +* // returns 2 +* +* idx = sub2ind( shape, strides, 0, 1, 1, mode ); +* // returns 3 +* +* // From the perspective of an underlying buffer... +* idx = sub2ind( shape, strides, offset, 0, 0, mode ); +* // returns 2 +* +* idx = sub2ind( shape, strides, offset, 0, 1, mode ); +* // returns 3 +* +* idx = sub2ind( shape, strides, offset, 1, 0, mode ); +* // returns 0 +* +* idx = sub2ind( shape, strides, offset, 1, 1, mode ); +* // returns 1 +* ``` +* +* In short, from the perspective of a view, view data is always ordered. +* +* +* @param shape - array shape +* @param strides - stride array +* @param offset - location of the first indexed value **based** on the stride array +* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions +* @param mode - specifies how to handle subscripts which exceed array dimensions +* @throws must provide subscripts which do not exceed array dimensions +* @returns linear index +* +* @example +* var shape = [ 3, 3, 3 ]; +* var strides = [ 9, 3, 1 ]; +* var offset = 0; +* var mode = [ 'throw' ] +* +* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); +* // returns 17 +*/ +declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length + + +// EXPORTS // + +export = sub2ind; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..5b515a5 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@esm/index.mjs";var e=r;var t=function(){var r,t,n,s,i,a,l,o,d,m,u,f;for(r=arguments[1],t=arguments[2],n=(a=arguments[3+(i=(s=arguments[0]).length)]).length,o=t,f=0;f=d&&(u=d-1);else if("wrap"===l)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: %u. Value: `%d`.",f,u));(m=r[f])<0&&0===t?o-=u*m:o+=u*m}return o};export{t as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..e9c9f90 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: %u. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"],"names":["format","require$$0","lib","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError"],"mappings":";;iFAsBA,IAAIA,EAASC,EAyKb,ICnJAC,EDmFA,WACC,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYjB,EAAQ,6FAA8Fc,EAAGD,KAEhID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..e918693 --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "@stdlib/ndarray-base-sub2ind", + "version": "0.0.0", + "description": "Convert subscripts to a linear index.", + "license": "Apache-2.0", + "type": "module", + "main": "./index.mjs", + "homepage": "https://stdlib.io", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/ndarray-base-sub2ind.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "keywords": [ + "stdlib", + "stdtypes", + "types", + "base", + "ndarray", + "shape", + "strides", + "offset", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util", + "sub2ind", + "subscript", + "linear", + "index", + "idx", + "strided" + ], + "funding": { + "type": "patreon", + "url": "https://www.patreon.com/athan" + } +} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..b48f8d2 --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + From 6154dd1040d330a6b2b7bf5418e1655ac1653cfc Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 30 Jun 2022 21:27:14 +0000 Subject: [PATCH 002/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index bc49a2b..be5ab40 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-library-manifest": "^0.0.x" }, From c8cccdb45dd017b0517df593e236d9daba422a64 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Jul 2022 02:32:52 +0000 Subject: [PATCH 003/137] Remove files --- index.d.ts | 130 --- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 2824 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 5b515a5..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@esm/index.mjs";var e=r;var t=function(){var r,t,n,s,i,a,l,o,d,m,u,f;for(r=arguments[1],t=arguments[2],n=(a=arguments[3+(i=(s=arguments[0]).length)]).length,o=t,f=0;f=d&&(u=d-1);else if("wrap"===l)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: %u. Value: `%d`.",f,u));(m=r[f])<0&&0===t?o-=u*m:o+=u*m}return o};export{t as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index e9c9f90..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: %u. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"],"names":["format","require$$0","lib","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError"],"mappings":";;iFAsBA,IAAIA,EAASC,EAyKb,ICnJAC,EDmFA,WACC,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYjB,EAAQ,6FAA8Fc,EAAGD,KAEhID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index b48f8d2..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From d14d2de213c4ae85a9e31976e77d79f9a9a4434c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Jul 2022 02:33:34 +0000 Subject: [PATCH 004/137] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 33 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 681 ------ .github/workflows/publish.yml | 157 -- .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 43 +- benchmark/benchmark.js | 403 --- benchmark/c/Makefile | 146 -- benchmark/c/benchmark.c | 573 ----- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 -- benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 -- manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 2689 +++++++++++++++++++++ test/test.js | 403 --- 45 files changed, 2715 insertions(+), 5397 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 7212d81..0000000 --- a/.gitattributes +++ /dev/null @@ -1,33 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 2b859da..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-06-30T21:26:39.493Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 128c22e..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,681 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the repository: - push: - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs rm -rf - - git add -A - git commit -m "Remove files" - - git merge -s recursive -X theirs origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch or create new branch tag: - - name: 'Push changes to `deno` branch or create new branch tag' - run: | - SLUG=${{ github.repository }} - VERSION=$(echo ${{ github.ref }} | sed -E -n 's/refs\/tags\/?(v[0-9]+.[0-9]+.[0-9]+).*/\1/p') - if [ -z "$VERSION" ]; then - echo "Workflow job was not triggered by a new tag...." - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - else - echo "Workflow job was triggered by a new tag: $VERSION" - echo "Creating new bundle branch tag of the form $VERSION-deno" - git tag -a $VERSION-deno -m "$VERSION-deno" - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" $VERSION-deno - fi - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs rm -rf - - git add -A - git commit -m "Remove files" - - git merge -s recursive -X theirs origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 08ba1ca..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 25f81ac..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The function does not compile if provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The function does not compile if provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..af67c76 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..e1b8e41 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;0FA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index be5ab40..b7a05ed 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-library-manifest": "^0.0.x" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.x", - "@stdlib/bench": "^0.0.x", - "@stdlib/math-base-special-floor": "^0.0.x", - "@stdlib/ndarray-base-numel": "^0.0.x", - "@stdlib/ndarray-base-shape2strides": "^0.0.x", - "@stdlib/ndarray-base-strides2offset": "^0.0.x", - "@stdlib/random-base-discrete-uniform": "^0.0.x", - "@stdlib/random-base-randu": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "patreon", "url": "https://www.patreon.com/athan" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..b748fb6 --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From c51e86fb4ab6e753b3c61841514ec961d97ee7d5 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Jul 2022 19:23:33 +0000 Subject: [PATCH 005/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index bc49a2b..be5ab40 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-library-manifest": "^0.0.x" }, From c53e114ba93e35330df39195fd57e7b14d1b56f3 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 2 Jul 2022 07:23:51 +0000 Subject: [PATCH 006/137] Remove files --- index.d.ts | 130 --- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 2824 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index af67c76..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index e1b8e41..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;0FA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index b748fb6..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 222f01ec4e92b46338ee40ebd3c22b482e3c2462 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 2 Jul 2022 07:24:37 +0000 Subject: [PATCH 007/137] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 33 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 681 ------ .github/workflows/publish.yml | 157 -- .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 43 +- benchmark/benchmark.js | 403 --- benchmark/c/Makefile | 146 -- benchmark/c/benchmark.c | 573 ----- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 -- benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 -- manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 2689 +++++++++++++++++++++ test/test.js | 403 --- 45 files changed, 2715 insertions(+), 5397 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 7212d81..0000000 --- a/.gitattributes +++ /dev/null @@ -1,33 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 1286f26..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-07-01T00:46:37.612Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 128c22e..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,681 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the repository: - push: - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs rm -rf - - git add -A - git commit -m "Remove files" - - git merge -s recursive -X theirs origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch or create new branch tag: - - name: 'Push changes to `deno` branch or create new branch tag' - run: | - SLUG=${{ github.repository }} - VERSION=$(echo ${{ github.ref }} | sed -E -n 's/refs\/tags\/?(v[0-9]+.[0-9]+.[0-9]+).*/\1/p') - if [ -z "$VERSION" ]; then - echo "Workflow job was not triggered by a new tag...." - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - else - echo "Workflow job was triggered by a new tag: $VERSION" - echo "Creating new bundle branch tag of the form $VERSION-deno" - git tag -a $VERSION-deno -m "$VERSION-deno" - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" $VERSION-deno - fi - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs rm -rf - - git add -A - git commit -m "Remove files" - - git merge -s recursive -X theirs origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 08ba1ca..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 25f81ac..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The function does not compile if provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The function does not compile if provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..af67c76 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..e1b8e41 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;0FA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index be5ab40..b7a05ed 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-library-manifest": "^0.0.x" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.x", - "@stdlib/bench": "^0.0.x", - "@stdlib/math-base-special-floor": "^0.0.x", - "@stdlib/ndarray-base-numel": "^0.0.x", - "@stdlib/ndarray-base-shape2strides": "^0.0.x", - "@stdlib/ndarray-base-strides2offset": "^0.0.x", - "@stdlib/random-base-discrete-uniform": "^0.0.x", - "@stdlib/random-base-randu": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "patreon", "url": "https://www.patreon.com/athan" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..2166d12 --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 6099b7db8d9af678f472a143cface1c4989b82e2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 6 Jul 2022 18:34:09 +0000 Subject: [PATCH 008/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index bc49a2b..be5ab40 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-library-manifest": "^0.0.x" }, From 0829623cfb736f128968e6db59b3d209e6df7fbf Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 6 Jul 2022 18:51:01 +0000 Subject: [PATCH 009/137] Remove files --- index.d.ts | 130 --- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 2824 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index af67c76..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index e1b8e41..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;0FA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 2166d12..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 08c8b94d293aabb88124f0cfa3faa85b5c6fa1d2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 6 Jul 2022 18:51:49 +0000 Subject: [PATCH 010/137] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 33 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 687 ------ .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 43 +- benchmark/benchmark.js | 403 --- benchmark/c/Makefile | 146 -- benchmark/c/benchmark.c | 573 ----- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 -- benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 -- manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 2689 +++++++++++++++++++++ test/test.js | 403 --- 44 files changed, 2715 insertions(+), 5362 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 7212d81..0000000 --- a/.gitattributes +++ /dev/null @@ -1,33 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 6726965..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,687 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the repository: - push: - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch or create new branch tag: - - name: 'Push changes to `deno` branch or create new branch tag' - run: | - SLUG=${{ github.repository }} - VERSION=$(echo ${{ github.ref }} | sed -E -n 's/refs\/tags\/?(v[0-9]+.[0-9]+.[0-9]+).*/\1/p') - if [ -z "$VERSION" ]; then - echo "Workflow job was not triggered by a new tag...." - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - else - echo "Workflow job was triggered by a new tag: $VERSION" - echo "Creating new bundle branch tag of the form $VERSION-deno" - git tag -a $VERSION-deno -m "$VERSION-deno" - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" $VERSION-deno - fi - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 08ba1ca..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 25f81ac..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The function does not compile if provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The function does not compile if provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..af67c76 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..e1b8e41 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;0FA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index be5ab40..b7a05ed 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-library-manifest": "^0.0.x" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.x", - "@stdlib/bench": "^0.0.x", - "@stdlib/math-base-special-floor": "^0.0.x", - "@stdlib/ndarray-base-numel": "^0.0.x", - "@stdlib/ndarray-base-shape2strides": "^0.0.x", - "@stdlib/ndarray-base-strides2offset": "^0.0.x", - "@stdlib/random-base-discrete-uniform": "^0.0.x", - "@stdlib/random-base-randu": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "patreon", "url": "https://www.patreon.com/athan" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..bed11a0 --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From cad31693955aa1e67a0eccf8df62c680ec9bc439 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Aug 2022 03:50:47 +0000 Subject: [PATCH 011/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index bc49a2b..be5ab40 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-library-manifest": "^0.0.x" }, From 64da22f196d694153405748520634fb43bc73db4 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Aug 2022 17:50:14 +0000 Subject: [PATCH 012/137] Remove files --- index.d.ts | 130 --- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 2824 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index af67c76..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index e1b8e41..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;0FA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index bed11a0..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 2968e0b0c6ab71bf3acb21ebdf3a30a4e660072d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Aug 2022 17:51:15 +0000 Subject: [PATCH 013/137] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 33 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 760 ------ .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 43 +- benchmark/benchmark.js | 403 --- benchmark/c/Makefile | 146 -- benchmark/c/benchmark.c | 573 ----- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 -- benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 -- manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 2689 +++++++++++++++++++++ test/test.js | 403 --- 45 files changed, 2715 insertions(+), 5436 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 7212d81..0000000 --- a/.gitattributes +++ /dev/null @@ -1,33 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index d036bd1..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-08-01T00:49:54.282Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 5094681..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,760 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 08ba1ca..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 25f81ac..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The function does not compile if provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The function does not compile if provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..4154f06 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..d9ab4d6 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index be5ab40..b7a05ed 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-library-manifest": "^0.0.x" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.x", - "@stdlib/bench": "^0.0.x", - "@stdlib/math-base-special-floor": "^0.0.x", - "@stdlib/ndarray-base-numel": "^0.0.x", - "@stdlib/ndarray-base-shape2strides": "^0.0.x", - "@stdlib/ndarray-base-strides2offset": "^0.0.x", - "@stdlib/random-base-discrete-uniform": "^0.0.x", - "@stdlib/random-base-randu": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "patreon", "url": "https://www.patreon.com/athan" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8cf6ec1 --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From d009f22b6496481c8d2566bf88f1d4811a265d7e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Sep 2022 03:20:33 +0000 Subject: [PATCH 014/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index bc49a2b..be5ab40 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-library-manifest": "^0.0.x" }, From 86ad0f9879d4195994820b6994a6140b93c974bc Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Sep 2022 16:19:04 +0000 Subject: [PATCH 015/137] Remove files --- index.d.ts | 130 --- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 2824 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 4154f06..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index d9ab4d6..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 8cf6ec1..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From b757c73c19d7113e1e22161f0802ef4e0b1cd2ae Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Sep 2022 16:20:08 +0000 Subject: [PATCH 016/137] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 760 ------ .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 43 +- benchmark/benchmark.js | 403 --- benchmark/c/Makefile | 146 -- benchmark/c/benchmark.c | 573 ----- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 -- benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 -- manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 2689 +++++++++++++++++++++ test/test.js | 403 --- 45 files changed, 2715 insertions(+), 5452 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 0c598a2..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-09-01T00:45:44.201Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 5094681..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,760 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 08ba1ca..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 25f81ac..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The function does not compile if provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The function does not compile if provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The function does not compile if provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..4154f06 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..d9ab4d6 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index be5ab40..b7a05ed 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-library-manifest": "^0.0.x" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.x", - "@stdlib/bench": "^0.0.x", - "@stdlib/math-base-special-floor": "^0.0.x", - "@stdlib/ndarray-base-numel": "^0.0.x", - "@stdlib/ndarray-base-shape2strides": "^0.0.x", - "@stdlib/ndarray-base-strides2offset": "^0.0.x", - "@stdlib/random-base-discrete-uniform": "^0.0.x", - "@stdlib/random-base-randu": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "patreon", "url": "https://www.patreon.com/athan" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..f48872b --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From d85b56f827214d7b5dd9aa751f8eb66848af4727 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Oct 2022 01:12:36 +0000 Subject: [PATCH 017/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index bc49a2b..be5ab40 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-library-manifest": "^0.0.x" }, From fecb527d53f6d9c1b11147a3ed8da9dbf681ec28 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Oct 2022 17:39:43 +0000 Subject: [PATCH 018/137] Remove files --- index.d.ts | 130 --- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 2824 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 4154f06..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index d9ab4d6..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index f48872b..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From da8e8f7b0d1953d736ee3e5c38117571d0cf61a8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Oct 2022 17:40:40 +0000 Subject: [PATCH 019/137] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 760 ------ .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 43 +- benchmark/benchmark.js | 403 --- benchmark/c/Makefile | 146 -- benchmark/c/benchmark.c | 573 ----- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 -- benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 -- manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 2689 +++++++++++++++++++++ test/test.js | 403 --- 45 files changed, 2715 insertions(+), 5452 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index ef3eaa5..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-10-01T01:10:10.653Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 5094681..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,760 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 08ba1ca..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..4154f06 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..d9ab4d6 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index be5ab40..b7a05ed 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-library-manifest": "^0.0.x" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.x", - "@stdlib/bench": "^0.0.x", - "@stdlib/math-base-special-floor": "^0.0.x", - "@stdlib/ndarray-base-numel": "^0.0.x", - "@stdlib/ndarray-base-shape2strides": "^0.0.x", - "@stdlib/ndarray-base-strides2offset": "^0.0.x", - "@stdlib/random-base-discrete-uniform": "^0.0.x", - "@stdlib/random-base-randu": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "patreon", "url": "https://www.patreon.com/athan" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..67caf83 --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 0bf7b7ae5927d675ec31872bd001ffba2fe8b8a5 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Nov 2022 03:24:10 +0000 Subject: [PATCH 020/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index bc49a2b..be5ab40 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-library-manifest": "^0.0.x" }, From c075661fd21f808a339e07579774c73192144641 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Nov 2022 13:52:31 +0000 Subject: [PATCH 021/137] Remove files --- index.d.ts | 130 --- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 2824 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 4154f06..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index d9ab4d6..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,EAGb,OAAOF"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 67caf83..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 2277089aeabdd8340cce9151dc6db07ebb6762b0 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Nov 2022 13:53:20 +0000 Subject: [PATCH 022/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 760 ---- .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 - .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 4044 +++++++++++++++++++++ test/test.js | 403 -- 45 files changed, 4070 insertions(+), 5452 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 5c9cdd3..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-11-01T00:56:38.137Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 9113bfe..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,760 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 08ba1ca..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..4154f06 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..ed5e09a --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index be5ab40..b7a05ed 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-library-manifest": "^0.0.x" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.x", - "@stdlib/bench": "^0.0.x", - "@stdlib/math-base-special-floor": "^0.0.x", - "@stdlib/ndarray-base-numel": "^0.0.x", - "@stdlib/ndarray-base-shape2strides": "^0.0.x", - "@stdlib/ndarray-base-strides2offset": "^0.0.x", - "@stdlib/random-base-discrete-uniform": "^0.0.x", - "@stdlib/random-base-randu": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "patreon", "url": "https://www.patreon.com/athan" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..ffcb108 --- /dev/null +++ b/stats.html @@ -0,0 +1,4044 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 7ae65c0167ada8033a1fed020ae7ca478dd3520c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Dec 2022 03:22:47 +0000 Subject: [PATCH 023/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index a3b4ad5..980af6b 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-library-manifest": "^0.0.x" }, From 5c81f615bac2ac45a9a4c6f79a724c263b223e04 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Dec 2022 13:29:44 +0000 Subject: [PATCH 024/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4044 ------------------------------------------------- 4 files changed, 4179 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 4154f06..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index ed5e09a..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index ffcb108..0000000 --- a/stats.html +++ /dev/null @@ -1,4044 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 620ca4bdb938dfb6992190db79aa32f5fcc7fbbf Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Dec 2022 13:30:40 +0000 Subject: [PATCH 025/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 781 ---- .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 183 - .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 4044 +++++++++++++++++++++ test/test.js | 403 -- 45 files changed, 4070 insertions(+), 5478 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 50cdd82..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-12-01T00:58:30.929Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 37ddb4f..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,781 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 08ba1ca..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..4154f06 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..de363ca --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 980af6b..b7a05ed 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-library-manifest": "^0.0.x" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.x", - "@stdlib/bench": "^0.0.x", - "@stdlib/math-base-special-floor": "^0.0.x", - "@stdlib/ndarray-base-numel": "^0.0.x", - "@stdlib/ndarray-base-shape2strides": "^0.0.x", - "@stdlib/ndarray-base-strides2offset": "^0.0.x", - "@stdlib/random-base-discrete-uniform": "^0.0.x", - "@stdlib/random-base-randu": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "2.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "patreon", "url": "https://www.patreon.com/athan" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..b6cfaa1 --- /dev/null +++ b/stats.html @@ -0,0 +1,4044 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 6fc973a5497908856f5475d9dcbdab7f0118b571 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Jan 2023 02:38:00 +0000 Subject: [PATCH 026/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index 2cee0e3..8f29359 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-library-manifest": "^0.0.x" }, From d88afc6e5212e387f66fe2ff355464a47e603982 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Jan 2023 11:56:07 +0000 Subject: [PATCH 027/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4044 ------------------------------------------------- 4 files changed, 4179 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 4154f06..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index de363ca..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index b6cfaa1..0000000 --- a/stats.html +++ /dev/null @@ -1,4044 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From cc8e1e16f9f945a42bdfebdba7b65ce6bcca764a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Jan 2023 11:57:14 +0000 Subject: [PATCH 028/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 791 ---- .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 184 - .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 4044 +++++++++++++++++++++ test/test.js | 403 -- 45 files changed, 4070 insertions(+), 5489 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 2fab7ed..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-01-01T00:45:51.151Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4eea88..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,791 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 08ba1ca..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c3d8651 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..de363ca --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 8f29359..b7a05ed 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-library-manifest": "^0.0.x" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.x", - "@stdlib/bench": "^0.0.x", - "@stdlib/math-base-special-floor": "^0.0.x", - "@stdlib/ndarray-base-numel": "^0.0.x", - "@stdlib/ndarray-base-shape2strides": "^0.0.x", - "@stdlib/ndarray-base-strides2offset": "^0.0.x", - "@stdlib/random-base-discrete-uniform": "^0.0.x", - "@stdlib/random-base-randu": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "patreon", "url": "https://www.patreon.com/athan" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..48a393b --- /dev/null +++ b/stats.html @@ -0,0 +1,4044 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 9d7ed9b073d3184f4a18678338c76a5bfaf2fbd8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Feb 2023 03:28:42 +0000 Subject: [PATCH 029/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index 2cee0e3..8f29359 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-library-manifest": "^0.0.x" }, From 7a4179101716a7ed6a10b91c0f285fdf47911dbb Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Feb 2023 08:27:19 +0000 Subject: [PATCH 030/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4044 ------------------------------------------------- 4 files changed, 4179 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c3d8651..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index de363ca..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 48a393b..0000000 --- a/stats.html +++ /dev/null @@ -1,4044 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 15fb1af98748b545c17a251e9181b233745becec Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Feb 2023 08:28:04 +0000 Subject: [PATCH 031/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 791 --- .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 184 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/test.js | 403 -- 45 files changed, 6203 insertions(+), 5489 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index be0953c..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-02-01T00:55:07.284Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4eea88..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,791 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 08ba1ca..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c3d8651 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..de363ca --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 8f29359..b7a05ed 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-library-manifest": "^0.0.x" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.x", - "@stdlib/bench": "^0.0.x", - "@stdlib/math-base-special-floor": "^0.0.x", - "@stdlib/ndarray-base-numel": "^0.0.x", - "@stdlib/ndarray-base-shape2strides": "^0.0.x", - "@stdlib/ndarray-base-strides2offset": "^0.0.x", - "@stdlib/random-base-discrete-uniform": "^0.0.x", - "@stdlib/random-base-randu": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "patreon", "url": "https://www.patreon.com/athan" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..dfa92d0 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 54f733d3a9027c7988d1aab0cf72750fb47a2ffc Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Mar 2023 02:20:36 +0000 Subject: [PATCH 032/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index 15aec8b..cd0fb1f 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-library-manifest": "^0.0.8" }, From 0d20ccc279519a682d3e4ac3e0726ea91af5734f Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Mar 2023 05:39:31 +0000 Subject: [PATCH 033/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c3d8651..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index de363ca..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index dfa92d0..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 63391bc28216786f0c32ed64f9129dd6f907ab86 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Mar 2023 05:40:26 +0000 Subject: [PATCH 034/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 236 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/test.js | 403 -- 45 files changed, 6203 insertions(+), 5624 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 5808b18..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-03-01T01:33:19.627Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 3e8e2db..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c3d8651 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..de363ca --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index cd0fb1f..644ac68 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-library-manifest": "^0.0.8" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.8", - "@stdlib/bench": "^0.0.12", - "@stdlib/math-base-special-floor": "^0.0.8", - "@stdlib/ndarray-base-numel": "^0.0.8", - "@stdlib/ndarray-base-shape2strides": "^0.0.8", - "@stdlib/ndarray-base-strides2offset": "^0.0.8", - "@stdlib/random-base-discrete-uniform": "^0.0.6", - "@stdlib/random-base-randu": "^0.0.8", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..e96acb0 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From bb89a481be04555bb206963b49819b42788ece7b Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Apr 2023 02:22:22 +0000 Subject: [PATCH 035/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index 15aec8b..cd0fb1f 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-library-manifest": "^0.0.8" }, From a60c0586c56468862c98ffc426f23a9d72f401d9 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Apr 2023 05:27:05 +0000 Subject: [PATCH 036/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c3d8651..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index de363ca..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index e96acb0..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 01992775f0301d726563c4bd6a109fea57b0eab1 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Apr 2023 05:27:58 +0000 Subject: [PATCH 037/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/test.js | 403 -- 45 files changed, 6203 insertions(+), 5630 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 9d22d93..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-04-01T01:39:16.929Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 3e8e2db..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c3d8651 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..de363ca --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index cd0fb1f..644ac68 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-library-manifest": "^0.0.8" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.8", - "@stdlib/bench": "^0.0.12", - "@stdlib/math-base-special-floor": "^0.0.8", - "@stdlib/ndarray-base-numel": "^0.0.8", - "@stdlib/ndarray-base-shape2strides": "^0.0.8", - "@stdlib/ndarray-base-strides2offset": "^0.0.8", - "@stdlib/random-base-discrete-uniform": "^0.0.6", - "@stdlib/random-base-randu": "^0.0.8", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..2851688 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 74332c56a455ede0ff6f40a2fa4ec03e62aeb36e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 May 2023 02:17:58 +0000 Subject: [PATCH 038/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index 15aec8b..cd0fb1f 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-library-manifest": "^0.0.8" }, From 00a518d6fd5eb20ac14ed8bbaeb305de3997dab4 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 May 2023 05:16:35 +0000 Subject: [PATCH 039/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c3d8651..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index de363ca..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 2851688..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From c2cd887e7170d07ed403ec031bd0d0019fb8f5d8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 May 2023 05:17:23 +0000 Subject: [PATCH 040/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/test.js | 403 -- 45 files changed, 6203 insertions(+), 5630 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 32e2ca8..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-05-01T01:36:06.343Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 3e8e2db..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c3d8651 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..de363ca --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index cd0fb1f..644ac68 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-library-manifest": "^0.0.8" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.8", - "@stdlib/bench": "^0.0.12", - "@stdlib/math-base-special-floor": "^0.0.8", - "@stdlib/ndarray-base-numel": "^0.0.8", - "@stdlib/ndarray-base-shape2strides": "^0.0.8", - "@stdlib/ndarray-base-strides2offset": "^0.0.8", - "@stdlib/random-base-discrete-uniform": "^0.0.6", - "@stdlib/random-base-randu": "^0.0.8", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..c38bc1f --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From b5c9c5b47714285845d50be8745883d085438a45 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Jun 2023 02:19:15 +0000 Subject: [PATCH 041/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index 15aec8b..cd0fb1f 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-library-manifest": "^0.0.8" }, From a30149335c5f6b1cc3a698a624a610564795f9a8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Jun 2023 05:28:20 +0000 Subject: [PATCH 042/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c3d8651..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index de363ca..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index c38bc1f..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 5598f6827e01679516d8e278f4041f80774bcdbd Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Jun 2023 05:29:18 +0000 Subject: [PATCH 043/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/test.js | 403 -- 45 files changed, 6203 insertions(+), 5630 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index db39944..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-06-01T01:36:21.996Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 3e8e2db..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c3d8651 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..de363ca --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index cd0fb1f..644ac68 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-library-manifest": "^0.0.8" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.8", - "@stdlib/bench": "^0.0.12", - "@stdlib/math-base-special-floor": "^0.0.8", - "@stdlib/ndarray-base-numel": "^0.0.8", - "@stdlib/ndarray-base-shape2strides": "^0.0.8", - "@stdlib/ndarray-base-strides2offset": "^0.0.8", - "@stdlib/random-base-discrete-uniform": "^0.0.6", - "@stdlib/random-base-randu": "^0.0.8", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..b00952e --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From d4aaf5da298e9360fffed900fa518e9bf2dae330 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Jul 2023 02:19:13 +0000 Subject: [PATCH 044/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index 15aec8b..cd0fb1f 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-library-manifest": "^0.0.8" }, From ed52020dfdd0b5ccb1117ceeee8d0ec9cb38a4b8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Jul 2023 05:24:02 +0000 Subject: [PATCH 045/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c3d8651..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index de363ca..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index b00952e..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From dc9c1dfaa8613027f228de175770acff297a6d43 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Jul 2023 05:24:57 +0000 Subject: [PATCH 046/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/test.js | 403 -- 45 files changed, 6203 insertions(+), 5630 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 3eb1cc0..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-07-01T01:36:36.899Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 3e8e2db..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c3d8651 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..de363ca --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index cd0fb1f..644ac68 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-library-manifest": "^0.0.8" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.8", - "@stdlib/bench": "^0.0.12", - "@stdlib/math-base-special-floor": "^0.0.8", - "@stdlib/ndarray-base-numel": "^0.0.8", - "@stdlib/ndarray-base-shape2strides": "^0.0.8", - "@stdlib/ndarray-base-strides2offset": "^0.0.8", - "@stdlib/random-base-discrete-uniform": "^0.0.6", - "@stdlib/random-base-randu": "^0.0.8", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..3830442 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From cb58910ae1ec46548dfe43ff6791b8aed69a7fb8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Aug 2023 02:27:28 +0000 Subject: [PATCH 047/137] Transform error messages --- lib/main.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..7ca4c34 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // diff --git a/package.json b/package.json index 15aec8b..cd0fb1f 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-library-manifest": "^0.0.8" }, From 3e6a81cac6853186a5b598d5d47368336d66bf82 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Aug 2023 05:45:40 +0000 Subject: [PATCH 048/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c3d8651..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index de363ca..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 3830442..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From ce3df582013ff78c38e9f989c3d7677b4414076f Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Aug 2023 05:46:35 +0000 Subject: [PATCH 049/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/test.js | 403 -- 45 files changed, 6203 insertions(+), 5630 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 11ad8f3..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-08-01T01:39:23.712Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 334eb59..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v2 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -274,7 +267,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 44f45cc..96feb4b 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c3d8651 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..de363ca --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 7ca4c34..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index cd0fb1f..644ac68 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.0.8", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-library-manifest": "^0.0.8" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.8", - "@stdlib/bench": "^0.0.12", - "@stdlib/math-base-special-floor": "^0.0.8", - "@stdlib/ndarray-base-numel": "^0.0.8", - "@stdlib/ndarray-base-shape2strides": "^0.0.8", - "@stdlib/ndarray-base-strides2offset": "^0.0.8", - "@stdlib/random-base-discrete-uniform": "^0.0.6", - "@stdlib/random-base-randu": "^0.0.8", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..3928249 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 7cebf524cd4d84c50e339218eb432dfe0214671f Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 22 Sep 2023 23:11:16 +0000 Subject: [PATCH 050/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..ff24671 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -172,7 +172,7 @@ function sub2ind() { } } } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J,L4', i, j ) ); } s = strides[ i ]; diff --git a/package.json b/package.json index 2841000..1bc8721 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.1.0", - "@stdlib/string-format": "^0.1.0", + "@stdlib/error-tools-fmtprodmsg": "^0.1.0", "@stdlib/types": "^0.1.0", "@stdlib/utils-library-manifest": "^0.1.0" }, From 9da93ce5ff6e227479c92f20791762fec6eb0d5f Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 22 Sep 2023 23:47:50 +0000 Subject: [PATCH 051/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96feb4b..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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: 2.0 - -/// - -import { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c3d8651..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";function r(){var r,t,s,n,i,o,a,l,d,m,u,f;for(r=arguments[1],t=arguments[2],s=(o=arguments[3+(i=(n=arguments[0]).length)]).length,l=t,f=0;f=d&&(u=d-1);else if("wrap"===a)u<0?(u+=d)<0&&0!==(u%=d)&&(u+=d):u>=d&&(u-=d)>=d&&(u%=d);else if(u<0||u>=d)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(m=r[f])<0&&0===t?l-=u*m:l+=u*m}return l}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index de363ca..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAElID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 3928249..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 84ea4f31d019c81576758a3cded4c3569fa37696 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 22 Sep 2023 23:49:13 +0000 Subject: [PATCH 052/137] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 783 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/test.js | 403 -- 48 files changed, 6203 insertions(+), 5664 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 3131977..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 857de24..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 91f2b93..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,783 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v2 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -285,7 +278,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 1c8dd10..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,u,f,m,v,t,e,n,r,a;for(u=arguments[0],i=arguments[1],s=arguments[2],f=u.length,m=arguments[3+f],o=m.length,t=s,a=0;a=e&&(r=e-1);else if(v==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(r<0||r>=e)throw new RangeError(c('0jX5J',a,r));n=i[a],n<0&&s===0?t-=r*n:t+=r*n}return t}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 6114f4c..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAGIE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAEtID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,ICxJjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 78d344c..b4acca1 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..eb6539b --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.0-esm/index.mjs";function e(){var e,t,o,s,n,l,f,i,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(n=(s=arguments[0]).length)]).length,i=t,p=0;p=m&&(d=m-1);else if("wrap"===f)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if(d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?i-=d*a:i+=d*a}return i}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..7c1c484 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAE9CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ff24671..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( '0jX5J,L4', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 1bc8721..d90f1db 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.1.0", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.1.0", - "@stdlib/error-tools-fmtprodmsg": "^0.1.0", - "@stdlib/types": "^0.1.0", - "@stdlib/utils-library-manifest": "^0.1.0" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.0.8", - "@stdlib/bench": "^0.1.0", - "@stdlib/math-base-special-floor": "^0.0.8", - "@stdlib/ndarray-base-numel": "^0.0.8", - "@stdlib/ndarray-base-shape2strides": "^0.0.8", - "@stdlib/ndarray-base-strides2offset": "^0.0.8", - "@stdlib/random-base-discrete-uniform": "^0.0.6", - "@stdlib/random-base-randu": "^0.0.8", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..37b4dd9 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 59041393aa9d7ca84aaeac8222e19d0b4021354f Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 23 Sep 2023 00:09:07 +0000 Subject: [PATCH 053/137] Update README.md for ESM bundle v0.1.0 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 397e14c..7590b5c 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ limitations under the License. ## Usage ```javascript -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.1.0-esm/index.mjs'; ``` #### sub2ind( shape, strides, offset, ...subscripts, mode ) @@ -203,7 +203,7 @@ import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sh import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs'; import numel from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@esm/index.mjs'; import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs'; -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.1.0-esm/index.mjs'; var shape = [ 3, 3 ]; var strides = shape2strides( shape, 'row-major' ); From 2baeee8261773e8439119f5e4e73754e5c9bcc2f Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 23 Sep 2023 00:09:08 +0000 Subject: [PATCH 054/137] Auto-generated commit --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7590b5c..ffcb810 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ limitations under the License. ## Usage +```javascript +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +``` +The previous example will load the latest bundled code from the esm branch. Alternatively, you may load a specific version by loading the file from one of the [tagged bundles](https://github.com/stdlib-js/ndarray-base-sub2ind/tags). For example, + ```javascript import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.1.0-esm/index.mjs'; ``` @@ -203,7 +208,7 @@ import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sh import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs'; import numel from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@esm/index.mjs'; import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs'; -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.1.0-esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; var shape = [ 3, 3 ]; var strides = shape2strides( shape, 'row-major' ); From 70e061391679a7195d04d146747390f6ff8cfd05 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Oct 2023 02:46:52 +0000 Subject: [PATCH 055/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..ff24671 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -172,7 +172,7 @@ function sub2ind() { } } } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J,L4', i, j ) ); } s = strides[ i ]; diff --git a/package.json b/package.json index bac20bd..95ae8ba 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.1.0", - "@stdlib/string-format": "^0.1.0", + "@stdlib/error-tools-fmtprodmsg": "^0.1.0", "@stdlib/types": "^0.1.0", "@stdlib/utils-library-manifest": "^0.1.0" }, From 9eaeb5074672efd7c86c46cee36d0bfdb71bbe2a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Oct 2023 21:11:51 +0000 Subject: [PATCH 056/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b4acca1..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index eb6539b..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.0-esm/index.mjs";function e(){var e,t,o,s,n,l,f,i,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(n=(s=arguments[0]).length)]).length,i=t,p=0;p=m&&(d=m-1);else if("wrap"===f)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if(d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?i-=d*a:i+=d*a}return i}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 7c1c484..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAE9CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 37b4dd9..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 863c39f53f4814dbb98c3a9b5283ab73bc346317 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Oct 2023 21:15:05 +0000 Subject: [PATCH 057/137] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 247 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 403 -- test/test.js | 403 -- 50 files changed, 6203 insertions(+), 6126 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index fca0638..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-10-01T01:21:11.383Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b6257c1..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -285,7 +278,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 1c8dd10..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,u,f,m,v,t,e,n,r,a;for(u=arguments[0],i=arguments[1],s=arguments[2],f=u.length,m=arguments[3+f],o=m.length,t=s,a=0;a=e&&(r=e-1);else if(v==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(r<0||r>=e)throw new RangeError(c('0jX5J',a,r));n=i[a],n<0&&s===0?t-=r*n:t+=r*n}return t}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 6114f4c..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAGIE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAEtID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,ICxJjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 78d344c..b4acca1 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..eb6539b --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.0-esm/index.mjs";function e(){var e,t,o,s,n,l,f,i,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(n=(s=arguments[0]).length)]).length,i=t,p=0;p=m&&(d=m-1);else if("wrap"===f)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if(d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?i-=d*a:i+=d*a}return i}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..7c1c484 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAE9CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ff24671..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( '0jX5J,L4', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 95ae8ba..d90f1db 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.1.0", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.1.0", - "@stdlib/error-tools-fmtprodmsg": "^0.1.0", - "@stdlib/types": "^0.1.0", - "@stdlib/utils-library-manifest": "^0.1.0" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.1.0", - "@stdlib/bench": "^0.1.0", - "@stdlib/math-base-special-floor": "^0.1.0", - "@stdlib/ndarray-base-numel": "^0.1.0", - "@stdlib/ndarray-base-shape2strides": "^0.1.0", - "@stdlib/ndarray-base-strides2offset": "^0.1.0", - "@stdlib/random-base-discrete-uniform": "^0.1.0", - "@stdlib/random-base-randu": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..f7ea21d --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index 9c806b0..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 422ee50d68701d87b2ad7a4ccafcacc9930dbcdf Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 4 Oct 2023 02:27:03 +0000 Subject: [PATCH 058/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..ff24671 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -172,7 +172,7 @@ function sub2ind() { } } } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J,L4', i, j ) ); } s = strides[ i ]; diff --git a/package.json b/package.json index 1d9c88e..6aad049 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.1.0", "@stdlib/utils-library-manifest": "^0.1.1" }, From 1d30752d14350f615874d5f6debd718821491cc9 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 4 Oct 2023 11:22:32 +0000 Subject: [PATCH 059/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b4acca1..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index eb6539b..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.0-esm/index.mjs";function e(){var e,t,o,s,n,l,f,i,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(n=(s=arguments[0]).length)]).length,i=t,p=0;p=m&&(d=m-1);else if("wrap"===f)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if(d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?i-=d*a:i+=d*a}return i}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 7c1c484..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAE9CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index f7ea21d..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 3bab08c9b8bd578d6554a085d66534ebca0b1262 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 4 Oct 2023 11:25:44 +0000 Subject: [PATCH 060/137] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 247 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 403 -- test/test.js | 403 -- 49 files changed, 6203 insertions(+), 6125 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b6257c1..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -285,7 +278,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 1c8dd10..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,u,f,m,v,t,e,n,r,a;for(u=arguments[0],i=arguments[1],s=arguments[2],f=u.length,m=arguments[3+f],o=m.length,t=s,a=0;a=e&&(r=e-1);else if(v==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(r<0||r>=e)throw new RangeError(c('0jX5J',a,r));n=i[a],n<0&&s===0?t-=r*n:t+=r*n}return t}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 6114f4c..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAGIE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAEtID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,ICxJjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 78d344c..b4acca1 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..ea59348 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";function e(){var e,t,o,s,n,l,f,i,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(n=(s=arguments[0]).length)]).length,i=t,p=0;p=m&&(d=m-1);else if("wrap"===f)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if(d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?i-=d*a:i+=d*a}return i}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..7c1c484 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAE9CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ff24671..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( '0jX5J,L4', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 6aad049..c3377ad 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.1.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.1.0", - "@stdlib/utils-library-manifest": "^0.1.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.1.0", - "@stdlib/bench": "^0.1.0", - "@stdlib/math-base-special-floor": "^0.1.0", - "@stdlib/ndarray-base-numel": "^0.1.0", - "@stdlib/ndarray-base-shape2strides": "^0.1.0", - "@stdlib/ndarray-base-strides2offset": "^0.1.0", - "@stdlib/random-base-discrete-uniform": "^0.1.0", - "@stdlib/random-base-randu": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8a9e36e --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index 9c806b0..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 60097bfccefab7c02d4d4a55ac58f2ed67ec4d9c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 4 Oct 2023 15:44:07 +0000 Subject: [PATCH 061/137] Update README.md for ESM bundle v0.1.1 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d41d989..9b5bc92 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ limitations under the License. ## Usage ```javascript -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.1.1-esm/index.mjs'; ``` #### sub2ind( shape, strides, offset, ...subscripts, mode ) @@ -203,7 +203,7 @@ import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sh import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs'; import numel from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@esm/index.mjs'; import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs'; -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.1.1-esm/index.mjs'; var shape = [ 3, 3 ]; var strides = shape2strides( shape, 'row-major' ); From 6f8cbb6364b615018fcffc2dec2c977f068b839d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 4 Oct 2023 15:44:08 +0000 Subject: [PATCH 062/137] Auto-generated commit --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b5bc92..ce07e44 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ limitations under the License. ## Usage +```javascript +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +``` +The previous example will load the latest bundled code from the esm branch. Alternatively, you may load a specific version by loading the file from one of the [tagged bundles](https://github.com/stdlib-js/ndarray-base-sub2ind/tags). For example, + ```javascript import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.1.1-esm/index.mjs'; ``` @@ -203,7 +208,7 @@ import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sh import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs'; import numel from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@esm/index.mjs'; import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs'; -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.1.1-esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; var shape = [ 3, 3 ]; var strides = shape2strides( shape, 'row-major' ); From ca829f023586f1165e9de2a7de56863cfa3a8329 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 5 Oct 2023 04:55:21 +0000 Subject: [PATCH 063/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..ff24671 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -172,7 +172,7 @@ function sub2ind() { } } } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J,L4', i, j ) ); } s = strides[ i ]; diff --git a/package.json b/package.json index 8ee701d..837c74f 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.1.0", "@stdlib/utils-library-manifest": "^0.1.1" }, From 8852baa811eba89262ada67304c89c9f2dbf3926 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 5 Oct 2023 22:17:16 +0000 Subject: [PATCH 064/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b4acca1..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index ea59348..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";function e(){var e,t,o,s,n,l,f,i,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(n=(s=arguments[0]).length)]).length,i=t,p=0;p=m&&(d=m-1);else if("wrap"===f)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if(d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?i-=d*a:i+=d*a}return i}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 7c1c484..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAE9CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 8a9e36e..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From d1446e47b6aa6f452a2af675bc930f9dc7e5fc56 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 5 Oct 2023 22:20:16 +0000 Subject: [PATCH 065/137] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 247 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 403 -- test/test.js | 403 -- 49 files changed, 6203 insertions(+), 6125 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b6257c1..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -285,7 +278,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 1c8dd10..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,u,f,m,v,t,e,n,r,a;for(u=arguments[0],i=arguments[1],s=arguments[2],f=u.length,m=arguments[3+f],o=m.length,t=s,a=0;a=e&&(r=e-1);else if(v==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(r<0||r>=e)throw new RangeError(c('0jX5J',a,r));n=i[a],n<0&&s===0?t-=r*n:t+=r*n}return t}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 6114f4c..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAGIE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAEtID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,ICxJjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 78d344c..b4acca1 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..ea59348 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";function e(){var e,t,o,s,n,l,f,i,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(n=(s=arguments[0]).length)]).length,i=t,p=0;p=m&&(d=m-1);else if("wrap"===f)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if(d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?i-=d*a:i+=d*a}return i}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..7c1c484 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAE9CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ff24671..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( '0jX5J,L4', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 837c74f..c3377ad 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.1.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.1.0", - "@stdlib/utils-library-manifest": "^0.1.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.1.0", - "@stdlib/bench": "^0.1.0", - "@stdlib/math-base-special-floor": "^0.1.0", - "@stdlib/ndarray-base-numel": "^0.1.1", - "@stdlib/ndarray-base-shape2strides": "^0.1.1", - "@stdlib/ndarray-base-strides2offset": "^0.1.1", - "@stdlib/random-base-discrete-uniform": "^0.1.0", - "@stdlib/random-base-randu": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..0d19a97 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index 9c806b0..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From e467b35bab28ff899af01a7cfc60c76f101fc232 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Nov 2023 02:49:59 +0000 Subject: [PATCH 066/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 87a0d37..ff24671 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -172,7 +172,7 @@ function sub2ind() { } } } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J,L4', i, j ) ); } s = strides[ i ]; diff --git a/package.json b/package.json index 209689c..75a7a08 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.1.0", "@stdlib/utils-library-manifest": "^0.1.1" }, From 0995302bc2c19708b4069594d470e084698eae07 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Nov 2023 21:25:22 +0000 Subject: [PATCH 067/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b4acca1..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index ea59348..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";function e(){var e,t,o,s,n,l,f,i,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(n=(s=arguments[0]).length)]).length,i=t,p=0;p=m&&(d=m-1);else if("wrap"===f)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if(d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?i-=d*a:i+=d*a}return i}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 7c1c484..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAE9CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 0d19a97..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From db7059c62b91dea57b8527648a7127e6f5cadd7f Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Nov 2023 21:29:03 +0000 Subject: [PATCH 068/137] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 403 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 573 -- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 54 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 192 - manifest.json | 40 - package.json | 64 +- src/main.c | 105 - stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 403 -- 50 files changed, 6203 insertions(+), 5764 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index a1454c7..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-11-01T01:22:03.214Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b6257c1..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -285,7 +278,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index d8a4510..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 5e2e5de..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,573 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version() { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic() { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double() { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10() { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 1c8dd10..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,u,f,m,v,t,e,n,r,a;for(u=arguments[0],i=arguments[1],s=arguments[2],f=u.length,m=arguments[3+f],o=m.length,t=s,a=0;a=e&&(r=e-1);else if(v==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(r<0||r>=e)throw new RangeError(c('0jX5J',a,r));n=i[a],n<0&&s===0?t-=r*n:t+=r*n}return t}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 6114f4c..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAGIE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAEtID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,ICxJjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index dbf6671..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,54 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'wrap', the function wraps around subscripts exceeding array - dimensions using modulo arithmetic. If equal to 'clamp', the function - sets subscripts exceeding array dimensions to either `0` (minimum index) - or the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index 23724dd..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 78d344c..b4acca1 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..ea59348 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";function e(){var e,t,o,s,n,l,f,i,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(n=(s=arguments[0]).length)]).length,i=t,p=0;p=m&&(d=m-1);else if("wrap"===f)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if(d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?i-=d*a:i+=d*a}return i}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..7c1c484 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAE9CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ff24671..0000000 --- a/lib/main.js +++ /dev/null @@ -1,192 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode === 'throw' - throw new RangeError( format( '0jX5J,L4', i, j ) ); - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 75a7a08..c3377ad 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.1.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.1.0", - "@stdlib/utils-library-manifest": "^0.1.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.1.0", - "@stdlib/bench": "^0.1.0", - "@stdlib/math-base-special-floor": "^0.1.1", - "@stdlib/ndarray-base-numel": "^0.1.1", - "@stdlib/ndarray-base-shape2strides": "^0.1.1", - "@stdlib/ndarray-base-strides2offset": "^0.1.1", - "@stdlib/random-base-discrete-uniform": "^0.1.0", - "@stdlib/random-base-randu": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 55d3a67..0000000 --- a/src/main.c +++ /dev/null @@ -1,105 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, int64_t *sub, int64_t nmodes, int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else if ( j < 0 || j >= m ) { // mode == 'error' - return -1; - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..45fe48c --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 8ed71e8..0000000 --- a/test/test.js +++ /dev/null @@ -1,403 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 6c41b534290bd229881f75194248d1aae7cece8e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 25 Nov 2023 07:28:29 +0000 Subject: [PATCH 069/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..fe0bf59 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J,L4', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index f548428..4508e05 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.2.0", "@stdlib/utils-library-manifest": "^0.1.1" }, From e986773c37022025254226efe3209092481d2201 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 25 Nov 2023 07:37:55 +0000 Subject: [PATCH 070/137] Remove files --- index.d.ts | 130 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6312 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b4acca1..0000000 --- a/index.d.ts +++ /dev/null @@ -1,130 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - `throw`: throws an error when a subscript exceeds array dimensions. -* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; // tslint-disable-line max-line-length - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index ea59348..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";function e(){var e,t,o,s,n,l,f,i,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(n=(s=arguments[0]).length)]).length,i=t,p=0;p=m&&(d=m-1);else if("wrap"===f)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if(d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?i-=d*a:i+=d*a}return i}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 7c1c484..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - `throw`: throws an error when a subscript exceeds array dimensions.\n* - `wrap`: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - `clamp`: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( j < 0 || j >= m ) { // mode === 'throw'\n\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAGD,GAAKE,EAAI,GAAKA,GAAKF,EACzB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAE9CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 45fe48c..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 22aa41a098fb90870886f615c38c5faaf71c3801 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 25 Nov 2023 07:40:34 +0000 Subject: [PATCH 071/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 64 +- src/main.c | 110 - stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 49 files changed, 6203 insertions(+), 5997 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b6257c1..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -286,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 9affeac..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..52b5815 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..6b384cd --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAG/CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index fe0bf59..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J,L4', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 4508e05..c3377ad 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.1.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.2.0", - "@stdlib/utils-library-manifest": "^0.1.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.1.0", - "@stdlib/bench": "^0.2.1", - "@stdlib/math-base-special-floor": "^0.1.1", - "@stdlib/ndarray-base-numel": "^0.1.1", - "@stdlib/ndarray-base-shape2strides": "^0.1.1", - "@stdlib/ndarray-base-strides2offset": "^0.1.1", - "@stdlib/random-base-discrete-uniform": "^0.1.0", - "@stdlib/random-base-randu": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..f17bed6 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 698c7e5555017c6a0e6cd19a4e34edbd0a88649d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Dec 2023 02:43:40 +0000 Subject: [PATCH 072/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..fe0bf59 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J,L4', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index f548428..4508e05 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.2.0", "@stdlib/utils-library-manifest": "^0.1.1" }, From 9526390dbaf1be4730fb3fa1753d10965dd81396 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Dec 2023 14:27:27 +0000 Subject: [PATCH 073/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6313 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 52b5815..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 6b384cd..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAG/CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index f17bed6..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From c1983885257b58971a56417d74a629c9cbe77cf9 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Dec 2023 14:30:03 +0000 Subject: [PATCH 074/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 64 +- src/main.c | 110 - stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 50 files changed, 6203 insertions(+), 5998 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 4e32f0f..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-12-01T01:26:31.295Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b6257c1..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -286,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 9affeac..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..52b5815 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..6b384cd --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAG/CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index fe0bf59..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J,L4', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 4508e05..c3377ad 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.1.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.2.0", - "@stdlib/utils-library-manifest": "^0.1.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.1.0", - "@stdlib/bench": "^0.2.1", - "@stdlib/math-base-special-floor": "^0.1.1", - "@stdlib/ndarray-base-numel": "^0.1.1", - "@stdlib/ndarray-base-shape2strides": "^0.1.1", - "@stdlib/ndarray-base-strides2offset": "^0.1.1", - "@stdlib/random-base-discrete-uniform": "^0.1.0", - "@stdlib/random-base-randu": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..88f53d9 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 3e695d32ed9ebb8b3df41131b651fe9b632dc373 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Jan 2024 02:04:19 +0000 Subject: [PATCH 075/137] Transform error messages --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2c0c8c5..e84a375 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.2.0", "@stdlib/utils-library-manifest": "^0.1.1" }, From e9f6c1f7c50fbdc164b5b0466b788efd03b86d68 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Jan 2024 05:41:32 +0000 Subject: [PATCH 076/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6313 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 52b5815..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J,L4",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 6b384cd..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J,L4', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,WAAYJ,EAAGD,KAG/CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 88f53d9..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 0c06c364c2a66468b5903d30010957c8f323e387 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Jan 2024 05:41:57 +0000 Subject: [PATCH 077/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 228 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 43 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 64 +- src/main.c | 110 - stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 6203 insertions(+), 6004 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 9ae3955..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-01-01T01:19:27.699Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 30656c4..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c92f5c4..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index f0db682..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index e1e3539..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -286,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 61522e2..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..dc6b55b --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";function r(){var r,t,n,i,s,a,l,o,m,d,u,f;for(r=arguments[1],t=arguments[2],n=(a=arguments[3+(s=(i=arguments[0]).length)]).length,o=t,f=0;f=m&&(u=m-1);else if("wrap"===l)u<0?(u+=m)<0&&0!==(u%=m)&&(u+=m):u>=m&&(u-=m)>=m&&(u%=m);else if("normalize"===l&&u<0&&(u+=m),u<0||u>=m)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(d=r[f])<0&&0===t?o-=u*d:o+=u*d}return o}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..3ffb267 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;wFA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAGnID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2e347d0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/string-format' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index e84a375..c3377ad 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.1.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.2.0", - "@stdlib/utils-library-manifest": "^0.1.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.1.0", - "@stdlib/math-base-special-floor": "^0.1.1", - "@stdlib/ndarray-base-numel": "^0.1.1", - "@stdlib/ndarray-base-shape2strides": "^0.1.1", - "@stdlib/ndarray-base-strides2offset": "^0.1.1", - "@stdlib/random-base-discrete-uniform": "^0.1.0", - "@stdlib/random-base-randu": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.1.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..c6efce6 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 2442abb556d9d128e5ae73298d0c41e9033a97d0 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Feb 2024 02:06:33 +0000 Subject: [PATCH 078/137] Transform error messages --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2c0c8c5..e84a375 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.2.0", "@stdlib/utils-library-manifest": "^0.1.1" }, From 117406a70ee862ca79aa935ddd09cf256b0ae1e2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Feb 2024 04:41:38 +0000 Subject: [PATCH 079/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6313 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index dc6b55b..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";function r(){var r,t,n,i,s,a,l,o,m,d,u,f;for(r=arguments[1],t=arguments[2],n=(a=arguments[3+(s=(i=arguments[0]).length)]).length,o=t,f=0;f=m&&(u=m-1);else if("wrap"===l)u<0?(u+=m)<0&&0!==(u%=m)&&(u+=m):u>=m&&(u-=m)>=m&&(u%=m);else if("normalize"===l&&u<0&&(u+=m),u<0||u>=m)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(d=r[f])<0&&0===t?o-=u*d:o+=u*d}return o}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 3ffb267..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;wFA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAGnID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index c6efce6..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 4e0d19155a4c26e5c42f6c3f9e86e0304065153c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Feb 2024 04:41:56 +0000 Subject: [PATCH 080/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 228 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 64 +- src/main.c | 110 - stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 6203 insertions(+), 6009 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 59d1175..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-02-01T01:27:24.812Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 30656c4..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c92f5c4..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index f0db682..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index e1e3539..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..dc6b55b --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";function r(){var r,t,n,i,s,a,l,o,m,d,u,f;for(r=arguments[1],t=arguments[2],n=(a=arguments[3+(s=(i=arguments[0]).length)]).length,o=t,f=0;f=m&&(u=m-1);else if("wrap"===l)u<0?(u+=m)<0&&0!==(u%=m)&&(u+=m):u>=m&&(u-=m)>=m&&(u%=m);else if("normalize"===l&&u<0&&(u+=m),u<0||u>=m)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(d=r[f])<0&&0===t?o-=u*d:o+=u*d}return o}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..3ffb267 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;wFA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAGnID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2e347d0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/string-format' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index e84a375..c3377ad 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.1.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.2.0", - "@stdlib/utils-library-manifest": "^0.1.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.1.0", - "@stdlib/math-base-special-floor": "^0.1.1", - "@stdlib/ndarray-base-numel": "^0.1.1", - "@stdlib/ndarray-base-shape2strides": "^0.1.1", - "@stdlib/ndarray-base-strides2offset": "^0.1.1", - "@stdlib/random-base-discrete-uniform": "^0.1.0", - "@stdlib/random-base-randu": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.1.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..179e3cf --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 7458ca91b97105ba49d6febfcf2cd8bbd8248fe8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 14 Feb 2024 05:06:46 +0000 Subject: [PATCH 081/137] Transform error messages --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 47476a1..9c8c978 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.0", - "@stdlib/string-format": "^0.2.0", + "@stdlib/error-tools-fmtprodmsg": "^0.2.0", "@stdlib/types": "^0.3.1", "@stdlib/utils-library-manifest": "^0.2.0" }, From 1bbd480a225ec5627d0c688dbea2bbd95bff317d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 14 Feb 2024 07:30:00 +0000 Subject: [PATCH 082/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6313 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index dc6b55b..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";function r(){var r,t,n,i,s,a,l,o,m,d,u,f;for(r=arguments[1],t=arguments[2],n=(a=arguments[3+(s=(i=arguments[0]).length)]).length,o=t,f=0;f=m&&(u=m-1);else if("wrap"===l)u<0?(u+=m)<0&&0!==(u%=m)&&(u+=m):u>=m&&(u-=m)>=m&&(u%=m);else if("normalize"===l&&u<0&&(u+=m),u<0||u>=m)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(d=r[f])<0&&0===t?o-=u*d:o+=u*d}return o}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 3ffb267..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;wFA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAGnID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 179e3cf..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From f1473bf4b01c8f71417852ca8fc5b14f90d71161 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 14 Feb 2024 07:30:36 +0000 Subject: [PATCH 083/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 132 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 228 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 64 +- src/main.c | 110 - stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 50 files changed, 6203 insertions(+), 6012 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 2edd41a..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 9106b5d..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..dc6b55b --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";function r(){var r,t,n,i,s,a,l,o,m,d,u,f;for(r=arguments[1],t=arguments[2],n=(a=arguments[3+(s=(i=arguments[0]).length)]).length,o=t,f=0;f=m&&(u=m-1);else if("wrap"===l)u<0?(u+=m)<0&&0!==(u%=m)&&(u+=m):u>=m&&(u-=m)>=m&&(u%=m);else if("normalize"===l&&u<0&&(u+=m),u<0||u>=m)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(d=r[f])<0&&0===t?o-=u*d:o+=u*d}return o}export{r as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..3ffb267 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;wFA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAGnID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2e347d0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/string-format' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 9c8c978..84f24cf 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.0", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.0", - "@stdlib/error-tools-fmtprodmsg": "^0.2.0", - "@stdlib/types": "^0.3.1", - "@stdlib/utils-library-manifest": "^0.2.0" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.1.0", - "@stdlib/math-base-special-floor": "^0.1.1", - "@stdlib/ndarray-base-numel": "^0.1.1", - "@stdlib/ndarray-base-shape2strides": "^0.1.1", - "@stdlib/ndarray-base-strides2offset": "^0.1.1", - "@stdlib/random-base-discrete-uniform": "^0.1.0", - "@stdlib/random-base-randu": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.1.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..ced3592 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 6b3813c34a5b5cec3d4cca024480eed5d4d77078 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 14 Feb 2024 10:22:32 +0000 Subject: [PATCH 084/137] Update README.md for ESM bundle v0.2.0 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 42d922c..aa5f4a7 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ limitations under the License. ## Usage ```javascript -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.0-esm/index.mjs'; ``` #### sub2ind( shape, strides, offset, ...subscripts, mode ) @@ -204,7 +204,7 @@ import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sh import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs'; import numel from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@esm/index.mjs'; import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs'; -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.0-esm/index.mjs'; var shape = [ 3, 3 ]; var strides = shape2strides( shape, 'row-major' ); From 24a0c3fdf471554b3fcde5ae0a49d3134d2bee2f Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 14 Feb 2024 10:22:33 +0000 Subject: [PATCH 085/137] Auto-generated commit --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aa5f4a7..077b95e 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ limitations under the License. ## Usage +```javascript +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +``` +The previous example will load the latest bundled code from the esm branch. Alternatively, you may load a specific version by loading the file from one of the [tagged bundles](https://github.com/stdlib-js/ndarray-base-sub2ind/tags). For example, + ```javascript import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.0-esm/index.mjs'; ``` @@ -204,7 +209,7 @@ import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sh import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs'; import numel from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@esm/index.mjs'; import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs'; -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.0-esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; var shape = [ 3, 3 ]; var strides = shape2strides( shape, 'row-major' ); From bea53c38a08014ad30f92abc0d0bbe23a2017f91 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 22 Feb 2024 02:12:06 +0000 Subject: [PATCH 086/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index fa32049..b6feb4d 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-library-manifest": "^0.2.1" }, From 6837ff58da8dc57c58e0467ed6d93836f47ff58d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 22 Feb 2024 03:33:05 +0000 Subject: [PATCH 087/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6313 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index dc6b55b..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";function r(){var r,t,n,i,s,a,l,o,m,d,u,f;for(r=arguments[1],t=arguments[2],n=(a=arguments[3+(s=(i=arguments[0]).length)]).length,o=t,f=0;f=m&&(u=m-1);else if("wrap"===l)u<0?(u+=m)<0&&0!==(u%=m)&&(u+=m):u>=m&&(u-=m)>=m&&(u%=m);else if("normalize"===l&&u<0&&(u+=m),u<0||u>=m)throw new RangeError(e("invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.",f,u));(d=r[f])<0&&0===t?o-=u*d:o+=u*d}return o}export{r as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 3ffb267..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;wFA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,+FAAgGJ,EAAGD,KAGnID,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index ced3592..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 4ff721cd1f7ae4423b39ac7759b785bcd5ba021c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 22 Feb 2024 03:34:39 +0000 Subject: [PATCH 088/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 ---- .github/workflows/publish.yml | 249 -- .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 132 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 228 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 64 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 50 files changed, 4868 insertions(+), 6006 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 9106b5d..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..273f672 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index b6feb4d..cca15dc 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,40 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-library-manifest": "^0.2.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.0", - "@stdlib/math-base-special-floor": "^0.2.0", - "@stdlib/ndarray-base-numel": "^0.2.0", - "@stdlib/ndarray-base-shape2strides": "^0.2.0", - "@stdlib/ndarray-base-strides2offset": "^0.2.0", - "@stdlib/random-base-discrete-uniform": "^0.2.0", - "@stdlib/random-base-randu": "^0.2.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.0" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -94,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..a0d0991 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From fb7c4e1cd67bc356d421cbd9e7c9ab2c00268aa2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 22 Feb 2024 03:55:26 +0000 Subject: [PATCH 089/137] Update README.md for ESM bundle v0.2.1 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e30951c..92c87e4 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ limitations under the License. ## Usage ```javascript -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.1-esm/index.mjs'; ``` #### sub2ind( shape, strides, offset, ...subscripts, mode ) @@ -204,7 +204,7 @@ import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sh import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs'; import numel from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@esm/index.mjs'; import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs'; -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.1-esm/index.mjs'; var shape = [ 3, 3 ]; var strides = shape2strides( shape, 'row-major' ); From b3feb51aeb00294ee14dedd41bf3e72faa1bdff1 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 22 Feb 2024 03:55:26 +0000 Subject: [PATCH 090/137] Auto-generated commit --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 92c87e4..97e248c 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ limitations under the License. ## Usage +```javascript +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +``` +The previous example will load the latest bundled code from the esm branch. Alternatively, you may load a specific version by loading the file from one of the [tagged bundles](https://github.com/stdlib-js/ndarray-base-sub2ind/tags). For example, + ```javascript import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.1-esm/index.mjs'; ``` @@ -204,7 +209,7 @@ import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sh import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs'; import numel from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@esm/index.mjs'; import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs'; -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.1-esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; var shape = [ 3, 3 ]; var strides = shape2strides( shape, 'row-major' ); From 734f66e5e82f6563b0f9823000e018fff7d7c555 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Mar 2024 01:53:45 +0000 Subject: [PATCH 091/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index 4dcc52c..be68c3a 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-library-manifest": "^0.2.1", "@stdlib/error-tools-fmtprodmsg": "^0.2.1" From 8922dce2ff1824b8de90a907d8697e79ea000a45 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Mar 2024 03:38:20 +0000 Subject: [PATCH 092/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 273f672..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index a0d0991..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 21957515aebb1895b0e2e96e47f06801a8f900ab Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Mar 2024 03:38:34 +0000 Subject: [PATCH 093/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 ---- .github/workflows/publish.yml | 249 -- .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 132 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 229 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 4868 insertions(+), 6009 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 8e3ff96..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-03-01T01:27:25.253Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 9106b5d..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..273f672 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index be68c3a..cca15dc 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-library-manifest": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.1", - "@stdlib/math-base-special-floor": "^0.2.1", - "@stdlib/ndarray-base-numel": "^0.2.1", - "@stdlib/ndarray-base-shape2strides": "^0.2.1", - "@stdlib/ndarray-base-strides2offset": "^0.2.1", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..a0d0991 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 3da80dbb034f6fc71c647889e2cd4414554f3b2c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Apr 2024 01:43:31 +0000 Subject: [PATCH 094/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index c8caa61..235f2b5 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-library-manifest": "^0.2.1", "@stdlib/error-tools-fmtprodmsg": "^0.2.1" From 18809bb46acb26688aa8bc904bb9b6f1a2355ad1 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Apr 2024 03:50:34 +0000 Subject: [PATCH 095/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 273f672..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index a0d0991..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 2e1ed02fb6da9166331bf8aa75597d5222779790 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Apr 2024 03:50:47 +0000 Subject: [PATCH 096/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 ---- .github/workflows/publish.yml | 249 -- .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 132 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 4868 insertions(+), 6012 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 296cd5e..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-04-01T01:15:09.650Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index ec90164..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..273f672 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 235f2b5..cca15dc 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-library-manifest": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.1", - "@stdlib/math-base-special-floor": "^0.2.2", - "@stdlib/ndarray-base-numel": "^0.2.1", - "@stdlib/ndarray-base-shape2strides": "^0.2.1", - "@stdlib/ndarray-base-strides2offset": "^0.2.1", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..a0d0991 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From d70f934fb151efadbe66f56fcd6c545cc2bc4538 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 11 Apr 2024 21:49:25 +0000 Subject: [PATCH 097/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index c8caa61..235f2b5 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-library-manifest": "^0.2.1", "@stdlib/error-tools-fmtprodmsg": "^0.2.1" From 5d9ef7ef403c2e652ea4369f4224f7a1fc92c1c8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 11 Apr 2024 23:51:16 +0000 Subject: [PATCH 098/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 273f672..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index a0d0991..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 019d28982951be55459b90447d4628d07cfa9aa3 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 11 Apr 2024 23:51:30 +0000 Subject: [PATCH 099/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 ---- .github/workflows/publish.yml | 249 -- .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 134 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 50 files changed, 4868 insertions(+), 6013 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index ec90164..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..273f672 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 235f2b5..cca15dc 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-library-manifest": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.1", - "@stdlib/math-base-special-floor": "^0.2.2", - "@stdlib/ndarray-base-numel": "^0.2.1", - "@stdlib/ndarray-base-shape2strides": "^0.2.1", - "@stdlib/ndarray-base-strides2offset": "^0.2.1", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..a0d0991 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 21f9724db61bad5f90653bb5f6a02c7759d1b28e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 May 2024 01:44:00 +0000 Subject: [PATCH 100/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index c8caa61..235f2b5 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-library-manifest": "^0.2.1", "@stdlib/error-tools-fmtprodmsg": "^0.2.1" From 5b3084f0626021882a45c957f6742719d7caad82 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 May 2024 03:15:32 +0000 Subject: [PATCH 101/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 273f672..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index a0d0991..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 668f8ccd9e38b67645c4d0d6640f28b29e4a5dac Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 May 2024 03:16:02 +0000 Subject: [PATCH 102/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 248 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 188 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 4868 insertions(+), 6021 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 8725c40..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-05-01T01:15:55.483Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f92a6c5..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..273f672 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 235f2b5..cca15dc 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-library-manifest": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.1", - "@stdlib/math-base-special-floor": "^0.2.2", - "@stdlib/ndarray-base-numel": "^0.2.1", - "@stdlib/ndarray-base-shape2strides": "^0.2.1", - "@stdlib/ndarray-base-strides2offset": "^0.2.1", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..a0d0991 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From b96e2e4104eabacab906acbaa6859b624f57dd5b Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Jun 2024 01:54:53 +0000 Subject: [PATCH 103/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index c8caa61..235f2b5 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-library-manifest": "^0.2.1", "@stdlib/error-tools-fmtprodmsg": "^0.2.1" From 2441be8c902ac628dd8bf308b01ecc93121cd907 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Jun 2024 03:19:49 +0000 Subject: [PATCH 104/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 273f672..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index a0d0991..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 5bfe6095d8fbe6977eda00f9326bf98c18f2abf0 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Jun 2024 03:20:02 +0000 Subject: [PATCH 105/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 248 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 188 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 4868 insertions(+), 6021 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 20f2372..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-06-01T01:27:38.729Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f92a6c5..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..273f672 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 235f2b5..cca15dc 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-library-manifest": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.1", - "@stdlib/math-base-special-floor": "^0.2.2", - "@stdlib/ndarray-base-numel": "^0.2.1", - "@stdlib/ndarray-base-shape2strides": "^0.2.1", - "@stdlib/ndarray-base-strides2offset": "^0.2.1", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..a0d0991 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 3b22bd40e9664ecaec0c30fa8149cde81cd1c3e3 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Jul 2024 01:52:25 +0000 Subject: [PATCH 106/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index c8caa61..235f2b5 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-library-manifest": "^0.2.1", "@stdlib/error-tools-fmtprodmsg": "^0.2.1" From f2584d3c19918075f180feb7386dd21479831be2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Jul 2024 03:21:08 +0000 Subject: [PATCH 107/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 273f672..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index a0d0991..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 6a12c19ec5ced30fc2731154450d39543c509c36 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Jul 2024 03:21:22 +0000 Subject: [PATCH 108/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 188 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 38 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 4868 insertions(+), 6058 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 78cf94f..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-07-01T01:24:27.843Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f92a6c5..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..273f672 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 235f2b5..cca15dc 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.1", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-library-manifest": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.1", - "@stdlib/math-base-special-floor": "^0.2.2", - "@stdlib/ndarray-base-numel": "^0.2.1", - "@stdlib/ndarray-base-shape2strides": "^0.2.1", - "@stdlib/ndarray-base-strides2offset": "^0.2.1", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..a0d0991 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 59fbf516a782f063433ee41be838b370fb075673 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 27 Jul 2024 02:11:49 +0000 Subject: [PATCH 109/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index 21346d0..75051b3 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.3.2", "@stdlib/utils-library-manifest": "^0.2.1", "@stdlib/error-tools-fmtprodmsg": "^0.2.2" From 8373592a386ac1e8132d1ca022fe8197ab35bcd6 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 27 Jul 2024 02:30:59 +0000 Subject: [PATCH 110/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 273f672..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index a0d0991..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From e8f5da4ff07f8c079e42aba2a6ff0aecc4d81548 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 27 Jul 2024 02:31:16 +0000 Subject: [PATCH 111/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 188 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 218 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 665 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 50 files changed, 4868 insertions(+), 6237 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 733f997..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,665 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -/** -* Benchmark `sub2ind`. -*/ -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random double on the interval [0,1]. -* -* @return random double -*/ -double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..5ebecf0 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 75051b3..09c32e1 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.2", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-library-manifest": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.1", - "@stdlib/math-base-special-floor": "^0.2.2", - "@stdlib/ndarray-base-numel": "^0.2.1", - "@stdlib/ndarray-base-shape2strides": "^0.2.1", - "@stdlib/ndarray-base-strides2offset": "^0.2.1", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8f79d18 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From fe725f8b062106fe612aec19dc3bebc183cd9dd6 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 27 Jul 2024 02:46:06 +0000 Subject: [PATCH 112/137] Update README.md for ESM bundle v0.2.2 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cfa6bb7..6644012 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ limitations under the License. ## Usage ```javascript -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.2-esm/index.mjs'; ``` #### sub2ind( shape, strides, offset, ...subscripts, mode ) @@ -204,7 +204,7 @@ import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sh import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs'; import numel from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@esm/index.mjs'; import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs'; -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.2-esm/index.mjs'; var shape = [ 3, 3 ]; var strides = shape2strides( shape, 'row-major' ); From 48b2e26a41def0dbdf3aad3f39f307cd9789a619 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 27 Jul 2024 02:46:07 +0000 Subject: [PATCH 113/137] Auto-generated commit --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6644012..304daa4 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ limitations under the License. ## Usage +```javascript +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; +``` +The previous example will load the latest bundled code from the esm branch. Alternatively, you may load a specific version by loading the file from one of the [tagged bundles](https://github.com/stdlib-js/ndarray-base-sub2ind/tags). For example, + ```javascript import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.2-esm/index.mjs'; ``` @@ -204,7 +209,7 @@ import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sh import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs'; import numel from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@esm/index.mjs'; import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs'; -import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@v0.2.2-esm/index.mjs'; +import sub2ind from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-sub2ind@esm/index.mjs'; var shape = [ 3, 3 ]; var strides = shape2strides( shape, 'row-major' ); From ae1dedbeda72cfe2f41fd8dfb22d89ba3750716b Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Aug 2024 01:56:19 +0000 Subject: [PATCH 114/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index cb83e2a..78ba9db 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.3.2", "@stdlib/utils-library-manifest": "^0.2.2", "@stdlib/error-tools-fmtprodmsg": "^0.2.2" From e78fb83ba482924d6506385494e7d1b3897b3cb1 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Aug 2024 03:49:52 +0000 Subject: [PATCH 115/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 5ebecf0..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 8f79d18..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 9a0ebab61fe7f8845843049e8ce9e2b788e1f853 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Aug 2024 03:50:05 +0000 Subject: [PATCH 116/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 168 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 662 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 4868 insertions(+), 6187 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index a3156e4..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-08-01T01:28:47.425Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 80c6fc3..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,662 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -static void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -static void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -static void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -static double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random number on the interval [0,1). -* -* @return random number -*/ -static double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..5ebecf0 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 78ba9db..09c32e1 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.2", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-library-manifest": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.2", - "@stdlib/math-base-special-floor": "^0.2.3", - "@stdlib/ndarray-base-numel": "^0.2.2", - "@stdlib/ndarray-base-shape2strides": "^0.2.2", - "@stdlib/ndarray-base-strides2offset": "^0.2.2", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8f79d18 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From e98d143dec11d099fda9ca32adabf2a9459a2b84 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 3 Aug 2024 16:36:34 +0000 Subject: [PATCH 117/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index cb83e2a..78ba9db 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.3.2", "@stdlib/utils-library-manifest": "^0.2.2", "@stdlib/error-tools-fmtprodmsg": "^0.2.2" From aa0994a2c694e46eb46374b77b1e86e1ce1b58b0 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 3 Aug 2024 17:40:36 +0000 Subject: [PATCH 118/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 5ebecf0..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 8f79d18..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 8cca3490e0356d01cb3db0e85e7282cf02738b94 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 3 Aug 2024 17:40:49 +0000 Subject: [PATCH 119/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 252 -- CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 662 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 4868 insertions(+), 6271 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index d4fd424..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-08-03T16:15:09.374Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 80c6fc3..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,662 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -static void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -static void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -static void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -static double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random number on the interval [0,1). -* -* @return random number -*/ -static double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..5ebecf0 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 78ba9db..09c32e1 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.2", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-library-manifest": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.2", - "@stdlib/math-base-special-floor": "^0.2.3", - "@stdlib/ndarray-base-numel": "^0.2.2", - "@stdlib/ndarray-base-shape2strides": "^0.2.2", - "@stdlib/ndarray-base-strides2offset": "^0.2.2", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8f79d18 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From fc8d700935bc446dc9d813d8148166d96e173ebb Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Sep 2024 02:02:16 +0000 Subject: [PATCH 120/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index d280c6c..f2d9ccf 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.1", "@stdlib/utils-library-manifest": "^0.2.2", "@stdlib/error-tools-fmtprodmsg": "^0.2.2" From 20b17bba4f20edbc6694ac9774ccc0e24b7047e6 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Sep 2024 03:42:58 +0000 Subject: [PATCH 121/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 5ebecf0..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 8f79d18..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 10ef5148b41a9c3946735adb6ec68fbe3f593448 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Sep 2024 03:43:14 +0000 Subject: [PATCH 122/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 251 -- CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 662 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 4868 insertions(+), 6270 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index c5916b6..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-09-01T01:35:42.865Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 80c6fc3..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,662 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -static void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -static void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -static void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -static double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random number on the interval [0,1). -* -* @return random number -*/ -static double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..5ebecf0 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index f2d9ccf..09c32e1 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.2", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.1", - "@stdlib/utils-library-manifest": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.2", - "@stdlib/math-base-special-floor": "^0.2.3", - "@stdlib/ndarray-base-numel": "^0.2.2", - "@stdlib/ndarray-base-shape2strides": "^0.2.2", - "@stdlib/ndarray-base-strides2offset": "^0.2.2", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8f79d18 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 62af5d77dbc85e65597e25ab6e8a1a9679fed70e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Oct 2024 02:07:29 +0000 Subject: [PATCH 123/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index d280c6c..f2d9ccf 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.1", "@stdlib/utils-library-manifest": "^0.2.2", "@stdlib/error-tools-fmtprodmsg": "^0.2.2" From de7521c46017004545b20f2f0143757d7081fda4 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Oct 2024 03:38:41 +0000 Subject: [PATCH 124/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 5ebecf0..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 8f79d18..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 281afc37b49aab9e4acdab4a57ed1b1f819eccdd Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Oct 2024 03:38:58 +0000 Subject: [PATCH 125/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 251 -- CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 662 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 4868 insertions(+), 6270 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index ee15d93..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-10-01T01:42:09.526Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 80c6fc3..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,662 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -static void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -static void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -static void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -static double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random number on the interval [0,1). -* -* @return random number -*/ -static double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..5ebecf0 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index f2d9ccf..09c32e1 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.2", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.1", - "@stdlib/utils-library-manifest": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.2", - "@stdlib/math-base-special-floor": "^0.2.3", - "@stdlib/ndarray-base-numel": "^0.2.2", - "@stdlib/ndarray-base-shape2strides": "^0.2.2", - "@stdlib/ndarray-base-strides2offset": "^0.2.2", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8f79d18 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 5ebb07691d3b98090a20fb0134e825a376bdece8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Nov 2024 02:04:17 +0000 Subject: [PATCH 126/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index d280c6c..f2d9ccf 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.1", "@stdlib/utils-library-manifest": "^0.2.2", "@stdlib/error-tools-fmtprodmsg": "^0.2.2" From 8cb335185d7daaa90dd12fb692555ed3d8611d4b Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Nov 2024 03:32:59 +0000 Subject: [PATCH 127/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 5ebecf0..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 8f79d18..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From ab7e2dd9959d68cc13fcb31e94a673c97c4c7b58 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Nov 2024 03:33:16 +0000 Subject: [PATCH 128/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 251 -- CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 662 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 51 files changed, 4868 insertions(+), 6270 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 8a7344c..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-11-01T01:39:18.553Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 80c6fc3..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,662 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -static void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -static void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -static void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -static double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random number on the interval [0,1). -* -* @return random number -*/ -static double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..5ebecf0 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index f2d9ccf..09c32e1 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.2", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.1", - "@stdlib/utils-library-manifest": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.2", - "@stdlib/math-base-special-floor": "^0.2.3", - "@stdlib/ndarray-base-numel": "^0.2.2", - "@stdlib/ndarray-base-shape2strides": "^0.2.2", - "@stdlib/ndarray-base-strides2offset": "^0.2.2", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8f79d18 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 358a3069d5f2454039541e63ff26af760de0c44c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Dec 2024 02:10:48 +0000 Subject: [PATCH 129/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index 6b2705c..7d8add9 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.3", "@stdlib/utils-library-manifest": "^0.2.2", "@stdlib/error-tools-fmtprodmsg": "^0.2.2" From f08b4cbe723b0eb61a0375a5039f59d4928b1ac2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Dec 2024 03:38:35 +0000 Subject: [PATCH 130/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 5ebecf0..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 8f79d18..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 72a97b6b72fc44b61acbca37cbe2fe5eb0f82388 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Dec 2024 03:38:52 +0000 Subject: [PATCH 131/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 --- .github/workflows/publish.yml | 252 - .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .github/workflows/test_published_package.yml | 105 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 251 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 662 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 ++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 52 files changed, 4868 insertions(+), 6375 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .github/workflows/test_published_package.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 95367f6..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-12-01T01:45:50.498Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 80c6fc3..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,662 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -static void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -static void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -static void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -static double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random number on the interval [0,1). -* -* @return random number -*/ -static double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..5ebecf0 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 7d8add9..09c32e1 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.2", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.3", - "@stdlib/utils-library-manifest": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.2", - "@stdlib/math-base-special-floor": "^0.2.3", - "@stdlib/ndarray-base-numel": "^0.2.2", - "@stdlib/ndarray-base-shape2strides": "^0.2.2", - "@stdlib/ndarray-base-strides2offset": "^0.2.2", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8f79d18 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 1fc220c6ab74c1202b082c2526d4f162e1e4379d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 30 Dec 2024 03:09:14 +0000 Subject: [PATCH 132/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index 6b2705c..7d8add9 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.3", "@stdlib/utils-library-manifest": "^0.2.2", "@stdlib/error-tools-fmtprodmsg": "^0.2.2" From 462541035c50975f7ed363b0c2e483d094efad43 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 30 Dec 2024 04:22:40 +0000 Subject: [PATCH 133/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 5ebecf0..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 8f79d18..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From bc6530cd3eda6937dc1c4cf93f68babb8a28e8de Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 30 Dec 2024 04:22:51 +0000 Subject: [PATCH 134/137] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 --- .github/workflows/publish.yml | 252 - .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .github/workflows/test_published_package.yml | 105 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 251 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 662 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 ++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 52 files changed, 4868 insertions(+), 6375 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .github/workflows/test_published_package.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0779e8a..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = true # Note: this disables using two spaces to force a hard line break, which is permitted in Markdown. As we don't typically follow that practice (TMK), we should be safe to automatically trim. - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 56e3fb2..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-12-30T02:44:35.448Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 80c6fc3..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,662 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -static void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -static void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -static void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -static double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random number on the interval [0,1). -* -* @return random number -*/ -static double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..5ebecf0 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 7d8add9..09c32e1 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.2", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.3", - "@stdlib/utils-library-manifest": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.2", - "@stdlib/math-base-special-floor": "^0.2.3", - "@stdlib/ndarray-base-numel": "^0.2.2", - "@stdlib/ndarray-base-shape2strides": "^0.2.2", - "@stdlib/ndarray-base-strides2offset": "^0.2.2", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8f79d18 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); From 97dab619c2f62326eed959689bb87e7d2c86a2dd Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 3 Feb 2025 00:55:26 +0000 Subject: [PATCH 135/137] Transform error messages --- lib/main.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index 2e347d0..298dd1b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -20,7 +20,7 @@ // MODULES // -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -176,7 +176,7 @@ function sub2ind() { j += m; } if ( j < 0 || j >= m ) { - throw new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) ); + throw new RangeError( format( '0jX5J', i, j ) ); } } s = strides[ i ]; diff --git a/package.json b/package.json index 6b2705c..7d8add9 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.3", "@stdlib/utils-library-manifest": "^0.2.2", "@stdlib/error-tools-fmtprodmsg": "^0.2.2" From 3a631762f7cd5d80834db514563c22c35a5f87d8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 3 Feb 2025 01:19:16 +0000 Subject: [PATCH 136/137] Remove files --- index.d.ts | 131 -- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 4978 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 379a75a..0000000 --- a/index.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 { ArrayLike } from '@stdlib/types/array'; - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* -* @param shape - array shape -* @param strides - stride array -* @param offset - location of the first indexed value **based** on the stride array -* @param args - subscripts followed by a `mode` specifying how to handle subscripts which exceed array dimensions -* @param mode - specifies how to handle subscripts which exceed array dimensions -* @throws must provide subscripts which do not exceed array dimensions -* @returns linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ -declare function sub2ind( shape: ArrayLike, strides: ArrayLike, offset: number, ...args: Array> ): number; - - -// EXPORTS // - -export = sub2ind; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 5ebecf0..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 83bd0dc..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 8f79d18..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 71ec4ecc6fd8ef3ee3faf252603db798a83a4fd0 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 3 Feb 2025 01:19:34 +0000 Subject: [PATCH 137/137] Auto-generated commit --- .editorconfig | 180 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 --- .github/workflows/publish.yml | 252 - .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .github/workflows/test_published_package.yml | 105 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 251 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 45 +- SECURITY.md | 5 - benchmark/benchmark.js | 478 -- benchmark/c/Makefile | 146 - benchmark/c/benchmark.c | 662 --- benchmark/julia/REQUIRE | 2 - benchmark/julia/benchmark.jl | 144 - benchmark/python/numpy/benchmark.py | 97 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 5 - dist/index.js.map | 7 - docs/repl.txt | 56 - docs/types/test.ts | 97 - examples/index.js | 65 - include/stdlib/ndarray/base/sub2ind.h | 41 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/index.js | 45 - lib/main.js | 197 - manifest.json | 40 - package.json | 65 +- src/main.c | 110 - stats.html | 4842 ++++++++++++++++++ test/dist/test.js | 33 - test/test.js | 463 -- 52 files changed, 4868 insertions(+), 6374 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .github/workflows/test_published_package.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.js delete mode 100644 benchmark/c/Makefile delete mode 100644 benchmark/c/benchmark.c delete mode 100644 benchmark/julia/REQUIRE delete mode 100644 benchmark/julia/benchmark.jl delete mode 100644 benchmark/python/numpy/benchmark.py delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js delete mode 100644 include/stdlib/ndarray/base/sub2ind.h rename docs/types/index.d.ts => index.d.ts (97%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/index.js delete mode 100644 lib/main.js delete mode 100644 manifest.json delete mode 100644 src/main.c create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index dab5d2a..0000000 --- a/.editorconfig +++ /dev/null @@ -1,180 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = true # Note: this disables using two spaces to force a hard line break, which is permitted in Markdown. As we don't typically follow that practice (TMK), we should be safe to automatically trim. - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 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. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 6e14367..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2025-02-03T00:48:09.425Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index b09e382..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 7a4c103..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 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. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b9a6211..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 12 * * 0' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 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. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -288,7 +279,7 @@ for ( i = 0; i < 20; i++ ) { ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index 0145231..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,478 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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-harness' ); -var randu = require( '@stdlib/random-base-randu' ); -var floor = require( '@stdlib/math-base-special-floor' ); -var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive; -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var pkg = require( './../package.json' ).name; -var sub2ind = require( './../lib' ); - - -// MAIN // - -bench( pkg+':mode=[throw]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[throw],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'throw' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*10.0 ); - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*100.0 ) - 50.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[normalize],offset=0', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - for ( i = 0; i < shape.length; i++ ) { - strides[ i ] *= -1; - } - offset = 0; - mode = [ 'normalize' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 10.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[wrap,clamp,clamp]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'wrap', 'clamp', 'clamp' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':mode=[clamp,wrap,wrap]', function benchmark( b ) { - var strides; - var offset; - var shape; - var mode; - var out; - var s0; - var s1; - var s2; - var i; - - shape = [ 10, 10, 10 ]; - strides = shape2strides( shape, 'row-major' ); - offset = strides2offset( shape, strides ); - mode = [ 'clamp', 'wrap', 'wrap' ]; - - s0 = discreteUniform( 0, shape[ 0 ]-1 ); - s1 = discreteUniform( 0, shape[ 1 ]-1 ); - s2 = discreteUniform( 0, shape[ 2 ]-1 ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - s2 = floor( randu()*20.0 ) - 5.0; - out = sub2ind( shape, strides, offset, s0, s1, s2, mode ); - if ( out !== out ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( !isInteger( out ) ) { - b.fail( 'should return an integer' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/benchmark/c/Makefile b/benchmark/c/Makefile deleted file mode 100644 index c12ea19..0000000 --- a/benchmark/c/Makefile +++ /dev/null @@ -1,146 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2018 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. -#/ - -# VARIABLES # - -ifndef VERBOSE - QUIET := @ -else - QUIET := -endif - -# Determine the OS ([1][1], [2][2]). -# -# [1]: https://en.wikipedia.org/wiki/Uname#Examples -# [2]: http://stackoverflow.com/a/27776822/2225624 -OS ?= $(shell uname) -ifneq (, $(findstring MINGW,$(OS))) - OS := WINNT -else -ifneq (, $(findstring MSYS,$(OS))) - OS := WINNT -else -ifneq (, $(findstring CYGWIN,$(OS))) - OS := WINNT -else -ifneq (, $(findstring Windows_NT,$(OS))) - OS := WINNT -endif -endif -endif -endif - -# Define the program used for compiling C source files: -ifdef C_COMPILER - CC := $(C_COMPILER) -else - CC := gcc -endif - -# Define the command-line options when compiling C files: -CFLAGS ?= \ - -std=c99 \ - -O3 \ - -Wall \ - -pedantic - -# Determine whether to generate position independent code ([1][1], [2][2]). -# -# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options -# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option -ifeq ($(OS), WINNT) - fPIC ?= -else - fPIC ?= -fPIC -endif - -# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): -INCLUDE ?= - -# List of source files: -SOURCE_FILES ?= - -# List of libraries (e.g., `-lopenblas -lpthread`): -LIBRARIES ?= - -# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): -LIBPATH ?= - -# List of C targets: -c_targets := benchmark.out - - -# RULES # - -#/ -# Compiles C source files. -# -# @param {string} SOURCE_FILES - list of C source files -# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) -# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} [C_COMPILER] - C compiler -# @param {string} [CFLAGS] - C compiler flags -# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code -# -# @example -# make -# -# @example -# make all -#/ -all: $(c_targets) - -.PHONY: all - -#/ -# Compiles C source files. -# -# @private -# @param {string} SOURCE_FILES - list of C source files -# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) -# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) -# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) -# @param {string} CC - C compiler -# @param {string} CFLAGS - C compiler flags -# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code -#/ -$(c_targets): %.out: %.c - $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) - -#/ -# Runs compiled benchmarks. -# -# @example -# make run -#/ -run: $(c_targets) - $(QUIET) ./$< - -.PHONY: run - -#/ -# Removes generated files. -# -# @example -# make clean -#/ -clean: - $(QUIET) -rm -f *.o *.out - -.PHONY: clean diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c deleted file mode 100644 index 80c6fc3..0000000 --- a/benchmark/c/benchmark.c +++ /dev/null @@ -1,662 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include -#include -#include -#include -#include -#include - -#define NAME "sub2ind" -#define ITERATIONS 1000000 -#define REPEATS 3 - -/** -* Prints the TAP version. -*/ -static void print_version( void ) { - printf( "TAP version 13\n" ); -} - -/** -* Prints the TAP summary. -* -* @param total total number of tests -* @param passing total number of passing tests -*/ -static void print_summary( int total, int passing ) { - printf( "#\n" ); - printf( "1..%d\n", total ); // TAP plan - printf( "# total %d\n", total ); - printf( "# pass %d\n", passing ); - printf( "#\n" ); - printf( "# ok\n" ); -} - -/** -* Prints benchmarks results. -* -* @param elapsed elapsed time in seconds -*/ -static void print_results( double elapsed ) { - double rate = (double)ITERATIONS / elapsed; - printf( " ---\n" ); - printf( " iterations: %d\n", ITERATIONS ); - printf( " elapsed: %0.9f\n", elapsed ); - printf( " rate: %0.9f\n", rate ); - printf( " ...\n" ); -} - -/** -* Returns a clock time. -* -* @return clock time -*/ -static double tic( void ) { - struct timeval now; - gettimeofday( &now, NULL ); - return (double)now.tv_sec + (double)now.tv_usec/1.0e6; -} - -/** -* Generates a random number on the interval [0,1). -* -* @return random number -*/ -static double rand_double( void ) { - int r = rand(); - return (double)r / ( (double)RAND_MAX + 1.0 ); -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark1( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark2( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_ERROR - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( rand_double()*10.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark3( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark4( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark5( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark6( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0) - 5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark7( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark8( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_CLAMP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark9( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark10( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_CLAMP, - STDLIB_NDARRAY_INDEX_WRAP, - STDLIB_NDARRAY_INDEX_WRAP - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*20.0)-5.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark11( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { 100, 10, 1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0)-1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Runs a benchmark. -* -* @return elapsed time in seconds -*/ -static double benchmark12( void ) { - double elapsed; - int64_t idx; - double t; - int i; - - int64_t ndims = 3; - int64_t shape[] = { 10, 10, 10 }; - int64_t strides[] = { -100, -10, -1 }; - int64_t offset = 0; - - int64_t sub[] = { 5, 5, 5 }; - - int64_t nmodes = 1; - int8_t modes[] = { - STDLIB_NDARRAY_INDEX_NORMALIZE - }; - - t = tic(); - for ( i = 0; i < ITERATIONS; i++ ) { - sub[ 2 ] = (int64_t)( (rand_double()*2000.0) - 1000.0 ); - idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); - if ( idx < -1 ) { - printf( "unexpected result\n" ); - break; - } - } - elapsed = tic() - t; - if ( idx < -1 ) { - printf( "unexpected result\n" ); - } - return elapsed; -} - -/** -* Main execution sequence. -*/ -int main( void ) { - double elapsed; - int count; - int i; - - count = 0; - - // Use the current time to seed the random number generator: - srand( time( NULL ) ); - - print_version(); - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error]\n", NAME ); - elapsed = benchmark1(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[error],offset=0\n", NAME ); - elapsed = benchmark2(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap]\n", NAME ); - elapsed = benchmark3(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap],offset=0\n", NAME ); - elapsed = benchmark4(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp]\n", NAME ); - elapsed = benchmark5(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp],offset=0\n", NAME ); - elapsed = benchmark6(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp]\n", NAME ); - elapsed = benchmark7(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[wrap,clamp,clamp]\n", NAME ); - elapsed = benchmark8(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap]\n", NAME ); - elapsed = benchmark9(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[clamp,wrap,wrap]\n", NAME ); - elapsed = benchmark10(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize]\n", NAME ); - elapsed = benchmark11(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - for ( i = 0; i < REPEATS; i++ ) { - count += 1; - printf( "# c::native::%s:mode=[normalize],offset=0\n", NAME ); - elapsed = benchmark12(); - print_results( elapsed ); - printf( "ok %d benchmark finished\n", count ); - } - print_summary( count, count ); -} diff --git a/benchmark/julia/REQUIRE b/benchmark/julia/REQUIRE deleted file mode 100644 index 98645e1..0000000 --- a/benchmark/julia/REQUIRE +++ /dev/null @@ -1,2 +0,0 @@ -julia 1.5 -BenchmarkTools 0.5.0 diff --git a/benchmark/julia/benchmark.jl b/benchmark/julia/benchmark.jl deleted file mode 100644 index a385ef1..0000000 --- a/benchmark/julia/benchmark.jl +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env julia -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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 BenchmarkTools -using Printf - -# Benchmark variables: -name = "sub2ind"; -repeats = 3; - -""" - print_version() - -Prints the TAP version. - -# Examples - -``` julia -julia> print_version() -``` -""" -function print_version() - @printf( "TAP version 13\n" ); -end - -""" - print_summary( total, passing ) - -Print the benchmark summary. - -# Arguments - -* `total`: total number of tests -* `passing`: number of passing tests - -# Examples - -``` julia -julia> print_summary( 3, 3 ) -``` -""" -function print_summary( total, passing ) - @printf( "#\n" ); - @printf( "1..%d\n", total ); # TAP plan - @printf( "# total %d\n", total ); - @printf( "# pass %d\n", passing ); - @printf( "#\n" ); - @printf( "# ok\n" ); -end - -""" - print_results( iterations, elapsed ) - -Print benchmark results. - -# Arguments - -* `iterations`: number of iterations -* `elapsed`: elapsed time (in seconds) - -# Examples - -``` julia -julia> print_results( 1000000, 0.131009101868 ) -``` -""" -function print_results( iterations, elapsed ) - rate = iterations / elapsed - - @printf( " ---\n" ); - @printf( " iterations: %d\n", iterations ); - @printf( " elapsed: %0.9f\n", elapsed ); - @printf( " rate: %0.9f\n", rate ); - @printf( " ...\n" ); -end - -""" - benchmark() - -Run a benchmark. - -# Notes - -* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. -* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. -* The elapsed time is in seconds. - -# Examples - -``` julia -julia> out = benchmark(); -``` -""" -function benchmark() - t = BenchmarkTools.@benchmark Base._sub2ind( (10,10,10), 9, 8, Int32( floor( rand()*10.0 ) ) ) samples=1e6 - - # Compute the total "elapsed" time and convert from nanoseconds to seconds: - s = sum( t.times ) / 1.0e9; - - # Determine the number of "iterations": - iter = length( t.times ); - - # Return the results: - [ iter, s ]; -end - -""" - main() - -Run benchmarks. - -# Examples - -``` julia -julia> main(); -``` -""" -function main() - print_version(); - for i in 1:repeats - @printf( "# julia::%s\n", name ); - results = benchmark(); - print_results( results[ 1 ], results[ 2 ] ); - @printf( "ok %d benchmark finished\n", i ); - end - print_summary( repeats, repeats ); -end - -main(); diff --git a/benchmark/python/numpy/benchmark.py b/benchmark/python/numpy/benchmark.py deleted file mode 100644 index f08b8fa..0000000 --- a/benchmark/python/numpy/benchmark.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -# @license Apache-2.0 -# -# Copyright (c) 2018 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. - -"""Benchmark numpy.ravel_multi_index.""" - -from __future__ import print_function -import timeit - -NAME = "sub2ind" -REPEATS = 3 -ITERATIONS = 1000000 - - -def print_version(): - """Print the TAP version.""" - print("TAP version 13") - - -def print_summary(total, passing): - """Print the benchmark summary. - - # Arguments - - * `total`: total number of tests - * `passing`: number of passing tests - - """ - print("#") - print("1.." + str(total)) # TAP plan - print("# total " + str(total)) - print("# pass " + str(passing)) - print("#") - print("# ok") - - -def print_results(elapsed): - """Print benchmark results. - - # Arguments - - * `elapsed`: elapsed time (in seconds) - - # Examples - - ``` python - python> print_results(0.131009101868) - ``` - """ - rate = ITERATIONS / elapsed - - print(" ---") - print(" iterations: " + str(ITERATIONS)) - print(" elapsed: " + str(elapsed)) - print(" rate: " + str(rate)) - print(" ...") - - -def benchmark(): - """Run the benchmark and print benchmark results.""" - setup = "import numpy as np; from random import random;" - stmt = "y = np.ravel_multi_index((9, 8, int(random()*10.0)), (10, 10, 10))" - - t = timeit.Timer(stmt, setup=setup) - - print_version() - - for i in range(REPEATS): - print("# python::numpy::" + NAME) - elapsed = t.timeit(number=ITERATIONS) - print_results(elapsed) - print("ok " + str(i+1) + " benchmark finished") - - print_summary(REPEATS, REPEATS) - - -def main(): - """Run the benchmark.""" - benchmark() - - -if __name__ == "__main__": - main() diff --git a/branches.md b/branches.md deleted file mode 100644 index 26da902..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind" -%% click B href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/main" -%% click C href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production" -%% click D href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm" -%% click E href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno" -%% click F href "https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/sub2ind -[production-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/production -[deno-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/deno -[deno-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/umd -[umd-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/ndarray-base-sub2ind/tree/esm -[esm-readme]: https://github.com/stdlib-js/ndarray-base-sub2ind/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index f14e96a..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import sub2ind from '../docs/types/index'; -export = sub2ind; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 98858aa..0000000 --- a/dist/index.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict";var g=function(i,s){return function(){return s||i((s={exports:{}}).exports,s),s.exports}};var l=g(function(x,d){ -var c=require('@stdlib/error-tools-fmtprodmsg/dist');function p(){var i,s,o,f,m,v,t,n,e,u,r,a;for(f=arguments[0],i=arguments[1],s=arguments[2],m=f.length,v=arguments[3+m],o=v.length,n=s,a=0;a=e&&(r=e-1);else if(t==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(t==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(c('0jX5J',a,r));u=i[a],u<0&&s===0?n-=r*u:n+=r*u}return n}d.exports=p -});var b=l();module.exports=b; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 474ac32..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( 'invalid argument. Subscripts must not exceed array dimensions. Subscript: `%u`. Value: `%d`.', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Convert subscripts to a linear index.\n*\n* @module @stdlib/ndarray-base-sub2ind\n*\n* @example\n* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' );\n*\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ];\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns 17\n*/\n\n// MODULES //\n\nvar sub2ind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = sub2ind;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAyG9C,SAASC,GAAU,CAClB,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IAPAR,EAAQ,UAAW,CAAE,EACrBH,EAAU,UAAW,CAAE,EACvBC,EAAS,UAAW,CAAE,EACtBG,EAAQD,EAAM,OACdE,EAAQ,UAAW,EAAED,CAAM,EAC3BF,EAASG,EAAM,OACfE,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,CAAE,EACbD,EAAI,UAAWC,EAAE,CAAE,EACnBL,EAAOD,EAAOM,EAAET,CAAO,EAClBI,IAAS,QACRI,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,WAEEF,IAAS,OACfI,EAAI,GACRA,GAAKF,EACAE,EAAI,IACRA,GAAKF,EACAE,IAAM,IACVA,GAAKF,KAGIE,GAAKF,IAChBE,GAAKF,EACAE,GAAKF,IACTE,GAAKF,YAIFF,IAAS,aAAeI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAI,WAAYV,EAAQ,+FAAgGa,EAAGD,CAAE,CAAE,EAGvID,EAAIT,EAASW,CAAE,EAGVF,EAAI,GAAKR,IAAW,EACxBM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEb,CACA,OAAOF,CACR,CAKAV,EAAO,QAAUE,IC7JjB,IAAIa,EAAU,IAKd,OAAO,QAAUA", - "names": ["require_main", "__commonJSMin", "exports", "module", "format", "sub2ind", "strides", "offset", "nmodes", "shape", "ndims", "modes", "mode", "idx", "m", "s", "j", "i", "sub2ind"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index f254c8c..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,56 +0,0 @@ - -{{alias}}( shape, strides, offset, ...subscript, mode ) - Converts subscripts to a linear index. - - When provided a stride array containing negative strides, if an `offset` is - greater than `0`, the function treats subscripts as mapping to a linear - index in an underlying data buffer for the array, thus returning a linear - index from the perspective of that buffer. If an `offset` is equal to `0`, - the function treats subscripts as mapping to a linear index in an array - view, thus returning a linear index from the perspective of that view. In - short, from the perspective of a view, view data is always ordered. - - When provided fewer modes than dimensions, the function recycles modes using - modulo arithmetic. - - Parameters - ---------- - shape: ArrayLike - Array shape. - - strides: ArrayLike - Stride array. - - offset: integer - Location of the first indexed value based on the stride array. - - subscript: ...integer - Subscripts. - - mode: Array - Specifies how to handle subscripts which exceed array dimensions for - each dimension. If the mode for a dimension is equal to 'throw', the - function throws an error when a subscript exceeds array dimensions. If - equal to 'normalize', the function normalizes negative subscripts and - throws an error when a subscript exceeds array dimensions. If equal to - 'wrap', the function wraps around subscripts exceeding array dimensions - using modulo arithmetic. If equal to 'clamp', the function sets - subscripts exceeding array dimensions to either `0` (minimum index) or - the maximum index along a particular dimension. - - Returns - ------- - idx: integer - Linear index. - - Examples - -------- - > var d = [ 3, 3, 3 ]; - > var s = [ 9, 3, 1 ]; - > var o = 0; - > var idx = {{alias}}( d, s, o, 1, 2, 2, [ 'throw' ] ) - 17 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index e86ea32..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,97 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 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 sub2ind = require( './index' ); - - -// TESTS // - -// The function returns a number... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - const mode = [ 'throw' ]; - sub2ind( shape, strides, offset, 1, 2, 2, mode ); // $ExpectType number -} - -// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... -{ - const strides = [ 10, 1 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( true, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( false, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( null, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( undefined, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( '5', strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( [ '1', '2' ], strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( {}, strides, offset, 0, 0, mode ); // $ExpectError - sub2ind( ( x: number ): number => x, strides, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... -{ - const shape = [ 3, 3, 3 ]; - const offset = 10; - const mode = [ 'throw' ]; - sub2ind( shape, true, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, false, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, null, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, undefined, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, '5', offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, [ '1', '2' ], offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, {}, offset, 0, 0, mode ); // $ExpectError - sub2ind( shape, ( x: number ): number => x, offset, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not a number... -{ - const shape = [ 10, 10 ]; - const strides = [ 10, 1 ]; - const mode = [ 'throw' ]; - sub2ind( shape, strides, true, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, false, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, null, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, undefined, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, '5', 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, [ '1', '2' ], 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, {}, 0, 0, mode ); // $ExpectError - sub2ind( shape, strides, ( x: number ): number => x, 0, 0, mode ); // $ExpectError -} - -// The compiler throws an error if the function is provided further arguments which are not numbers followed by an array of strings... -{ - const shape = [ 3, 3, 3 ]; - const strides = [ 9, 3, 1 ]; - const offset = 0; - sub2ind( shape, strides, offset, true ); // $ExpectError - sub2ind( shape, strides, offset, false ); // $ExpectError - sub2ind( shape, strides, offset, null ); // $ExpectError - sub2ind( shape, strides, offset, undefined ); // $ExpectError - sub2ind( shape, strides, offset, 'abc' ); // $ExpectError - sub2ind( shape, strides, offset, {} ); // $ExpectError - sub2ind( shape, strides, offset, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided insufficient arguments... -{ - sub2ind(); // $ExpectError - sub2ind( [ 10, 10 ] ); // $ExpectError - sub2ind( [ 10, 10 ], [ 10, 1 ] ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 19313bb..0000000 --- a/examples/index.js +++ /dev/null @@ -1,65 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); -var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); -var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); -var numel = require( '@stdlib/ndarray-base-numel' ); -var randu = require( '@stdlib/random-base-randu' ); -var sub2ind = require( './../lib' ); - -var shape = [ 3, 3 ]; -var strides = shape2strides( shape, 'row-major' ); -var mode = 'throw'; -var len = numel( shape ); - -var arr = []; -var i; -for ( i = 0; i < len; i++ ) { - arr.push( i ); -} - -var offset; -var idx; -var row; -var j; -var n; -var m; -for ( i = 0; i < 20; i++ ) { - j = discreteUniform( 0, shape.length-1 ); - strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; - offset = strides2offset( shape, strides ); - - console.log( '' ); - console.log( 'Dimensions: %s.', shape.join( 'x' ) ); - console.log( 'Strides: %s.', strides.join( ',' ) ); - console.log( 'View:' ); - for ( n = 0; n < shape[ 0 ]; n++ ) { - row = ' '; - for ( m = 0; m < shape[ 1 ]; m++ ) { - idx = sub2ind( shape, strides, offset, n, m, mode ); - row += arr[ idx ]; - if ( m < shape[ 1 ]-1 ) { - row += ', '; - } - } - console.log( row ); - } -} diff --git a/include/stdlib/ndarray/base/sub2ind.h b/include/stdlib/ndarray/base/sub2ind.h deleted file mode 100644 index cbb2899..0000000 --- a/include/stdlib/ndarray/base/sub2ind.h +++ /dev/null @@ -1,41 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#ifndef STDLIB_NDARRAY_BASE_SUB2IND_H -#define STDLIB_NDARRAY_BASE_SUB2IND_H - -#include "stdlib/ndarray/index_modes.h" -#include - -/* -* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -/** -* Converts subscripts to a linear index. -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ); - -#ifdef __cplusplus -} -#endif - -#endif // !STDLIB_NDARRAY_BASE_SUB2IND_H diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 97% rename from docs/types/index.d.ts rename to index.d.ts index 1dd968a..379a75a 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { ArrayLike } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c428267 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2025 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";function e(){var e,t,o,n,s,l,i,f,m,a,d,p;for(e=arguments[1],t=arguments[2],o=(l=arguments[3+(s=(n=arguments[0]).length)]).length,f=t,p=0;p=m&&(d=m-1);else if("wrap"===i)d<0?(d+=m)<0&&0!==(d%=m)&&(d+=m):d>=m&&(d-=m)>=m&&(d%=m);else if("normalize"===i&&d<0&&(d+=m),d<0||d>=m)throw new RangeError(r("0jX5J",p,d));(a=e[p])<0&&0===t?f-=d*a:f+=d*a}return f}export{e as default}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..83bd0dc --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Converts subscripts to a linear index.\n*\n* ## Notes\n*\n* - The function accepts the following \"modes\":\n*\n* - **throw**: throw an error when a subscript exceeds array dimensions.\n* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions.\n* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic.\n* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension.\n*\n* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic.\n*\n* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view.\n*\n* ```text\n* Dims: 2x2\n* Buffer: [ 1, 2, 3, 4 ]\n*\n* View = [ a00, a01,\n* a10, a11 ]\n*\n* Strides: 2,1\n* Offset: 0\n*\n* View = [ 1, 2,\n* 3, 4 ]\n*\n* Strides: 2,-1\n* Offset: 1\n*\n* View = [ 2, 1,\n* 4, 3 ]\n*\n* Strides: -2,1\n* Offset: 2\n*\n* View = [ 3, 4,\n* 1, 2 ]\n*\n* Strides: -2,-1\n* Offset: 3\n*\n* View = [ 4, 3,\n* 2, 1 ]\n* ```\n*\n* ```javascript\n* var shape = [ 2, 2 ];\n* var strides = [ -2, 1 ];\n* var offset = 2;\n* var mode = [ 'throw' ];\n*\n* // From the perspective of a view...\n* var idx = sub2ind( shape, strides, 0, 0, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, 0, 0, 1, mode );\n* // returns 1\n*\n* idx = sub2ind( shape, strides, 0, 1, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, 0, 1, 1, mode );\n* // returns 3\n*\n* // From the perspective of an underlying buffer...\n* idx = sub2ind( shape, strides, offset, 0, 0, mode );\n* // returns 2\n*\n* idx = sub2ind( shape, strides, offset, 0, 1, mode );\n* // returns 3\n*\n* idx = sub2ind( shape, strides, offset, 1, 0, mode );\n* // returns 0\n*\n* idx = sub2ind( shape, strides, offset, 1, 1, mode );\n* // returns 1\n* ```\n*\n* In short, from the perspective of a view, view data is always ordered.\n*\n* @param {NonNegativeIntegerArray} shape - array shape\n* @param {IntegerArray} strides - stride array\n* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array\n* @param {...integer} i - subscripts\n* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions\n* @throws {RangeError} must provide subscripts which do not exceed array dimensions\n* @returns {NonNegativeInteger} linear index\n*\n* @example\n* var shape = [ 3, 3, 3 ];\n* var strides = [ 9, 3, 1 ];\n* var offset = 0;\n* var mode = [ 'throw' ]\n*\n* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode );\n* // returns [ 'throw' ]\n*/\nfunction sub2ind() {\n\tvar strides;\n\tvar offset;\n\tvar nmodes;\n\tvar shape;\n\tvar ndims;\n\tvar modes;\n\tvar mode;\n\tvar idx;\n\tvar m;\n\tvar s;\n\tvar j;\n\tvar i;\n\n\tshape = arguments[ 0 ];\n\tstrides = arguments[ 1 ];\n\toffset = arguments[ 2 ];\n\tndims = shape.length;\n\tmodes = arguments[ 3+ndims ]; // last argument\n\tnmodes = modes.length;\n\tidx = offset;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tm = shape[ i ];\n\t\tj = arguments[ i+3 ];\n\t\tmode = modes[ i%nmodes ];\n\t\tif ( mode === 'clamp' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj = 0;\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj = m - 1;\n\t\t\t}\n\t\t} else if ( mode === 'wrap' ) {\n\t\t\tif ( j < 0 ) {\n\t\t\t\tj += m; // slight optimization to avoid modulo arithmetic when |j| <= m\n\t\t\t\tif ( j < 0 ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t\tif ( j !== 0 ) {\n\t\t\t\t\t\tj += m;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if ( j >= m ) {\n\t\t\t\tj -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m\n\t\t\t\tif ( j >= m ) {\n\t\t\t\t\tj %= m;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif ( mode === 'normalize' && j < 0 ) {\n\t\t\t\tj += m;\n\t\t\t}\n\t\t\tif ( j < 0 || j >= m ) {\n\t\t\t\tthrow new RangeError( format( '0jX5J', i, j ) );\n\t\t\t}\n\t\t}\n\t\ts = strides[ i ];\n\n\t\t// Check if array view...\n\t\tif ( s < 0 && offset === 0 ) {\n\t\t\tidx -= j * s; // increments idx\n\t\t} else {\n\t\t\tidx += j * s; // may increment or decrement idx\n\t\t}\n\t}\n\treturn idx;\n}\n\n\n// EXPORTS //\n\nexport default sub2ind;\n"],"names":["sub2ind","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","i","arguments","length","RangeError","format"],"mappings":";;iGA+HA,SAASA,IACR,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EASJ,IANAX,EAAUY,UAAW,GACrBX,EAASW,UAAW,GAGpBV,GADAG,EAAQO,UAAW,GADnBR,GAHAD,EAAQS,UAAW,IAGLC,UAECA,OACfN,EAAMN,EACAU,EAAI,EAAGA,EAAIP,EAAOO,IAAM,CAI7B,GAHAH,EAAIL,EAAOQ,GACXD,EAAIE,UAAWD,EAAE,GAEH,WADdL,EAAOD,EAAOM,EAAET,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,KADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIM,WAAYC,EAAQ,QAASJ,EAAGD,KAG5CD,EAAIT,EAASW,IAGJ,GAAgB,IAAXV,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index c49fcb7..0000000 --- a/lib/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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'; - -/** -* Convert subscripts to a linear index. -* -* @module @stdlib/ndarray-base-sub2ind -* -* @example -* var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); -* -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ]; -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns 17 -*/ - -// MODULES // - -var sub2ind = require( './main.js' ); - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 298dd1b..0000000 --- a/lib/main.js +++ /dev/null @@ -1,197 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - The function accepts the following "modes": -* -* - **throw**: throw an error when a subscript exceeds array dimensions. -* - **normalize**: normalize negative subscripts and throw an error when a subscript exceeds array dimensions. -* - **wrap**: wrap around subscripts exceeding array dimensions using modulo arithmetic. -* - **clamp**: set subscripts exceeding array dimensions to either `0` (minimum index) or the maximum index along a particular dimension. -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. -* -* ```text -* Dims: 2x2 -* Buffer: [ 1, 2, 3, 4 ] -* -* View = [ a00, a01, -* a10, a11 ] -* -* Strides: 2,1 -* Offset: 0 -* -* View = [ 1, 2, -* 3, 4 ] -* -* Strides: 2,-1 -* Offset: 1 -* -* View = [ 2, 1, -* 4, 3 ] -* -* Strides: -2,1 -* Offset: 2 -* -* View = [ 3, 4, -* 1, 2 ] -* -* Strides: -2,-1 -* Offset: 3 -* -* View = [ 4, 3, -* 2, 1 ] -* ``` -* -* ```javascript -* var shape = [ 2, 2 ]; -* var strides = [ -2, 1 ]; -* var offset = 2; -* var mode = [ 'throw' ]; -* -* // From the perspective of a view... -* var idx = sub2ind( shape, strides, 0, 0, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, 0, 0, 1, mode ); -* // returns 1 -* -* idx = sub2ind( shape, strides, 0, 1, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, 0, 1, 1, mode ); -* // returns 3 -* -* // From the perspective of an underlying buffer... -* idx = sub2ind( shape, strides, offset, 0, 0, mode ); -* // returns 2 -* -* idx = sub2ind( shape, strides, offset, 0, 1, mode ); -* // returns 3 -* -* idx = sub2ind( shape, strides, offset, 1, 0, mode ); -* // returns 0 -* -* idx = sub2ind( shape, strides, offset, 1, 1, mode ); -* // returns 1 -* ``` -* -* In short, from the perspective of a view, view data is always ordered. -* -* @param {NonNegativeIntegerArray} shape - array shape -* @param {IntegerArray} strides - stride array -* @param {NonNegativeInteger} offset - location of the first indexed value **based** on the stride array -* @param {...integer} i - subscripts -* @param {StringArray} mode - specifies how to handle subscripts which exceed array dimensions -* @throws {RangeError} must provide subscripts which do not exceed array dimensions -* @returns {NonNegativeInteger} linear index -* -* @example -* var shape = [ 3, 3, 3 ]; -* var strides = [ 9, 3, 1 ]; -* var offset = 0; -* var mode = [ 'throw' ] -* -* var idx = sub2ind( shape, strides, offset, 1, 2, 2, mode ); -* // returns [ 'throw' ] -*/ -function sub2ind() { - var strides; - var offset; - var nmodes; - var shape; - var ndims; - var modes; - var mode; - var idx; - var m; - var s; - var j; - var i; - - shape = arguments[ 0 ]; - strides = arguments[ 1 ]; - offset = arguments[ 2 ]; - ndims = shape.length; - modes = arguments[ 3+ndims ]; // last argument - nmodes = modes.length; - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = arguments[ i+3 ]; - mode = modes[ i%nmodes ]; - if ( mode === 'clamp' ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode === 'wrap' ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j %= m; - if ( j !== 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode === 'normalize' && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - throw new RangeError( format( '0jX5J', i, j ) ); - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset === 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} - - -// EXPORTS // - -module.exports = sub2ind; diff --git a/manifest.json b/manifest.json deleted file mode 100644 index 1c0cf86..0000000 --- a/manifest.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "options": {}, - "fields": [ - { - "field": "src", - "resolve": true, - "relative": true - }, - { - "field": "include", - "resolve": true, - "relative": true - }, - { - "field": "libraries", - "resolve": false, - "relative": false - }, - { - "field": "libpath", - "resolve": true, - "relative": false - } - ], - "confs": [ - { - "src": [ - "./src/main.c" - ], - "include": [ - "./include" - ], - "libraries": [], - "libpath": [], - "dependencies": [ - "@stdlib/ndarray-index-modes" - ] - } - ] -} diff --git a/package.json b/package.json index 7d8add9..09c32e1 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,8 @@ "version": "0.2.2", "description": "Convert subscripts to a linear index.", "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", - "include": "./include", - "lib": "./lib", - "src": "./src", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.3", - "@stdlib/utils-library-manifest": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/assert-is-integer": "^0.2.2", - "@stdlib/math-base-special-floor": "^0.2.3", - "@stdlib/ndarray-base-numel": "^0.2.2", - "@stdlib/ndarray-base-shape2strides": "^0.2.2", - "@stdlib/ndarray-base-strides2offset": "^0.2.2", - "@stdlib/random-base-discrete-uniform": "^0.2.1", - "@stdlib/random-base-randu": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdtypes", @@ -95,7 +35,6 @@ "idx", "strided" ], - "__stdlib__": {}, "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 212a5e7..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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. -*/ - -#include "stdlib/ndarray/base/sub2ind.h" -#include "stdlib/ndarray/index_modes.h" -#include - -/** -* Converts subscripts to a linear index. -* -* ## Notes -* -* - When provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. -* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function treats subscripts as mapping to a linear index in an underlying data buffer for the array, thus returning a linear index from the perspective of that buffer. If an `offset` is equal to `0`, the function treats subscripts as mapping to a linear index in an array view, thus returning a linear index from the perspective of that view. In short, from the perspective of a view, view data is always ordered. -* - In "error" mode, the function returns `-1` if a subscript is out-of-bounds. -* -* @param ndims number of dimensions -* @param shape array shape (dimensions) -* @param strides array strides -* @param offset location of the first indexed value **based** on the stride array -* @param sub subscripts -* @param nmodes number of modes -* @param modes specifies how to handle subscripts which exceed array dimensions -* @return linear index -* -* @example -* #include "stdlib/ndarray/index_modes.h" -* #include "stdlib/ndarray/base/sub2ind.h" -* -* int64_t ndims = 3; -* int64_t shape[] = { 3, 3, 3 }; -* int64_t strides[] = { 9, 3, 1 }; -* int64_t offset = 0; -* -* int64_t nmodes = 1; -* int8_t modes[] = { STDLIB_NDARRAY_INDEX_ERROR }; -* -* int64_t sub[] = { 1, 2, 2 }; -* -* int64_t idx = stdlib_ndarray_sub2ind( ndims, shape, strides, offset, sub, nmodes, modes ); -* // returns 17 -*/ -int64_t stdlib_ndarray_sub2ind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const int64_t *sub, const int64_t nmodes, const int8_t *modes ) { - enum STDLIB_NDARRAY_INDEX_MODE mode; - int64_t idx; - int64_t s; - int64_t m; - int64_t j; - int64_t i; - - idx = offset; - for ( i = 0; i < ndims; i++ ) { - m = shape[ i ]; - j = sub[ i ]; - mode = modes[ i%nmodes ]; - if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) { - if ( j < 0 ) { - j = 0; - } else if ( j >= m ) { - j = m - 1; - } - } else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) { - if ( j < 0 ) { - j += m; // slight optimization to avoid modulo arithmetic when |j| <= m - if ( j < 0 ) { - j -= m*( (int64_t)( j/m ) ); // this is equivalent to `j mod m`, where the result has same sign as dividend (i.e., `j`); cannot use `%` as the sign of the result is implementation defined in C - if ( j != 0 ) { - j += m; - } - } - } else if ( j >= m ) { - j -= m; // slight optimization to avoid modulo arithmetic when m < j <= 2m - if ( j >= m ) { - j %= m; - } - } - } else { - if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && j < 0 ) { - j += m; - } - if ( j < 0 || j >= m ) { - return -1; - } - } - s = strides[ i ]; - - // Check if array view... - if ( s < 0 && offset == 0 ) { - idx -= j * s; // increments idx - } else { - idx += j * s; // may increment or decrement idx - } - } - return idx; -} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..8f79d18 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 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 main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 0d65a0a..0000000 --- a/test/test.js +++ /dev/null @@ -1,463 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 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 sub2ind = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof sub2ind, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (simple)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 2; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 1; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 3; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function converts subscripts to a linear index (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, -1 ]; - offset = 0; - mode = [ 'throw' ]; - - idx = sub2ind( shape, strides, offset, 0, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `throw`, the function throws if provided a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'throw' ]; - - t.throws( foo, RangeError, 'throws a range error' ); - t.throws( bar, RangeError, 'throws a range error' ); - - t.end(); - - function foo() { - sub2ind( shape, strides, offset, 999999, 1, mode ); - } - - function bar() { - sub2ind( shape, strides, offset, 1, 999999, mode ); - } -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `wrap`, the function wraps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'wrap' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, -4, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `clamp`, the function clamps a subscript which exceeds array dimensions (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'clamp' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 2, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, 3, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -3, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 1, 5, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'if a dimension index `mode` is `normalize`, the function normalizes negative subscripts (offset=0)', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2 ]; - strides = [ -2, 1 ]; - offset = 0; - mode = [ 'normalize' ]; - - idx = sub2ind( shape, strides, offset, 1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, 0, mode ); - t.strictEqual( idx, 2, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, 0, -1, mode ); - t.strictEqual( idx, 1, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -2, 0, mode ); - t.strictEqual( idx, 0, 'returns expected value' ); - - idx = sub2ind( shape, strides, offset, -1, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function supports providing mixed modes', function test( t ) { - var strides; - var offset; - var shape; - var mode; - var idx; - - shape = [ 2, 2, 2 ]; - strides = [ 4, 2, 1 ]; - offset = 0; - mode = [ 'wrap', 'clamp' ]; - - idx = sub2ind( shape, strides, offset, -2, 10, -1, mode ); - t.strictEqual( idx, 3, 'returns expected value' ); - - t.end(); -});