Skip to content

Commit 28adeac

Browse files
committed
Refactor to handle edge case where dimension is zero
1 parent 0a60311 commit 28adeac

File tree

10 files changed

+152
-4
lines changed

10 files changed

+152
-4
lines changed

lib/node_modules/@stdlib/ndarray/base/max-view-buffer-index/lib/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ function maxViewBufferIndex( shape, strides, offset ) {
6868
ndims = shape.length;
6969
idx = offset;
7070
for ( i = 0; i < ndims; i++ ) {
71+
if ( shape[ i ] === 0 ) {
72+
return offset;
73+
}
7174
if ( strides[ i ] > 0 ) {
72-
idx += strides[ i ] * (shape[ i ] - 1);
75+
idx += strides[ i ] * ( shape[ i ] - 1 );
7376
}
7477
}
7578
return idx;

lib/node_modules/@stdlib/ndarray/base/max-view-buffer-index/src/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ int64_t stdlib_ndarray_max_view_buffer_index( int64_t ndims, int64_t *shape, int
4646

4747
idx = offset;
4848
for ( i = 0; i < ndims; i++ ) {
49+
if ( shape[ i ] == 0 ) {
50+
return offset;
51+
}
4952
if ( strides[ i ] > 0 ) {
50-
idx += strides[ i ] * (shape[ i ] - 1);
53+
idx += strides[ i ] * ( shape[ i ] - 1 );
5154
}
5255
}
5356
return idx;

lib/node_modules/@stdlib/ndarray/base/max-view-buffer-index/test/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,30 @@ tape( 'the function computes the maximum linear index in an underlying array buf
126126

127127
t.end();
128128
});
129+
130+
tape( 'the function returns the provided offset when one or more dimensions is zero', function test( t ) {
131+
var strides;
132+
var offset;
133+
var shape;
134+
var idx;
135+
136+
shape = [ 3, 0 ];
137+
strides = [ 2, 1 ];
138+
offset = 10;
139+
idx = maxViewBufferIndex( shape, strides, offset );
140+
t.strictEqual( idx, 10, 'returns expected value' );
141+
142+
shape = [ 0, 2 ];
143+
strides = [ -2, 1 ];
144+
offset = 14;
145+
idx = maxViewBufferIndex( shape, strides, offset );
146+
t.strictEqual( idx, 14, 'returns expected value' );
147+
148+
shape = [ 0, 0 ];
149+
strides = [ 2, -1 ];
150+
offset = 11;
151+
idx = maxViewBufferIndex( shape, strides, offset );
152+
t.strictEqual( idx, 11, 'returns expected value' );
153+
154+
t.end();
155+
});

lib/node_modules/@stdlib/ndarray/base/min-view-buffer-index/lib/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ function minViewBufferIndex( shape, strides, offset ) {
6868
ndims = shape.length;
6969
idx = offset;
7070
for ( i = 0; i < ndims; i++ ) {
71+
if ( shape[ i ] === 0 ) {
72+
return offset;
73+
}
7174
if ( strides[ i ] < 0 ) {
72-
idx += strides[ i ] * (shape[ i ] - 1); // decrements the index
75+
idx += strides[ i ] * ( shape[ i ] - 1 ); // decrements the index
7376
}
7477
}
7578
return idx;

lib/node_modules/@stdlib/ndarray/base/min-view-buffer-index/src/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ int64_t stdlib_ndarray_min_view_buffer_index( int64_t ndims, int64_t *shape, int
4646

4747
idx = offset;
4848
for ( i = 0; i < ndims; i++ ) {
49+
if ( shape[ i ] == 0 ) {
50+
return offset;
51+
}
4952
if ( strides[ i ] < 0 ) {
50-
idx += strides[ i ] * (shape[ i ] - 1); // decrements the index
53+
idx += strides[ i ] * ( shape[ i ] - 1 ); // decrements the index
5154
}
5255
}
5356
return idx;

lib/node_modules/@stdlib/ndarray/base/min-view-buffer-index/test/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,30 @@ tape( 'the function computes the minimum linear index in an underlying array buf
126126

127127
t.end();
128128
});
129+
130+
tape( 'the function returns the provided offset when one or more dimensions is zero', function test( t ) {
131+
var strides;
132+
var offset;
133+
var shape;
134+
var idx;
135+
136+
shape = [ 3, 0 ];
137+
strides = [ 2, 1 ];
138+
offset = 10;
139+
idx = minViewBufferIndex( shape, strides, offset );
140+
t.strictEqual( idx, 10, 'returns expected value' );
141+
142+
shape = [ 0, 2 ];
143+
strides = [ -2, 1 ];
144+
offset = 14;
145+
idx = minViewBufferIndex( shape, strides, offset );
146+
t.strictEqual( idx, 14, 'returns expected value' );
147+
148+
shape = [ 0, 0 ];
149+
strides = [ 2, -1 ];
150+
offset = 11;
151+
idx = minViewBufferIndex( shape, strides, offset );
152+
t.strictEqual( idx, 11, 'returns expected value' );
153+
154+
t.end();
155+
});

lib/node_modules/@stdlib/ndarray/base/minmax-view-buffer-index/lib/assign.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ function minmaxViewBufferIndex( shape, strides, offset, out ) {
8888
min = offset;
8989
max = offset;
9090
for ( i = 0; i < ndims; i++ ) {
91+
if ( shape[ i ] === 0 ) {
92+
out[ 0 ] = offset;
93+
out[ 1 ] = offset;
94+
return out;
95+
}
9196
s = strides[ i ];
9297
if ( s > 0 ) {
9398
max += s * ( shape[i]-1 );

lib/node_modules/@stdlib/ndarray/base/minmax-view-buffer-index/lib/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ function minmaxViewBufferIndex( shape, strides, offset ) {
7171
min = offset;
7272
max = offset;
7373
for ( i = 0; i < ndims; i++ ) {
74+
if ( shape[ i ] === 0 ) {
75+
return [ offset, offset ];
76+
}
7477
s = strides[ i ];
7578
if ( s > 0 ) {
7679
max += s * ( shape[i]-1 );

lib/node_modules/@stdlib/ndarray/base/minmax-view-buffer-index/src/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ int8_t stdlib_ndarray_minmax_view_buffer_index( int64_t ndims, int64_t *shape, i
5656
min = offset;
5757
max = offset;
5858
for ( i = 0; i < ndims; i++ ) {
59+
if ( shape[ i ] == 0 ) {
60+
out[ 0 ] = offset;
61+
out[ 1 ] = offset;
62+
return 0;
63+
}
5964
s = strides[ i ];
6065
if ( s > 0 ) {
6166
max += s * ( shape[i]-1 );

lib/node_modules/@stdlib/ndarray/base/minmax-view-buffer-index/test/test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,36 @@ tape( 'the function computes the minimum and maximum linear indices in an underl
152152
t.end();
153153
});
154154

155+
tape( 'the function returns the provided offset when one or more dimensions is zero', function test( t ) {
156+
var strides;
157+
var offset;
158+
var shape;
159+
var idx;
160+
161+
shape = [ 3, 0 ];
162+
strides = [ 2, 1 ];
163+
offset = 10;
164+
idx = minmaxViewBufferIndex( shape, strides, offset );
165+
t.strictEqual( idx[ 0 ], 10, 'returns expected value' );
166+
t.strictEqual( idx[ 1 ], 10, 'returns expected value' );
167+
168+
shape = [ 0, 2 ];
169+
strides = [ -2, 1 ];
170+
offset = 14;
171+
idx = minmaxViewBufferIndex( shape, strides, offset );
172+
t.strictEqual( idx[ 0 ], 14, 'returns expected value' );
173+
t.strictEqual( idx[ 1 ], 14, 'returns expected value' );
174+
175+
shape = [ 0, 0 ];
176+
strides = [ 2, -1 ];
177+
offset = 11;
178+
idx = minmaxViewBufferIndex( shape, strides, offset );
179+
t.strictEqual( idx[ 0 ], 11, 'returns expected value' );
180+
t.strictEqual( idx[ 1 ], 11, 'returns expected value' );
181+
182+
t.end();
183+
});
184+
155185
tape( 'attached to the main function is a method which supports providing an output object', function test( t ) {
156186
var strides;
157187
var offset;
@@ -247,6 +277,45 @@ tape( 'attached to the main function is a method which supports providing an out
247277
t.end();
248278
});
249279

280+
tape( 'the function returns the provided offset when one or more dimensions is zero (assign)', function test( t ) {
281+
var strides;
282+
var offset;
283+
var shape;
284+
var idx;
285+
var out;
286+
287+
out = [ 0, 0 ];
288+
shape = [ 3, 0 ];
289+
strides = [ 2, 1 ];
290+
offset = 10;
291+
idx = minmaxViewBufferIndex.assign( shape, strides, offset, out );
292+
t.strictEqual( idx, out, 'returns expected value' );
293+
t.strictEqual( idx[ 0 ], 10, 'returns expected value' );
294+
t.strictEqual( idx[ 1 ], 10, 'returns expected value' );
295+
296+
out = [ 0, 0 ];
297+
shape = [ 3, 0 ];
298+
shape = [ 0, 2 ];
299+
strides = [ -2, 1 ];
300+
offset = 14;
301+
idx = minmaxViewBufferIndex.assign( shape, strides, offset, out );
302+
t.strictEqual( idx, out, 'returns expected value' );
303+
t.strictEqual( idx[ 0 ], 14, 'returns expected value' );
304+
t.strictEqual( idx[ 1 ], 14, 'returns expected value' );
305+
306+
out = [ 0, 0 ];
307+
shape = [ 3, 0 ];
308+
shape = [ 0, 0 ];
309+
strides = [ 2, -1 ];
310+
offset = 11;
311+
idx = minmaxViewBufferIndex.assign( shape, strides, offset, out );
312+
t.strictEqual( idx, out, 'returns expected value' );
313+
t.strictEqual( idx[ 0 ], 11, 'returns expected value' );
314+
t.strictEqual( idx[ 1 ], 11, 'returns expected value' );
315+
316+
t.end();
317+
});
318+
250319
tape( 'the function computes the minimum and maximum linear indices in an underlying array buffer which are accessible to an array view (shape[k]=1)', function test( t ) {
251320
var strides;
252321
var offset;

0 commit comments

Comments
 (0)