Skip to content

Commit 5c9d067

Browse files
committed
feat: add tests and update docs for normalize index mode support
1 parent a7d2960 commit 5c9d067

21 files changed

+3532
-9
lines changed

lib/node_modules/@stdlib/ndarray/ctor/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ The constructor accepts the following `options`:
7979
The constructor supports the following `modes`:
8080

8181
- **throw**: specifies that an `ndarray` instance should throw an error when an index exceeds array dimensions.
82+
- **normalize**: specifies that an `ndarray` instance should normalize negative indices and throw an error when an index exceeds array dimensions.
8283
- **wrap**: specifies that an `ndarray` instance should wrap around an index exceeding array dimensions using modulo arithmetic.
8384
- **clamp**: specifies that an `ndarray` instance should set an index exceeding array dimensions to either `0` (minimum index) or the maximum index.
8485

lib/node_modules/@stdlib/ndarray/ctor/benchmark/benchmark.get.js

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,45 @@ bench( pkg+'::1d:get:mode=clamp', function benchmark( b ) {
139139
b.end();
140140
});
141141

142+
bench( pkg+'::1d:get:mode=normalize', function benchmark( b ) {
143+
var strides;
144+
var buffer;
145+
var offset;
146+
var shape;
147+
var order;
148+
var opts;
149+
var out;
150+
var v;
151+
var i;
152+
153+
opts = {
154+
'mode': 'normalize'
155+
};
156+
157+
buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
158+
shape = [ 6 ];
159+
strides = [ 1 ];
160+
offset = 0;
161+
order = 'row-major';
162+
163+
out = ndarray( 'generic', buffer, shape, strides, offset, order, opts );
164+
165+
b.tic();
166+
for ( i = 0; i < b.iterations; i++ ) {
167+
buffer[ 1 ] = i;
168+
v = out.get( (i%12)-6 );
169+
if ( v !== v ) {
170+
b.fail( 'should not return NaN' );
171+
}
172+
}
173+
b.toc();
174+
if ( v !== v ) {
175+
b.fail( 'should not return NaN' );
176+
}
177+
b.pass( 'benchmark finished' );
178+
b.end();
179+
});
180+
142181
bench( pkg+'::2d:get', function benchmark( b ) {
143182
var strides;
144183
var buffer;
@@ -251,6 +290,45 @@ bench( pkg+'::2d:get:mode=clamp', function benchmark( b ) {
251290
b.end();
252291
});
253292

293+
bench( pkg+'::2d:get:mode=normalize', function benchmark( b ) {
294+
var strides;
295+
var buffer;
296+
var offset;
297+
var shape;
298+
var order;
299+
var opts;
300+
var out;
301+
var v;
302+
var i;
303+
304+
opts = {
305+
'mode': 'normalize'
306+
};
307+
308+
buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
309+
shape = [ 3, 2 ];
310+
strides = [ 2, 1 ];
311+
offset = 0;
312+
order = 'row-major';
313+
314+
out = ndarray( 'generic', buffer, shape, strides, offset, order, opts );
315+
316+
b.tic();
317+
for ( i = 0; i < b.iterations; i++ ) {
318+
buffer[ 1 ] = i;
319+
v = out.get( (i%6)-3, 1 );
320+
if ( v !== v ) {
321+
b.fail( 'should not return NaN' );
322+
}
323+
}
324+
b.toc();
325+
if ( v !== v ) {
326+
b.fail( 'should not return NaN' );
327+
}
328+
b.pass( 'benchmark finished' );
329+
b.end();
330+
});
331+
254332
bench( pkg+'::3d:get', function benchmark( b ) {
255333
var strides;
256334
var buffer;
@@ -363,6 +441,45 @@ bench( pkg+'::3d:get:mode=clamp', function benchmark( b ) {
363441
b.end();
364442
});
365443

444+
bench( pkg+'::3d:get:mode=normalize', function benchmark( b ) {
445+
var strides;
446+
var buffer;
447+
var offset;
448+
var shape;
449+
var order;
450+
var opts;
451+
var out;
452+
var v;
453+
var i;
454+
455+
opts = {
456+
'mode': 'normalize'
457+
};
458+
459+
buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
460+
shape = [ 1, 3, 2 ];
461+
strides = [ 6, 2, 1 ];
462+
offset = 0;
463+
order = 'row-major';
464+
465+
out = ndarray( 'generic', buffer, shape, strides, offset, order, opts );
466+
467+
b.tic();
468+
for ( i = 0; i < b.iterations; i++ ) {
469+
buffer[ 1 ] = i;
470+
v = out.get( 0, (i%6)-3, 1 );
471+
if ( v !== v ) {
472+
b.fail( 'should not return NaN' );
473+
}
474+
}
475+
b.toc();
476+
if ( v !== v ) {
477+
b.fail( 'should not return NaN' );
478+
}
479+
b.pass( 'benchmark finished' );
480+
b.end();
481+
});
482+
366483
bench( pkg+'::4d:get', function benchmark( b ) {
367484
var strides;
368485
var buffer;
@@ -475,6 +592,45 @@ bench( pkg+'::4d:get:mode=clamp', function benchmark( b ) {
475592
b.end();
476593
});
477594

595+
bench( pkg+'::4d:get:mode=normalize', function benchmark( b ) {
596+
var strides;
597+
var buffer;
598+
var offset;
599+
var shape;
600+
var order;
601+
var opts;
602+
var out;
603+
var v;
604+
var i;
605+
606+
opts = {
607+
'mode': 'normalize'
608+
};
609+
610+
buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
611+
shape = [ 1, 1, 3, 2 ];
612+
strides = [ 6, 6, 2, 1 ];
613+
offset = 0;
614+
order = 'row-major';
615+
616+
out = ndarray( 'generic', buffer, shape, strides, offset, order, opts );
617+
618+
b.tic();
619+
for ( i = 0; i < b.iterations; i++ ) {
620+
buffer[ 1 ] = i;
621+
v = out.get( 0, 0, (i%6)-3, 1 );
622+
if ( v !== v ) {
623+
b.fail( 'should not return NaN' );
624+
}
625+
}
626+
b.toc();
627+
if ( v !== v ) {
628+
b.fail( 'should not return NaN' );
629+
}
630+
b.pass( 'benchmark finished' );
631+
b.end();
632+
});
633+
478634
bench( pkg+'::5d:get', function benchmark( b ) {
479635
var strides;
480636
var buffer;
@@ -586,3 +742,42 @@ bench( pkg+'::5d:get:mode=clamp', function benchmark( b ) {
586742
b.pass( 'benchmark finished' );
587743
b.end();
588744
});
745+
746+
bench( pkg+'::5d:get:mode=normalize', function benchmark( b ) {
747+
var strides;
748+
var buffer;
749+
var offset;
750+
var shape;
751+
var order;
752+
var opts;
753+
var out;
754+
var v;
755+
var i;
756+
757+
opts = {
758+
'mode': 'normalize'
759+
};
760+
761+
buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
762+
shape = [ 1, 1, 1, 3, 2 ];
763+
strides = [ 6, 6, 6, 2, 1 ];
764+
offset = 0;
765+
order = 'row-major';
766+
767+
out = ndarray( 'generic', buffer, shape, strides, offset, order, opts );
768+
769+
b.tic();
770+
for ( i = 0; i < b.iterations; i++ ) {
771+
buffer[ 1 ] = i;
772+
v = out.get( 0, 0, 0, (i%6)-3, 1 );
773+
if ( v !== v ) {
774+
b.fail( 'should not return NaN' );
775+
}
776+
}
777+
b.toc();
778+
if ( v !== v ) {
779+
b.fail( 'should not return NaN' );
780+
}
781+
b.pass( 'benchmark finished' );
782+
b.end();
783+
});

lib/node_modules/@stdlib/ndarray/ctor/benchmark/benchmark.iget.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,45 @@ bench( pkg+'::1d:iget:mode=clamp', function benchmark( b ) {
139139
b.end();
140140
});
141141

142+
bench( pkg+'::1d:iget:mode=normalize', function benchmark( b ) {
143+
var strides;
144+
var buffer;
145+
var offset;
146+
var shape;
147+
var order;
148+
var opts;
149+
var out;
150+
var v;
151+
var i;
152+
153+
opts = {
154+
'mode': 'normalize'
155+
};
156+
157+
buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
158+
shape = [ 6 ];
159+
strides = [ 1 ];
160+
offset = 0;
161+
order = 'row-major';
162+
163+
out = ndarray( 'generic', buffer, shape, strides, offset, order, opts );
164+
165+
b.tic();
166+
for ( i = 0; i < b.iterations; i++ ) {
167+
buffer[ 1 ] = i;
168+
v = out.iget( (i%12)-6 );
169+
if ( v !== v ) {
170+
b.fail( 'should not return NaN' );
171+
}
172+
}
173+
b.toc();
174+
if ( v !== v ) {
175+
b.fail( 'should not return NaN' );
176+
}
177+
b.pass( 'benchmark finished' );
178+
b.end();
179+
});
180+
142181
bench( pkg+'::2d,all_positive_strides:iget:order=row-major', function benchmark( b ) {
143182
var strides;
144183
var buffer;

lib/node_modules/@stdlib/ndarray/ctor/benchmark/benchmark.iset.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,48 @@ bench( pkg+'::1d:iset:mode=clamp', function benchmark( b ) {
147147
b.end();
148148
});
149149

150+
bench( pkg+'::1d:iset:mode=normalize', function benchmark( b ) {
151+
var strides;
152+
var buffer;
153+
var offset;
154+
var shape;
155+
var order;
156+
var opts;
157+
var out;
158+
var tmp;
159+
var v;
160+
var i;
161+
var j;
162+
163+
opts = {
164+
'mode': 'normalize'
165+
};
166+
167+
buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
168+
shape = [ 6 ];
169+
strides = [ 1 ];
170+
offset = 0;
171+
order = 'row-major';
172+
173+
out = ndarray( 'generic', buffer, shape, strides, offset, order, opts );
174+
175+
b.tic();
176+
for ( i = 0; i < b.iterations; i++ ) {
177+
v = i;
178+
j = (i%12) - 6;
179+
tmp = out.iset( j, v );
180+
if ( typeof tmp !== 'object' ) {
181+
b.fail( 'should return an object' );
182+
}
183+
}
184+
b.toc();
185+
if ( out.iget( j ) !== v ) {
186+
b.fail( 'should set value' );
187+
}
188+
b.pass( 'benchmark finished' );
189+
b.end();
190+
});
191+
150192
bench( pkg+'::2d,all_positive_strides:iset:order=row-major', function benchmark( b ) {
151193
var strides;
152194
var buffer;

0 commit comments

Comments
 (0)