array-kit 0.2.11 → 0.2.12

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.
Files changed (2) hide show
  1. package/lib/NDArray.js +44 -33
  2. package/package.json +1 -1
package/lib/NDArray.js CHANGED
@@ -86,6 +86,9 @@ function NDArray( dataOrConstructor , sizes , params ) {
86
86
  // Define the layout of the array in memory, strides are products of all faster dimensions
87
87
  this.strides = new Array( dimensions ) ;
88
88
 
89
+ // Use to speed up the whole array iteration
90
+ this.backStrides = new Array( dimensions ) ;
91
+
89
92
  // This is the index of the first logical element in the physical data storage
90
93
  this.offset = 0 ;
91
94
 
@@ -203,6 +206,7 @@ NDArray.prototype._initMinsMaxsFromSizes = function( sizes ) {
203
206
  NDArray.prototype._initStridesUsingSizes = function( stride = 1 ) {
204
207
  for ( const d of this.order ) {
205
208
  this.strides[ d ] = stride ;
209
+ this.backStrides[ d ] = ( 1 - this.sizes[ d ] ) * stride ;
206
210
  stride *= this.sizes[ d ] ;
207
211
  }
208
212
  } ;
@@ -283,6 +287,7 @@ NDArray.prototype.flip = function( dimension ) {
283
287
  }
284
288
 
285
289
  this.strides[ dimension ] *= - 1 ;
290
+ this.backStrides[ dimension ] *= - 1 ;
286
291
  this._initOffsetEndFromStartSizesStrides() ;
287
292
 
288
293
  return this ;
@@ -290,7 +295,11 @@ NDArray.prototype.flip = function( dimension ) {
290
295
 
291
296
  // Flip coordinates along all dimensions at once, but preserving min and max
292
297
  NDArray.prototype.flipAll = function() {
293
- for ( let d = 0 ; d < this.dimensions ; d ++ ) { this.strides[ d ] *= - 1 ; }
298
+ for ( let d = 0 ; d < this.dimensions ; d ++ ) {
299
+ this.strides[ d ] *= - 1 ;
300
+ this.backStrides[ d ] *= - 1 ;
301
+ }
302
+
294
303
  this._initOffsetEndFromStartSizesStrides() ;
295
304
  return this ;
296
305
  } ;
@@ -309,6 +318,7 @@ NDArray.prototype._cloneGeometry = function() {
309
318
  Object.assign( newNDArray.mins , this.mins ) ;
310
319
  Object.assign( newNDArray.maxs , this.maxs ) ;
311
320
  Object.assign( newNDArray.strides , this.strides ) ;
321
+ Object.assign( newNDArray.backStrides , this.backStrides ) ;
312
322
 
313
323
  // Start at index=0 in the physical storage
314
324
  newNDArray.offset = this.offset - this.dataStart ;
@@ -350,7 +360,6 @@ NDArray.prototype._newStorageOfTheSameKind = function( size = this.dataEnd - thi
350
360
  // index = coords[ 0 ] * strides[ 0 ] + coords[ 1 ] * strides[ 1 ] + ... + coords[ n ] * strides[ n ]
351
361
  NDArray.prototype._getIndex = function( coords ) {
352
362
  let index = this.offset ;
353
- //for ( const d of this.order ) { index += ( coords[ d ] - this.mins[ d ] ) * this.strides[ d ] ; }
354
363
  for ( let d = 0 ; d < this.dimensions ; d ++ ) { index += ( coords[ d ] - this.mins[ d ] ) * this.strides[ d ] ; }
355
364
  return index ;
356
365
  } ;
@@ -537,31 +546,7 @@ NDArray.prototype._getStrideStartAndVectorDimension = function( coords ) {
537
546
  */
538
547
  NDArray.prototype.forEach =
539
548
  NDArray.prototype._forEach = function( callback ) {
540
- // Usually =1, but later we could group neighbours (e.g. for RGBA images => stride0 = 4)
541
- const stride0 = this.strides[ this.order[ 0 ] ] ;
542
- const coords = Array.from( this.mins ) ;
543
- let index = this.dataStart ;
544
-
545
- for ( ;; ) {
546
- callback( this.data[ index ] , coords , index , this ) ;
547
-
548
- index += stride0 ;
549
- if ( index >= this.dataEnd ) { return ; }
550
-
551
- for ( let i = 0 ; i < this.dimensions ; i ++ ) {
552
- const d = this.order[ i ] ;
553
- coords[ d ] ++ ;
554
- if (
555
- i < this.dimensions - 1
556
- && coords[ d ] - this.mins[ d ] >= this.strides[ this.order[ i + 1 ] ]
557
- ) {
558
- coords[ d ] = this.mins[ d ] ;
559
- }
560
- else {
561
- break ;
562
- }
563
- }
564
- }
549
+ return this._forEachInRegion( this.mins , this.maxs , callback , true ) ;
565
550
  } ;
566
551
 
567
552
 
@@ -597,10 +582,19 @@ NDArray.prototype._forEachInRegionCheck = function( mins , maxs , callback ) {
597
582
  this._forEachInRegion( mins , maxs , callback ) ;
598
583
  } ;
599
584
 
600
- NDArray.prototype._forEachInRegion = function( mins , maxs , callback ) {
601
- const backStrides = mins.map( ( min , d ) => ( min - maxs[ d ] ) * this.strides[ d ] ) ;
585
+ // wholeArray=true when coming from ._forEach()
586
+ NDArray.prototype._forEachInRegion = function( mins , maxs , callback , wholeArray = false ) {
587
+ let backStrides , index ;
602
588
  const coords = Array.from( mins ) ;
603
- let index = this._getIndex( coords ) ;
589
+
590
+ if ( wholeArray ) {
591
+ backStrides = this.backStrides ;
592
+ index = this.offset ;
593
+ }
594
+ else {
595
+ backStrides = mins.map( ( min , d ) => ( min - maxs[ d ] ) * this.strides[ d ] ) ;
596
+ index = this._getIndex( coords ) ;
597
+ }
604
598
 
605
599
  for ( ;; ) {
606
600
  callback( this.data[ index ] , coords , index , this ) ;
@@ -677,6 +671,13 @@ NDArray.prototype._forEachVectorInRegion = function( strideStartCoords , maxs ,
677
671
 
678
672
 
679
673
 
674
+ // Return a Generator
675
+ // .each()
676
+ NDArray.prototype.each =
677
+ NDArray.prototype._each = function() {
678
+ return this._eachInRegion( this.mins , this.maxs , true ) ;
679
+ } ;
680
+
680
681
  // Return a Generator
681
682
  // .eachInRegion( region )
682
683
  // .eachInRegion( mins , maxs )
@@ -696,10 +697,20 @@ NDArray.prototype._eachInRegionCheck = function( mins , maxs ) {
696
697
 
697
698
  // Generator
698
699
  // The yielded entry { coords , index , value } should be cloned, as well as entry.coords, if userland want to modify it
699
- NDArray.prototype._eachInRegion = function*( mins , maxs ) {
700
- const backStrides = mins.map( ( min , d ) => ( min - maxs[ d ] ) * this.strides[ d ] ) ;
700
+ // wholeArray=true when coming from ._each()
701
+ NDArray.prototype._eachInRegion = function*( mins , maxs , wholeArray = false ) {
702
+ let backStrides , index ;
701
703
  const coords = Array.from( mins ) ;
702
- let index = this._getIndex( coords ) ;
704
+
705
+ if ( wholeArray ) {
706
+ backStrides = this.backStrides ;
707
+ index = this.offset ;
708
+ }
709
+ else {
710
+ backStrides = mins.map( ( min , d ) => ( min - maxs[ d ] ) * this.strides[ d ] ) ;
711
+ index = this._getIndex( coords ) ;
712
+ }
713
+
703
714
  const entry = { coords , index , value: undefined } ;
704
715
 
705
716
  for ( ;; ) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "array-kit",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "description": "An array manipulation toolbox.",
5
5
  "main": "lib/array-kit.js",
6
6
  "directories": {