Skip to content

Commit

Permalink
[UPDATE] support negative indices for iget and iset.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Jun 1, 2015
1 parent b7d706b commit ca17190
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ __Note__: out-of-bounds row and column indices will silently fail.
<a name="matrix-iset"></a>
#### mat.iset( index, value )

Sets a `Matrix` element located at a specified [`index`](#linear-indexing).
Sets a `Matrix` element located at a specified [`index`](#linear-indexing). If `idx < 0`, the index refers to a position relative to the `Matrix` length, where `idx = -1` corresponds to the last element.

``` javascript
mat.iset( 7, 25 );
Expand All @@ -226,6 +226,15 @@ mat.iset( 7, 25 );
6 25
8 9 ]
*/

mat.iset( -3, 20 );
/*
[ 0 1
2 3
4 5
6 20
8 9 ]
*/
```

__Note__: out-of-bounds indices will silently fail.
Expand Down Expand Up @@ -344,11 +353,14 @@ __Note__: out-of-bounds row and column indices will return a value of `undefined
<a name="matrix-iget"></a>
#### mat.iget( index )

Returns a `Matrix` element located at a specified [`index`](#linear-indexing).
Returns a `Matrix` element located at a specified [`index`](#linear-indexing). If `idx < 0`, the index refers to a position relative to the `Matrix` length, where `idx = -1` corresponds to the last element.

``` javascript
var value = mat.iget( 7 );
// returns 7

value = mat.iget( -3 );
// returns 7
```

__Note__: out-of-bounds indices will return a value of `undefined`.
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TODO
5.
6.
7.
8. should we allow negative indices for `iset`, `iget`, etc?
8.
9.
10. pretty print a matrix
- separate module? => `compute-print-matrix`
Expand Down
56 changes: 56 additions & 0 deletions benchmark/b.iget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

// MODULES //

var matrix = require( './../lib' );


// VARIABLES //

var start,
stop,
nRows,
nCols,
res,
len,
m,
v,
i, j;


// --------------------------------------
// WARM-UP

len = 1e6;
for ( i = 0; i < len; i++ ) {
i = i;
}


// --------------------------------------
// BENCHMARK

len = 1e6;
nRows = 128;
nCols = 128;

res = new Array( 1 );

m = matrix( [nRows,nCols] );
j = 64*m.strides[0] + 1*m.strides[1];

start = process.hrtime();
for ( i = 0; i < len; i++ ) {
v = m.iget( j );
}
stop = process.hrtime( start );

res[ 0 ] = stop[ 0 ] + stop[ 1 ]*1e-9;


// --------------------------------------
// RESULTS

console.log( 'iget:\t%d ops/sec', Math.floor( len/res[ 0 ] ) );
console.log( '\n' );

3 changes: 3 additions & 0 deletions lib/iget.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
function iget( idx ) {
/*jshint validthis:true */
if ( idx < 0 ) {
idx += this.length;
}
return this.data[ idx ];
} // end FUNCTION iget()

Expand Down
3 changes: 3 additions & 0 deletions lib/iset.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/
function iset( idx, v ) {
/* jshint validthis: true */
if ( idx < 0 ) {
idx += this.length;
}
this.data[ idx ] = v;
return this;
} // end FUNCTION iset()
Expand Down

0 comments on commit ca17190

Please sign in to comment.