@stdlib/ndarray-base-bind2vind 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +1 -1
- package/README.md +11 -7
- package/SECURITY.md +5 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +2 -2
- package/docs/types/index.d.ts +1 -1
- package/include/stdlib/ndarray/base/bind2vind.h +1 -1
- package/lib/main.js +7 -2
- package/package.json +9 -30
- package/src/main.c +33 -26
- package/CITATION.cff +0 -30
package/NOTICE
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Copyright (c) 2016-
|
|
1
|
+
Copyright (c) 2016-2024 The Stdlib Authors.
|
package/README.md
CHANGED
|
@@ -77,11 +77,12 @@ var idx = bind2vind( shape, strides, offset, order, 7, 'throw' );
|
|
|
77
77
|
// returns 1
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
-
The function supports the following
|
|
80
|
+
The function supports the following modes:
|
|
81
81
|
|
|
82
|
-
-
|
|
83
|
-
-
|
|
84
|
-
-
|
|
82
|
+
- **throw**: specifies that the function should throw an error when a linear index exceeds array dimensions.
|
|
83
|
+
- **normalize**: specifies that the function should normalize negative indices and should throw an error when a linear index exceeds array dimensions.
|
|
84
|
+
- **wrap**: specifies that the function should wrap around a linear index exceeding array dimensions using modulo arithmetic.
|
|
85
|
+
- **clamp**: specifies that the function should set a linear index exceeding array dimensions to either `0` (minimum linear index) or the maximum linear index.
|
|
85
86
|
|
|
86
87
|
```javascript
|
|
87
88
|
var shape = [ 2, 2 ];
|
|
@@ -213,7 +214,7 @@ See [LICENSE][stdlib-license].
|
|
|
213
214
|
|
|
214
215
|
## Copyright
|
|
215
216
|
|
|
216
|
-
Copyright © 2016-
|
|
217
|
+
Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
217
218
|
|
|
218
219
|
</section>
|
|
219
220
|
|
|
@@ -226,8 +227,8 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors].
|
|
|
226
227
|
[npm-image]: http://img.shields.io/npm/v/@stdlib/ndarray-base-bind2vind.svg
|
|
227
228
|
[npm-url]: https://npmjs.org/package/@stdlib/ndarray-base-bind2vind
|
|
228
229
|
|
|
229
|
-
[test-image]: https://github.com/stdlib-js/ndarray-base-bind2vind/actions/workflows/test.yml/badge.svg?branch=v0.1
|
|
230
|
-
[test-url]: https://github.com/stdlib-js/ndarray-base-bind2vind/actions/workflows/test.yml?query=branch:v0.1
|
|
230
|
+
[test-image]: https://github.com/stdlib-js/ndarray-base-bind2vind/actions/workflows/test.yml/badge.svg?branch=v0.2.1
|
|
231
|
+
[test-url]: https://github.com/stdlib-js/ndarray-base-bind2vind/actions/workflows/test.yml?query=branch:v0.2.1
|
|
231
232
|
|
|
232
233
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/ndarray-base-bind2vind/main.svg
|
|
233
234
|
[coverage-url]: https://codecov.io/github/stdlib-js/ndarray-base-bind2vind?branch=main
|
|
@@ -250,8 +251,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors].
|
|
|
250
251
|
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
|
|
251
252
|
|
|
252
253
|
[deno-url]: https://github.com/stdlib-js/ndarray-base-bind2vind/tree/deno
|
|
254
|
+
[deno-readme]: https://github.com/stdlib-js/ndarray-base-bind2vind/blob/deno/README.md
|
|
253
255
|
[umd-url]: https://github.com/stdlib-js/ndarray-base-bind2vind/tree/umd
|
|
256
|
+
[umd-readme]: https://github.com/stdlib-js/ndarray-base-bind2vind/blob/umd/README.md
|
|
254
257
|
[esm-url]: https://github.com/stdlib-js/ndarray-base-bind2vind/tree/esm
|
|
258
|
+
[esm-readme]: https://github.com/stdlib-js/ndarray-base-bind2vind/blob/esm/README.md
|
|
255
259
|
[branches-url]: https://github.com/stdlib-js/ndarray-base-bind2vind/blob/main/branches.md
|
|
256
260
|
|
|
257
261
|
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/ndarray-base-bind2vind/main/LICENSE
|
package/SECURITY.md
ADDED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";var q=function(u,t){return function(){return t||u((t={exports:{}}).exports,t),t.exports}};var c=q(function(p,i){
|
|
2
|
-
var g=require('@stdlib/error-tools-fmtprodmsg/dist'),
|
|
2
|
+
var g=require('@stdlib/error-tools-fmtprodmsg/dist'),o=require('@stdlib/math-base-special-trunc/dist'),m=require('@stdlib/math-base-special-abs/dist');function w(u,t,j,b,r,v){var l,e,s,f,n,a;for(l=u.length,e=1,a=0;a<l;a++)e*=u[a];if(v==="clamp")r<0?r=0:r>=e&&(r=e-1);else if(v==="wrap")r<0?(r+=e,r<0&&(r%=e,r!==0&&(r+=e))):r>=e&&(r-=e,r>=e&&(r%=e));else if(v==="normalize"&&r<0&&(r+=e),r<0||r>=e)throw new RangeError(g('0i35E',e,r));if(s=0,b==="column-major"){for(a=l-1;a>=0;a--)n=t[a],n<0?(f=o(r/n),r-=f*n,f+=u[a]-1):(f=o(r/n),r-=f*n),s+=f*m(n);return s}for(a=0;a<l;a++)n=t[a],n<0?(f=o(r/n),r-=f*n,f+=u[a]-1):(f=o(r/n),r-=f*n),s+=f*m(n);return s}i.exports=w
|
|
3
3
|
});var y=c();module.exports=y;
|
|
4
4
|
/** @license Apache-2.0 */
|
|
5
5
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../lib/main.js", "../lib/index.js"],
|
|
4
|
-
"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' );\nvar trunc = require( '@stdlib/math-base-special-trunc' );\nvar abs = require( '@stdlib/math-base-special-abs' );\n\n\n// MAIN //\n\n/**\n* Converts a linear index in an underlying data buffer to a linear index in an array view.\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 {string} order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)\n* @param {integer} idx - linear index in an underlying data buffer\n* @param {string} mode - specifies how to handle a linear index which exceeds array dimensions\n* @throws {RangeError} linear index must not exceed array dimensions\n* @returns {NonNegativeInteger} linear index in an array view\n*\n* @example\n* var shape = [ 3, 3 ];\n* var strides = [ -3, 1 ];\n* var offset = 6;\n* var order = 'row-major';\n* var mode = 'throw';\n*\n* var ind = bind2vind( shape, strides, offset, order, 7, mode );\n* // returns 1\n*/\nfunction bind2vind( shape, strides, offset, order, idx, mode ) {\n\tvar ndims;\n\tvar len;\n\tvar ind;\n\tvar k;\n\tvar s;\n\tvar i;\n\n\tndims = shape.length;\n\tlen = 1;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tlen *= shape[ i ];\n\t}\n\tif ( mode === 'clamp' ) {\n\t\tif ( idx < 0 ) {\n\t\t\tidx = 0;\n\t\t} else if ( idx >= len ) {\n\t\t\tidx = len - 1;\n\t\t}\n\t} else if ( mode === 'wrap' ) {\n\t\tif ( idx < 0 ) {\n\t\t\tidx += len; // slight optimization to avoid modulo arithmetic when |idx| <= len\n\t\t\tif ( idx < 0 ) {\n\t\t\t\tidx %= len;\n\t\t\t\tif ( idx !== 0 ) {\n\t\t\t\t\tidx += len;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( idx >= len ) {\n\t\t\tidx -= len; // slight optimization to avoid modulo arithmetic when len < idx <= 2*len\n\t\t\tif ( idx >= len ) {\n\t\t\t\tidx %= len;\n\t\t\t}\n\t\t}\n\t} else
|
|
5
|
-
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAC1CC,EAAQ,QAAS,iCAAkC,EACnDC,EAAM,QAAS,+BAAgC,EA2BnD,SAASC,EAAWC,EAAOC,EAASC,EAAQC,EAAOC,EAAKC,EAAO,CAC9D,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EAIJ,IAFAL,EAAQN,EAAM,OACdO,EAAM,EACAI,EAAI,EAAGA,EAAIL,EAAOK,IACvBJ,GAAOP,EAAOW,CAAE,EAEjB,GAAKN,IAAS,QACRD,EAAM,EACVA,EAAM,EACKA,GAAOG,IAClBH,EAAMG,EAAM,WAEFF,IAAS,OACfD,EAAM,GACVA,GAAOG,EACFH,EAAM,IACVA,GAAOG,EACFH,IAAQ,IACZA,GAAOG,KAGEH,GAAOG,IAClBH,GAAOG,EACFH,GAAOG,IACXH,GAAOG,
|
|
4
|
+
"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' );\nvar trunc = require( '@stdlib/math-base-special-trunc' );\nvar abs = require( '@stdlib/math-base-special-abs' );\n\n\n// MAIN //\n\n/**\n* Converts a linear index in an underlying data buffer to a linear index in an array view.\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 {string} order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)\n* @param {integer} idx - linear index in an underlying data buffer\n* @param {string} mode - specifies how to handle a linear index which exceeds array dimensions\n* @throws {RangeError} linear index must not exceed array dimensions\n* @returns {NonNegativeInteger} linear index in an array view\n*\n* @example\n* var shape = [ 3, 3 ];\n* var strides = [ -3, 1 ];\n* var offset = 6;\n* var order = 'row-major';\n* var mode = 'throw';\n*\n* var ind = bind2vind( shape, strides, offset, order, 7, mode );\n* // returns 1\n*/\nfunction bind2vind( shape, strides, offset, order, idx, mode ) {\n\tvar ndims;\n\tvar len;\n\tvar ind;\n\tvar k;\n\tvar s;\n\tvar i;\n\n\tndims = shape.length;\n\tlen = 1;\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\tlen *= shape[ i ];\n\t}\n\tif ( mode === 'clamp' ) {\n\t\tif ( idx < 0 ) {\n\t\t\tidx = 0;\n\t\t} else if ( idx >= len ) {\n\t\t\tidx = len - 1;\n\t\t}\n\t} else if ( mode === 'wrap' ) {\n\t\tif ( idx < 0 ) {\n\t\t\tidx += len; // slight optimization to avoid modulo arithmetic when |idx| <= len\n\t\t\tif ( idx < 0 ) {\n\t\t\t\tidx %= len;\n\t\t\t\tif ( idx !== 0 ) {\n\t\t\t\t\tidx += len;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if ( idx >= len ) {\n\t\t\tidx -= len; // slight optimization to avoid modulo arithmetic when len < idx <= 2*len\n\t\t\tif ( idx >= len ) {\n\t\t\t\tidx %= len;\n\t\t\t}\n\t\t}\n\t} else {\n\t\tif ( mode === 'normalize' && idx < 0 ) {\n\t\t\tidx += len;\n\t\t}\n\t\tif ( idx < 0 || idx >= len ) {\n\t\t\tthrow new RangeError( format( 'invalid argument. Linear index must not exceed array dimensions. Number of array elements: `%u`. Value: `%d`.', len, idx ) );\n\t\t}\n\t}\n\t// The approach which follows is to resolve a buffer index to its subscripts and then plug the subscripts into the standard formula for computing the linear index in the array view (i.e., where all strides are positive and offset is 0)...\n\tind = 0;\n\tif ( order === 'column-major' ) {\n\t\tfor ( i = ndims-1; i >= 0; i-- ) {\n\t\t\ts = strides[ i ];\n\t\t\tif ( s < 0 ) {\n\t\t\t\tk = trunc( idx/s );\n\t\t\t\tidx -= k * s;\n\t\t\t\tk += shape[ i ] - 1;\n\t\t\t} else {\n\t\t\t\tk = trunc( idx/s );\n\t\t\t\tidx -= k * s;\n\t\t\t}\n\t\t\tind += k * abs( s );\n\t\t}\n\t\treturn ind;\n\t}\n\t// Case: row-major\n\tfor ( i = 0; i < ndims; i++ ) {\n\t\ts = strides[ i ];\n\t\tif ( s < 0 ) {\n\t\t\tk = trunc( idx/s );\n\t\t\tidx -= k * s;\n\t\t\tk += shape[ i ] - 1;\n\t\t} else {\n\t\t\tk = trunc( idx/s );\n\t\t\tidx -= k * s;\n\t\t}\n\t\tind += k * abs( s );\n\t}\n\treturn ind;\n}\n\n\n// EXPORTS //\n\nmodule.exports = bind2vind;\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 a linear index in an underlying data buffer to a linear index in an array view.\n*\n* @module @stdlib/ndarray-base-bind2vind\n*\n* @example\n* var bind2vind = require( '@stdlib/ndarray-base-bind2vind' );\n*\n* var shape = [ 3, 3 ];\n* var strides = [ -3, 1 ];\n* var offset = 6;\n* var order = 'row-major';\n* var mode = 'throw';\n*\n* var ind = bind2vind( shape, strides, offset, order, 7, mode );\n* // returns 1\n*/\n\n// MODULES //\n\nvar bind2vind = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = bind2vind;\n"],
|
|
5
|
+
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,uBAAwB,EAC1CC,EAAQ,QAAS,iCAAkC,EACnDC,EAAM,QAAS,+BAAgC,EA2BnD,SAASC,EAAWC,EAAOC,EAASC,EAAQC,EAAOC,EAAKC,EAAO,CAC9D,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EAIJ,IAFAL,EAAQN,EAAM,OACdO,EAAM,EACAI,EAAI,EAAGA,EAAIL,EAAOK,IACvBJ,GAAOP,EAAOW,CAAE,EAEjB,GAAKN,IAAS,QACRD,EAAM,EACVA,EAAM,EACKA,GAAOG,IAClBH,EAAMG,EAAM,WAEFF,IAAS,OACfD,EAAM,GACVA,GAAOG,EACFH,EAAM,IACVA,GAAOG,EACFH,IAAQ,IACZA,GAAOG,KAGEH,GAAOG,IAClBH,GAAOG,EACFH,GAAOG,IACXH,GAAOG,YAIJF,IAAS,aAAeD,EAAM,IAClCA,GAAOG,GAEHH,EAAM,GAAKA,GAAOG,EACtB,MAAM,IAAI,WAAYX,EAAQ,gHAAiHW,EAAKH,CAAI,CAAE,EAK5J,GADAI,EAAM,EACDL,IAAU,eAAiB,CAC/B,IAAMQ,EAAIL,EAAM,EAAGK,GAAK,EAAGA,IAC1BD,EAAIT,EAASU,CAAE,EACVD,EAAI,GACRD,EAAIZ,EAAOO,EAAIM,CAAE,EACjBN,GAAOK,EAAIC,EACXD,GAAKT,EAAOW,CAAE,EAAI,IAElBF,EAAIZ,EAAOO,EAAIM,CAAE,EACjBN,GAAOK,EAAIC,GAEZF,GAAOC,EAAIX,EAAKY,CAAE,EAEnB,OAAOF,CACR,CAEA,IAAMG,EAAI,EAAGA,EAAIL,EAAOK,IACvBD,EAAIT,EAASU,CAAE,EACVD,EAAI,GACRD,EAAIZ,EAAOO,EAAIM,CAAE,EACjBN,GAAOK,EAAIC,EACXD,GAAKT,EAAOW,CAAE,EAAI,IAElBF,EAAIZ,EAAOO,EAAIM,CAAE,EACjBN,GAAOK,EAAIC,GAEZF,GAAOC,EAAIX,EAAKY,CAAE,EAEnB,OAAOF,CACR,CAKAb,EAAO,QAAUI,ICzFjB,IAAIa,EAAY,IAKhB,OAAO,QAAUA",
|
|
6
6
|
"names": ["require_main", "__commonJSMin", "exports", "module", "format", "trunc", "abs", "bind2vind", "shape", "strides", "offset", "order", "idx", "mode", "ndims", "len", "ind", "k", "s", "i", "bind2vind"]
|
|
7
7
|
}
|
package/docs/types/index.d.ts
CHANGED
|
@@ -46,7 +46,7 @@ import { Mode, Order } from '@stdlib/types/ndarray';
|
|
|
46
46
|
* var ind = bind2vind( shape, strides, offset, order, 7, mode );
|
|
47
47
|
* // returns 1
|
|
48
48
|
*/
|
|
49
|
-
declare function bind2vind( shape: ArrayLike<number>, strides: ArrayLike<number>, offset: number, order: Order, idx: number, mode: Mode ): number;
|
|
49
|
+
declare function bind2vind( shape: ArrayLike<number>, strides: ArrayLike<number>, offset: number, order: Order, idx: number, mode: Mode ): number;
|
|
50
50
|
|
|
51
51
|
|
|
52
52
|
// EXPORTS //
|
|
@@ -33,7 +33,7 @@ extern "C" {
|
|
|
33
33
|
/**
|
|
34
34
|
* Converts a linear index in an underlying data buffer to a linear index in an array view.
|
|
35
35
|
*/
|
|
36
|
-
int64_t stdlib_ndarray_bind2vind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, enum STDLIB_NDARRAY_ORDER order, int64_t idx, enum STDLIB_NDARRAY_INDEX_MODE mode );
|
|
36
|
+
int64_t stdlib_ndarray_bind2vind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const enum STDLIB_NDARRAY_ORDER order, const int64_t idx, const enum STDLIB_NDARRAY_INDEX_MODE mode );
|
|
37
37
|
|
|
38
38
|
#ifdef __cplusplus
|
|
39
39
|
}
|
package/lib/main.js
CHANGED
|
@@ -83,8 +83,13 @@ function bind2vind( shape, strides, offset, order, idx, mode ) {
|
|
|
83
83
|
idx %= len;
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
-
} else
|
|
87
|
-
|
|
86
|
+
} else {
|
|
87
|
+
if ( mode === 'normalize' && idx < 0 ) {
|
|
88
|
+
idx += len;
|
|
89
|
+
}
|
|
90
|
+
if ( idx < 0 || idx >= len ) {
|
|
91
|
+
throw new RangeError( format( 'invalid argument. Linear index must not exceed array dimensions. Number of array elements: `%u`. Value: `%d`.', len, idx ) );
|
|
92
|
+
}
|
|
88
93
|
}
|
|
89
94
|
// The approach which follows is to resolve a buffer index to its subscripts and then plug the subscripts into the standard formula for computing the linear index in the array view (i.e., where all strides are positive and offset is 0)...
|
|
90
95
|
ind = 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/ndarray-base-bind2vind",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Convert a linear index in an underlying data buffer to a linear index in an array view.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -15,21 +15,14 @@
|
|
|
15
15
|
],
|
|
16
16
|
"main": "./lib",
|
|
17
17
|
"directories": {
|
|
18
|
-
"benchmark": "./benchmark",
|
|
19
18
|
"doc": "./docs",
|
|
20
|
-
"example": "./examples",
|
|
21
19
|
"include": "./include",
|
|
22
20
|
"lib": "./lib",
|
|
23
21
|
"src": "./src",
|
|
24
|
-
"
|
|
22
|
+
"dist": "./dist"
|
|
25
23
|
},
|
|
26
24
|
"types": "./docs/types",
|
|
27
|
-
"scripts": {
|
|
28
|
-
"test": "make test",
|
|
29
|
-
"test-cov": "make test-cov",
|
|
30
|
-
"examples": "make examples",
|
|
31
|
-
"benchmark": "make benchmark"
|
|
32
|
-
},
|
|
25
|
+
"scripts": {},
|
|
33
26
|
"homepage": "https://stdlib.io",
|
|
34
27
|
"repository": {
|
|
35
28
|
"type": "git",
|
|
@@ -39,27 +32,13 @@
|
|
|
39
32
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
40
33
|
},
|
|
41
34
|
"dependencies": {
|
|
42
|
-
"@stdlib/math-base-special-abs": "^0.1
|
|
43
|
-
"@stdlib/math-base-special-trunc": "^0.1
|
|
44
|
-
"@stdlib/ndarray-index-modes": "^0.1
|
|
45
|
-
"@stdlib/ndarray-orders": "^0.1
|
|
46
|
-
"@stdlib/string-format": "^0.1
|
|
47
|
-
"@stdlib/types": "^0.1.0",
|
|
48
|
-
"@stdlib/utils-library-manifest": "^0.1.0",
|
|
49
|
-
"@stdlib/error-tools-fmtprodmsg": "^0.1.0"
|
|
50
|
-
},
|
|
51
|
-
"devDependencies": {
|
|
52
|
-
"@stdlib/assert-is-integer": "^0.1.0",
|
|
53
|
-
"@stdlib/bench": "^0.1.0",
|
|
54
|
-
"@stdlib/math-base-special-floor": "^0.1.0",
|
|
55
|
-
"@stdlib/ndarray-base-numel": "^0.1.0",
|
|
56
|
-
"@stdlib/ndarray-base-shape2strides": "^0.1.0",
|
|
57
|
-
"@stdlib/ndarray-base-strides2offset": "^0.1.0",
|
|
58
|
-
"@stdlib/random-base-randu": "^0.0.8",
|
|
59
|
-
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
|
|
60
|
-
"istanbul": "^0.4.1",
|
|
61
|
-
"tap-min": "git+https://github.com/Planeshifter/tap-min.git"
|
|
35
|
+
"@stdlib/math-base-special-abs": "^0.2.1",
|
|
36
|
+
"@stdlib/math-base-special-trunc": "^0.2.1",
|
|
37
|
+
"@stdlib/ndarray-index-modes": "^0.2.1",
|
|
38
|
+
"@stdlib/ndarray-orders": "^0.2.1",
|
|
39
|
+
"@stdlib/string-format": "^0.2.1"
|
|
62
40
|
},
|
|
41
|
+
"devDependencies": {},
|
|
63
42
|
"engines": {
|
|
64
43
|
"node": ">=0.10.0",
|
|
65
44
|
"npm": ">2.7.0"
|
package/src/main.c
CHANGED
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
*
|
|
28
28
|
* ## Notes
|
|
29
29
|
*
|
|
30
|
-
* - In "error"
|
|
30
|
+
* - In "error" and "normalize" modes, the function returns `-1` if an index is out-of-bounds.
|
|
31
31
|
*
|
|
32
32
|
* @param ndims number of dimensions
|
|
33
33
|
* @param shape array shape (dimensions)
|
|
@@ -51,40 +51,47 @@
|
|
|
51
51
|
* int64_t idx = stdlib_ndarray_bind2vind( ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, 7, STDLIB_NDARRAY_INDEX_ERROR );
|
|
52
52
|
* // returns 1
|
|
53
53
|
*/
|
|
54
|
-
int64_t stdlib_ndarray_bind2vind( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, enum STDLIB_NDARRAY_ORDER order, int64_t idx, enum STDLIB_NDARRAY_INDEX_MODE mode ) {
|
|
54
|
+
int64_t stdlib_ndarray_bind2vind( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const enum STDLIB_NDARRAY_ORDER order, const int64_t idx, const enum STDLIB_NDARRAY_INDEX_MODE mode ) {
|
|
55
|
+
int64_t index;
|
|
55
56
|
int64_t len;
|
|
56
57
|
int64_t ind;
|
|
57
58
|
int64_t s;
|
|
58
59
|
int64_t k;
|
|
59
60
|
int64_t i;
|
|
60
61
|
|
|
62
|
+
index = idx;
|
|
61
63
|
len = 1;
|
|
62
64
|
for ( i = 0; i < ndims; i++ ) {
|
|
63
65
|
len *= shape[ i ];
|
|
64
66
|
}
|
|
65
67
|
if ( mode == STDLIB_NDARRAY_INDEX_CLAMP ) {
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
} else if (
|
|
69
|
-
|
|
68
|
+
if ( index < 0 ) {
|
|
69
|
+
index = 0;
|
|
70
|
+
} else if ( index >= len ) {
|
|
71
|
+
index = len - 1;
|
|
70
72
|
}
|
|
71
73
|
} else if ( mode == STDLIB_NDARRAY_INDEX_WRAP ) {
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
if (
|
|
77
|
-
|
|
74
|
+
if ( index < 0 ) {
|
|
75
|
+
index += len; // slight optimization to avoid modulo arithmetic when |index| <= len
|
|
76
|
+
if ( index < 0 ) {
|
|
77
|
+
index -= len*( (int64_t)( index/len ) ); // this is equivalent to `index mod len`, where the result has same sign as dividend (i.e., `index`); cannot use `%` as the sign of the result is implementation defined in C
|
|
78
|
+
if ( index != 0 ) {
|
|
79
|
+
index += len;
|
|
78
80
|
}
|
|
79
81
|
}
|
|
80
|
-
} else if (
|
|
81
|
-
|
|
82
|
-
if (
|
|
83
|
-
|
|
82
|
+
} else if ( index >= len ) {
|
|
83
|
+
index -= len; // slight optimization to avoid modulo arithmetic when len < index <= 2*len
|
|
84
|
+
if ( index >= len ) {
|
|
85
|
+
index %= len;
|
|
84
86
|
}
|
|
85
87
|
}
|
|
86
|
-
} else
|
|
87
|
-
|
|
88
|
+
} else {
|
|
89
|
+
if ( mode == STDLIB_NDARRAY_INDEX_NORMALIZE && index < 0 ) {
|
|
90
|
+
index += len;
|
|
91
|
+
}
|
|
92
|
+
if ( index < 0 || index >= len ) {
|
|
93
|
+
return -1;
|
|
94
|
+
}
|
|
88
95
|
}
|
|
89
96
|
// The approach which follows is to resolve a buffer index to its subscripts and then plug the subscripts into the standard formula for computing the linear index in the array view (i.e., where all strides are positive and offset is 0)...
|
|
90
97
|
ind = 0;
|
|
@@ -92,12 +99,12 @@ int64_t stdlib_ndarray_bind2vind( int64_t ndims, int64_t *shape, int64_t *stride
|
|
|
92
99
|
for ( i = ndims-1; i >= 0; i-- ) {
|
|
93
100
|
s = strides[ i ];
|
|
94
101
|
if ( s < 0 ) {
|
|
95
|
-
k =
|
|
96
|
-
|
|
102
|
+
k = index / s; // truncates
|
|
103
|
+
index -= k * s;
|
|
97
104
|
k += shape[ i ] - 1;
|
|
98
105
|
} else {
|
|
99
|
-
k =
|
|
100
|
-
|
|
106
|
+
k = index / s; // cppcheck-suppress zerodivcond // truncates
|
|
107
|
+
index -= k * s;
|
|
101
108
|
}
|
|
102
109
|
ind += k * llabs( s );
|
|
103
110
|
}
|
|
@@ -107,12 +114,12 @@ int64_t stdlib_ndarray_bind2vind( int64_t ndims, int64_t *shape, int64_t *stride
|
|
|
107
114
|
for ( i = 0; i < ndims; i++ ) {
|
|
108
115
|
s = strides[ i ];
|
|
109
116
|
if ( s < 0 ) {
|
|
110
|
-
k =
|
|
111
|
-
|
|
117
|
+
k = index / s; // truncates
|
|
118
|
+
index -= k * s;
|
|
112
119
|
k += shape[ i ] - 1;
|
|
113
120
|
} else {
|
|
114
|
-
k =
|
|
115
|
-
|
|
121
|
+
k = index / s; // cppcheck-suppress zerodivcond // truncates
|
|
122
|
+
index -= k * s;
|
|
116
123
|
}
|
|
117
124
|
ind += k * llabs( s );
|
|
118
125
|
}
|
package/CITATION.cff
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
cff-version: 1.2.0
|
|
2
|
-
title: stdlib
|
|
3
|
-
message: >-
|
|
4
|
-
If you use this software, please cite it using the
|
|
5
|
-
metadata from this file.
|
|
6
|
-
|
|
7
|
-
type: software
|
|
8
|
-
|
|
9
|
-
authors:
|
|
10
|
-
- name: The Stdlib Authors
|
|
11
|
-
url: https://github.com/stdlib-js/stdlib/graphs/contributors
|
|
12
|
-
|
|
13
|
-
repository-code: https://github.com/stdlib-js/stdlib
|
|
14
|
-
url: https://stdlib.io
|
|
15
|
-
|
|
16
|
-
abstract: |
|
|
17
|
-
Standard library for JavaScript and Node.js.
|
|
18
|
-
|
|
19
|
-
keywords:
|
|
20
|
-
- JavaScript
|
|
21
|
-
- Node.js
|
|
22
|
-
- TypeScript
|
|
23
|
-
- standard library
|
|
24
|
-
- scientific computing
|
|
25
|
-
- numerical computing
|
|
26
|
-
- statistical computing
|
|
27
|
-
|
|
28
|
-
license: Apache-2.0 AND BSL-1.0
|
|
29
|
-
|
|
30
|
-
date-released: 2016
|