Skip to content

Commit 42bfbed

Browse files
committed
Add Typescript definitions
1 parent 6ced125 commit 42bfbed

6 files changed

Lines changed: 488 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
/// <reference types="node"/>
23+
24+
import { ArrayLike } from '@stdlib/types/array';
25+
import { Mode, Order } from '@stdlib/types/ndarray';
26+
27+
/**
28+
* Converts a linear index in an underlying data buffer to a linear index in an array view.
29+
*
30+
* @param shape - array shape
31+
* @param strides - stride array
32+
* @param offset - location of the first indexed value **based** on the stride array
33+
* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
34+
* @param idx - linear index in an underlying data buffer
35+
* @param mode - specifies how to handle a linear index which exceeds array dimensions
36+
* @throws linear index must not exceed array dimensions
37+
* @returns linear index in an array view
38+
*
39+
* @example
40+
* var shape = [ 3, 3 ];
41+
* var strides = [ -3, 1 ];
42+
* var offset = 6;
43+
* var order = 'row-major';
44+
* var mode = 'throw';
45+
*
46+
* var ind = bind2vind( shape, strides, offset, order, 7, mode );
47+
* // returns 1
48+
*/
49+
declare function bind2vind( shape: ArrayLike<number>, strides: ArrayLike<number>, offset: number, order: Order, idx: number, mode: Mode ): number; // tslint-disable-line max-line-length
50+
51+
52+
// EXPORTS //
53+
54+
export = bind2vind;
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import bind2vind = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
const shape = [ 3, 3, 3 ];
27+
const strides = [ 9, 6, 1 ];
28+
const offset = 0;
29+
const order = 'row-major';
30+
const idx = 17;
31+
const mode = 'throw';
32+
bind2vind( shape, strides, offset, order, idx, mode ); // $ExpectType number
33+
}
34+
35+
// The function does not compile if provided a first argument which is not an array-like object containing numbers...
36+
{
37+
const strides = [ 9, 6, 1 ];
38+
const offset = 0;
39+
const order = 'row-major';
40+
const idx = 17;
41+
const mode = 'throw';
42+
bind2vind( true, strides, offset, order, idx, mode ); // $ExpectError
43+
bind2vind( false, strides, offset, order, idx, mode ); // $ExpectError
44+
bind2vind( null, strides, offset, order, idx, mode ); // $ExpectError
45+
bind2vind( undefined, strides, offset, order, idx, mode ); // $ExpectError
46+
bind2vind( '5', strides, offset, order, idx, mode ); // $ExpectError
47+
bind2vind( [ '1', '2' ], strides, offset, order, idx, mode ); // $ExpectError
48+
bind2vind( {}, strides, offset, order, idx, mode ); // $ExpectError
49+
bind2vind( ( x: number ): number => x, strides, offset, order, idx, mode ); // $ExpectError
50+
}
51+
52+
// The function does not compile if provided a second argument which is not an array-like object containing numbers...
53+
{
54+
const shape = [ 3, 3, 3 ];
55+
const offset = 0;
56+
const order = 'row-major';
57+
const idx = 17;
58+
const mode = 'throw';
59+
bind2vind( shape, true, offset, order, idx, mode ); // $ExpectError
60+
bind2vind( shape, false, offset, order, idx, mode ); // $ExpectError
61+
bind2vind( shape, null, offset, order, idx, mode ); // $ExpectError
62+
bind2vind( shape, undefined, offset, order, idx, mode ); // $ExpectError
63+
bind2vind( shape, '5', offset, order, idx, mode ); // $ExpectError
64+
bind2vind( shape, [ '1', '2' ], offset, order, idx, mode ); // $ExpectError
65+
bind2vind( shape, {}, offset, order, idx, mode ); // $ExpectError
66+
bind2vind( shape, ( x: number ): number => x, offset, order, idx, mode ); // $ExpectError
67+
}
68+
69+
// The function does not compile if provided a third argument which is not a number...
70+
{
71+
const shape = [ 3, 3, 3 ];
72+
const strides = [ 9, 6, 1 ];
73+
const order = 'row-major';
74+
const idx = 17;
75+
const mode = 'throw';
76+
bind2vind( shape, strides, true, order, idx, mode ); // $ExpectError
77+
bind2vind( shape, strides, false, order, idx, mode ); // $ExpectError
78+
bind2vind( shape, strides, null, order, idx, mode ); // $ExpectError
79+
bind2vind( shape, strides, undefined, order, idx, mode ); // $ExpectError
80+
bind2vind( shape, strides, '5', order, idx, mode ); // $ExpectError
81+
bind2vind( shape, strides, [ '1', '2' ], order, idx, mode ); // $ExpectError
82+
bind2vind( shape, strides, {}, order, idx, mode ); // $ExpectError
83+
bind2vind( shape, strides, ( x: number ): number => x, order, idx, mode ); // $ExpectError
84+
}
85+
86+
// The function does not compile if provided a fourth argument which is not a known array order...
87+
{
88+
const shape = [ 3, 3, 3 ];
89+
const strides = [ 9, 6, 1 ];
90+
const offset = 0;
91+
const idx = 17;
92+
const mode = 'throw';
93+
bind2vind( shape, strides, offset, true, idx, mode ); // $ExpectError
94+
bind2vind( shape, strides, offset, false, idx, mode ); // $ExpectError
95+
bind2vind( shape, strides, offset, null, idx, mode ); // $ExpectError
96+
bind2vind( shape, strides, offset, undefined, idx, mode ); // $ExpectError
97+
bind2vind( shape, strides, offset, '5', idx, mode ); // $ExpectError
98+
bind2vind( shape, strides, offset, [ '1', '2' ], idx, mode ); // $ExpectError
99+
bind2vind( shape, strides, offset, {}, idx, mode ); // $ExpectError
100+
bind2vind( shape, strides, offset, ( x: number ): number => x, idx, mode ); // $ExpectError
101+
}
102+
103+
// The function does not compile if provided a fifth argument which is not a number...
104+
{
105+
const shape = [ 3, 3, 3 ];
106+
const strides = [ 9, 6, 1 ];
107+
const offset = 0;
108+
const order = 'row-major';
109+
const mode = 'throw';
110+
bind2vind( shape, strides, offset, order, true, mode ); // $ExpectError
111+
bind2vind( shape, strides, offset, order, false, mode ); // $ExpectError
112+
bind2vind( shape, strides, offset, order, null, mode ); // $ExpectError
113+
bind2vind( shape, strides, offset, order, undefined, mode ); // $ExpectError
114+
bind2vind( shape, strides, offset, order, '5', mode ); // $ExpectError
115+
bind2vind( shape, strides, offset, order, [ '1', '2' ], mode ); // $ExpectError
116+
bind2vind( shape, strides, offset, order, {}, mode ); // $ExpectError
117+
bind2vind( shape, strides, offset, order, ( x: number ): number => x, mode ); // $ExpectError
118+
}
119+
120+
// The function does not compile if provided a sixth argument which is not a known mode...
121+
{
122+
const shape = [ 3, 3, 3 ];
123+
const strides = [ 9, 6, 1 ];
124+
const offset = 0;
125+
const order = 'row-major';
126+
const idx = 17;
127+
bind2vind( shape, strides, offset, order, idx, true ); // $ExpectError
128+
bind2vind( shape, strides, offset, order, idx, false ); // $ExpectError
129+
bind2vind( shape, strides, offset, order, idx, null ); // $ExpectError
130+
bind2vind( shape, strides, offset, order, idx, undefined ); // $ExpectError
131+
bind2vind( shape, strides, offset, order, idx, '5' ); // $ExpectError
132+
bind2vind( shape, strides, offset, order, idx, [ '1', '2' ] ); // $ExpectError
133+
bind2vind( shape, strides, offset, order, idx, {} ); // $ExpectError
134+
bind2vind( shape, strides, offset, order, idx, ( x: number ): number => x ); // $ExpectError
135+
}
136+
137+
// The function does not compile if provided insufficient arguments...
138+
{
139+
const shape = [ 3, 3, 3 ];
140+
const strides = [ 9, 6, 1 ];
141+
const offset = 0;
142+
const order = 'row-major';
143+
const idx = 17;
144+
bind2vind(); // $ExpectError
145+
bind2vind( shape ); // $ExpectError
146+
bind2vind( shape, strides ); // $ExpectError
147+
bind2vind( shape, strides, offset ); // $ExpectError
148+
bind2vind( shape, strides, offset, order ); // $ExpectError
149+
bind2vind( shape, strides, offset, order, idx ); // $ExpectError
150+
}

