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 +``` -```javascript -var sub2ind = require( '@stdlib/ndarray-base-sub2ind' ); +If no recognized module system is present, access bundle contents via the global scope: + +```html + ``` #### sub2ind( shape, strides, offset, ...subscripts, mode ) @@ -211,13 +217,18 @@ idx = sub2ind( shape, strides, offset, 10, 10, mode ); -```javascript -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( '@stdlib/ndarray-base-sub2ind' ); +```html + + + + + + + + + + + + ```
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/browser.js b/browser.js new file mode 100644 index 0000000..44cdaa8 --- /dev/null +++ b/browser.js @@ -0,0 +1,3 @@ +// Copyright (c) 2025 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +var e,n;e=this,n=function(){"use strict";function e(){var e,n=arguments,t="https://stdlib.io/e/"+n[0]+"?";for(e=1;e=s&&(a=s-1);else if("wrap"===l)a<0?(a+=s)<0&&0!=(a%=s)&&(a+=s):a>=s&&(a-=s)>=s&&(a%=s);else if("normalize"===l&&a<0&&(a+=s),a<0||a>=s)throw new RangeError(e("0jX5J",p,a));(u=n[p])<0&&0===t?d-=a*u:d+=a*u}return d}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).sub2ind=n(); +//# sourceMappingURL=browser.js.map diff --git a/browser.js.map b/browser.js.map new file mode 100644 index 0000000..fafdc57 --- /dev/null +++ b/browser.js.map @@ -0,0 +1 @@ +{"version":3,"file":"browser.js","sources":["../node_modules/@stdlib/error-tools-fmtprodmsg/lib/main.js","../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 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// MAIN //\n\n/**\n* Formats an error message for production.\n*\n* @param {string} code - error code\n* @param {*} ...args - error message arguments\n* @returns {string} formatted error message\n*\n* @example\n* var msg = fmtprodmsg( '3', 'wrong_type' );\n* // returns 'https://stdlib.io/e/3?&arg[]=wrong_type'\n*/\nfunction fmtprodmsg() {\n\tvar a = arguments;\n\tvar c = a[ 0 ];\n\tvar u = 'https://stdlib.io/e/'+c+'?';\n\tvar i;\n\tfor ( i = 1; i < a.length; i++ ) {\n\t\tu += '&arg[]=' + encodeURIComponent( a[ i ] );\n\t}\n\treturn u;\n}\n\n\n// EXPORTS //\n\nexport default fmtprodmsg;\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// 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":["fmtprodmsg","i","a","arguments","u","length","encodeURIComponent","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","RangeError","format"],"mappings":";yCAiCA,SAASA,IACR,IAGIC,EAHAC,EAAIC,UAEJC,EAAI,uBADAF,EAAG,GACsB,IAEjC,IAAMD,EAAI,EAAGA,EAAIC,EAAEG,OAAQJ,IAC1BG,GAAK,UAAYE,mBAAoBJ,EAAGD,IAEzC,OAAOG,CACR,QCqFA,WACC,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAhB,EASJ,IANAM,EAAUJ,UAAW,GACrBK,EAASL,UAAW,GAGpBM,GADAG,EAAQT,UAAW,GADnBQ,GAHAD,EAAQP,UAAW,IAGLE,UAECA,OACfS,EAAMN,EACAP,EAAI,EAAGA,EAAIU,EAAOV,IAAM,CAI7B,GAHAc,EAAIL,EAAOT,GACXgB,EAAId,UAAWF,EAAE,GAEH,WADdY,EAAOD,EAAOX,EAAEQ,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,IADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIG,WAAYC,EAAQ,QAASlB,EAAGgB,KAG5CD,EAAIT,EAASN,IAGJ,GAAgB,IAAXO,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR","x_google_ignoreList":[0]} \ 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/index.d.ts b/docs/types/index.d.ts deleted file mode 100644 index 1dd968a..0000000 --- a/docs/types/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/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/index.js b/index.js new file mode 100644 index 0000000..ea9fab0 --- /dev/null +++ b/index.js @@ -0,0 +1,3 @@ +// Copyright (c) 2025 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +var e,n;e=this,n=function(){"use strict";function e(){var e,n=arguments,t="https://stdlib.io/e/"+n[0]+"?";for(e=1;e=s&&(a=s-1);else if("wrap"===l)a<0?(a+=s)<0&&0!=(a%=s)&&(a+=s):a>=s&&(a-=s)>=s&&(a%=s);else if("normalize"===l&&a<0&&(a+=s),a<0||a>=s)throw new RangeError(e("0jX5J",p,a));(u=n[p])<0&&0===t?d-=a*u:d+=a*u}return d}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).sub2ind=n(); +//# sourceMappingURL=index.js.map diff --git a/index.js.map b/index.js.map new file mode 100644 index 0000000..2629027 --- /dev/null +++ b/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sources":["../node_modules/@stdlib/error-tools-fmtprodmsg/lib/main.js","../lib/main.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 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// MAIN //\n\n/**\n* Formats an error message for production.\n*\n* @param {string} code - error code\n* @param {*} ...args - error message arguments\n* @returns {string} formatted error message\n*\n* @example\n* var msg = fmtprodmsg( '3', 'wrong_type' );\n* // returns 'https://stdlib.io/e/3?&arg[]=wrong_type'\n*/\nfunction fmtprodmsg() {\n\tvar a = arguments;\n\tvar c = a[ 0 ];\n\tvar u = 'https://stdlib.io/e/'+c+'?';\n\tvar i;\n\tfor ( i = 1; i < a.length; i++ ) {\n\t\tu += '&arg[]=' + encodeURIComponent( a[ i ] );\n\t}\n\treturn u;\n}\n\n\n// EXPORTS //\n\nexport default fmtprodmsg;\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// 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":["fmtprodmsg","i","a","arguments","u","length","encodeURIComponent","strides","offset","nmodes","shape","ndims","modes","mode","idx","m","s","j","RangeError","format"],"mappings":";yCAiCA,SAASA,IACR,IAGIC,EAHAC,EAAIC,UAEJC,EAAI,uBADAF,EAAG,GACsB,IAEjC,IAAMD,EAAI,EAAGA,EAAIC,EAAEG,OAAQJ,IAC1BG,GAAK,UAAYE,mBAAoBJ,EAAGD,IAEzC,OAAOG,CACR,QCqFA,WACC,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAhB,EASJ,IANAM,EAAUJ,UAAW,GACrBK,EAASL,UAAW,GAGpBM,GADAG,EAAQT,UAAW,GADnBQ,GAHAD,EAAQP,UAAW,IAGLE,UAECA,OACfS,EAAMN,EACAP,EAAI,EAAGA,EAAIU,EAAOV,IAAM,CAI7B,GAHAc,EAAIL,EAAOT,GACXgB,EAAId,UAAWF,EAAE,GAEH,WADdY,EAAOD,EAAOX,EAAEQ,IAEVQ,EAAI,EACRA,EAAI,EACOA,GAAKF,IAChBE,EAAIF,EAAI,QAEH,GAAc,SAATF,EACNI,EAAI,GACRA,GAAKF,GACI,GAEG,IADXE,GAAKF,KAEJE,GAAKF,GAGIE,GAAKF,IAChBE,GAAKF,IACKA,IACTE,GAAKF,QAOP,GAHc,cAATF,GAAwBI,EAAI,IAChCA,GAAKF,GAEDE,EAAI,GAAKA,GAAKF,EAClB,MAAM,IAAIG,WAAYC,EAAQ,QAASlB,EAAGgB,KAG5CD,EAAIT,EAASN,IAGJ,GAAgB,IAAXO,EACbM,GAAOG,EAAID,EAEXF,GAAOG,EAAID,CAEZ,CACD,OAAOF,CACR","x_google_ignoreList":[0]} \ 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 6b2705c..f093420 100644 --- a/package.json +++ b/package.json @@ -3,33 +3,7 @@ "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" - }, + "main": "./index.js", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -38,41 +12,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/ndarray-index-modes": "^0.2.2", - "@stdlib/string-format": "^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 +34,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_browser.html b/stats_browser.html new file mode 100644 index 0000000..43e9ffc --- /dev/null +++ b/stats_browser.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/stats_node.html b/stats_node.html new file mode 100644 index 0000000..c640583 --- /dev/null +++ b/stats_node.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(); -});