lib/node_modules/@stdlib/ndarray/base/bind2vind/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
/// <reference types="node"/>
23+
24+
import { ArrayLike } from '@stdlib/types/array';
25+
import { Mode, Order } from '@stdlib/types/ndarray';
26+
27+
/**
28+
* Converts a linear index to an array of subscripts.
29+
*
30+
* ## Notes
31+
*
32+
* - The function accepts the following "modes":
33+
*
34+
* - `throw`: throws an error when a linear index exceeds array dimensions.
35+
* - `wrap`: wrap around a linear index exceeding array dimensions using modulo arithmetic.
36+
* - `clamp`: set a linear index exceeding array dimensions to either `0` (minimum linear index) or the maximum linear index.
37+
*
38+
* - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function interprets the linear index as an index into the underlying data buffer for the array, thus returning subscripts from the perspective of that buffer. If an `offset` is equal to `0`, the function treats the linear index as an index into an array view, thus returning subscripts from the perspective of that view.
39+
*
40+
* ```text
41+
* Dims: 2x2
42+
* Buffer: [ 1, 2, 3, 4 ]
43+
*
44+
* View = [ a00, a01,
45+
* a10, a11 ]
46+
*
47+
* Strides: 2,1
48+
* Offset: 0
49+
*
50+
* View = [ 1, 2,
51+
* 3, 4 ]
52+
*
53+
* Strides: 2,-1
54+
* Offset: 1
55+
*
56+
* View = [ 2, 1,
57+
* 4, 3 ]
58+
*
59+
* Strides: -2,1
60+
* Offset: 2
61+
*
62+
* View = [ 3, 4,
63+
* 1, 2 ]
64+
*
65+
* Strides: -2,-1
66+
* Offset: 3
67+
*
68+
* View = [ 4, 3,
69+
* 2, 1 ]
70+
* ```
71+
*
72+
* ```javascript
73+
* var shape = [ 2, 2 ];
74+
* var order = 'row-major';
75+
* var strides = [ -2, 1 ];
76+
* var offset = 2;
77+
* var mode = 'throw';
78+
*
79+
* // From the perspective of a view...
80+
* var s = ind2sub( shape, strides, 0, order, 0, mode );
81+
* // returns [ 0, 0 ]
82+
*
83+
* s = ind2sub( shape, strides, 0, order, 1, mode );
84+
* // returns [ 0, 1 ]
85+
*
86+
* s = ind2sub( shape, strides, 0, order, 2, mode );
87+
* // returns [ 1, 0 ]
88+
*
89+
* s = ind2sub( shape, strides, 0, order, 3, mode );
90+
* // returns [ 1, 1 ]
91+
*
92+
* // From the perspective of an underlying buffer...
93+
* s = ind2sub( shape, strides, offset, order, 0, mode );
94+
* // returns [ 1, 0 ]
95+
*
96+
* s = ind2sub( shape, strides, offset, order, 1, mode );
97+
* // returns [ 1, 1 ]
98+
*
99+
* s = ind2sub( shape, strides, offset, order, 2, mode );
100+
* // returns [ 0, 0 ]
101+
*
102+
* s = ind2sub( shape, strides, offset, order, 3, mode );
103+
* // returns [ 0, 1 ]
104+
* ```
105+
*
106+
* In short, from the perspective of a view, view data is always ordered.
107+
*
108+
*
109+
* @param shape - array shape
110+
* @param strides - stride array
111+
* @param offset - location of the first indexed value **based** on the stride array
112+
* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
113+
* @param idx - linear index
114+
* @param mode - specifies how to handle a linear index which exceeds array dimensions
115+
* @throws linear index must not exceed array dimensions
116+
* @returns subscripts
117+
*
118+
* @example
119+
* var shape = [ 3, 3, 3 ];
120+
* var strides = [ 9, 6, 1 ];
121+
* var offset = 0;
122+
* var order = 'row-major';
123+
*
124+
* var s = ind2sub( shape, strides, offset, order, 17, 'throw' );
125+
* // returns [ 1, 2, 2 ]
126+
*/
127+
declare function ind2sub( shape: ArrayLike<number>, strides: ArrayLike<number>, offset: number, order: Order, idx: number, mode: Mode ): Array<number>; // tslint-disable-line max-line-length
128+
129+
130+
// EXPORTS //
131+
132+
export = ind2sub;

0 commit comments

Comments
 (0)