Skip to content

Commit

Permalink
fix #4
Browse files Browse the repository at this point in the history
Decreased the running time of benchmark/benchmark.js from:

$ node --prof benchmark/benchmark.js
number of operations:  100000
cons: 384ms
tail: 544ms
push: 338ms
split: 7090ms
init: 493ms

to:

$ node benchmark/benchmark.js
number of operations:  100000
cons: 96ms
tail: 218ms
push: 75ms
split: 1298ms
init: 178ms
  • Loading branch information
make-github-pseudonymous-again committed Sep 9, 2015
1 parent 1eeb30f commit 295011e
Show file tree
Hide file tree
Showing 20 changed files with 1,305 additions and 1,629 deletions.
1,747 changes: 742 additions & 1,005 deletions js/dist/fingertree.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/fingertree.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/fingertree.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/src/0-core/concatenate/app3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const app3 = function ( A , list , B ) {
function app3 ( A , list , B ) {

A = A.force( ) ;
B = B.force( ) ;
Expand All @@ -20,4 +20,4 @@ const app3 = function ( A , list , B ) {
B.right
) ;

} ;
}
15 changes: 15 additions & 0 deletions js/src/0-core/measure/CachedMeasure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function CachedMeasure ( M ) {
this.M = M ;
}

CachedMeasure.prototype.zero = function ( ) {
return this.M.zero( ) ;
} ;

CachedMeasure.prototype.plus = function ( a , b ) {
return this.M.plus( a , b ) ;
} ;

CachedMeasure.prototype.measure = function ( measured ) {
return measured.measure( ) ;
} ;
23 changes: 0 additions & 23 deletions js/src/0-core/measure/cache.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@

class CachedMeasure {

constructor ( M ) {
this.M = M ;
}

zero ( ) {
return this.M.zero( ) ;
}

plus ( a , b ) {
return this.M.plus( a , b ) ;
}

measure ( measured ) {
return measured.measure( ) ;
}

}

function cache ( M ) {

return M instanceof CachedMeasure ? M : new CachedMeasure( M ) ;

}
10 changes: 2 additions & 8 deletions js/src/0-core/optimizing/empty/1-_EmptyGenerator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
class _EmptyGenerator {
function _EmptyGenerator ( ) { }

next ( ) {

return { done : true } ;

}

}
_EmptyGenerator.prototype.next = function ( ) { return { done : true } ; } ;
13 changes: 4 additions & 9 deletions js/src/0-core/split/Split.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@

class Split {

constructor ( left , middle , right ) {
this.left = left ;
this.middle = middle ;
this.right = right ;
}

function Split ( left , middle , right ) {
this.left = left ;
this.middle = middle ;
this.right = right ;
}
76 changes: 36 additions & 40 deletions js/src/1-digit/1-One.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
class One {

constructor ( a ) {
this.a = a ;
}

*[Symbol.iterator] ( ) {
yield this.a ;
}
function One ( a ) {
this.a = a ;
}

measure ( M ) {
return M.measure( this.a ) ;
}
One.prototype[Symbol.iterator] = function* ( ) {
yield this.a ;
} ;

head ( ) {
return this.a ;
}
One.prototype.measure = function ( M ) {
return M.measure( this.a ) ;
} ;

last ( ) {
return this.a ;
}
One.prototype.head = function ( ) {
return this.a ;
} ;

init ( ) {
throw new Error( "cannot call init on digit One" ) ;
}
One.prototype.last = function ( ) {
return this.a ;
} ;

tail ( ) {
throw new Error( "cannot call tail on digit One" ) ;
}
One.prototype.init = function ( ) {
throw new Error( "cannot call init on digit One" ) ;
} ;

push ( value ) {
return new Two( this.a , value ) ;
}
One.prototype.tail = function ( ) {
throw new Error( "cannot call tail on digit One" ) ;
} ;

cons ( value ) {
return new Two( value , this.a ) ;
}
One.prototype.push = function ( value ) {
return new Two( this.a , value ) ;
} ;

node ( M ) {
throw new Error( "cannot convert One to node" ) ;
}
One.prototype.cons = function ( value ) {
return new Two( value , this.a ) ;
} ;

/**
* It is assumed that p(|this|) is true.
*/
splitDigit ( p , i , M ) {
return new Split( [ ] , this.a , [ ] ) ;
}
One.prototype.node = function ( M ) {
throw new Error( "cannot convert One to node" ) ;
} ;

}
/**
* It is assumed that p(|this|) is true.
*/
One.prototype.splitDigit = function ( p , i , M ) {
return new Split( [ ] , this.a , [ ] ) ;
} ;
102 changes: 49 additions & 53 deletions js/src/1-digit/2-Two.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
class Two {

constructor ( a , b ) {
this.a = a ;
this.b = b ;
}

*[Symbol.iterator] ( ) {
yield this.a ;
yield this.b ;
}

measure ( M ) {
return M.plus( M.measure( this.a ) , M.measure( this.b ) ) ;
}

head ( ) {
return this.a ;
}

last ( ) {
return this.b ;
}

init ( ) {
return new One( this.a ) ;
}

tail ( ) {
return new One( this.b ) ;
}

push ( value ) {
return new Three( this.a , this.b , value ) ;
}

cons ( value ) {
return new Three( value , this.a , this.b ) ;
}

node ( M ) {
throw new Error( "Two should never be converted to Node2 with current implementation" ) ;
}

/**
* It is assumed that p(|this|) is true.
*/
splitDigit ( p , i , M ) {
i = M.plus( i , M.measure( this.a ) ) ;
if ( p( i ) ) return new Split( [ ] , this.a , [ this.b ] ) ;
return new Split( [ this.a ] , this.b , [ ] ) ;
}

function Two ( a , b ) {
this.a = a ;
this.b = b ;
}

Two.prototype[Symbol.iterator] = function* ( ) {
yield this.a ;
yield this.b ;
} ;

Two.prototype.measure = function ( M ) {
return M.plus( M.measure( this.a ) , M.measure( this.b ) ) ;
} ;

Two.prototype.head = function ( ) {
return this.a ;
} ;

Two.prototype.last = function ( ) {
return this.b ;
} ;

Two.prototype.init = function ( ) {
return new One( this.a ) ;
} ;

Two.prototype.tail = function ( ) {
return new One( this.b ) ;
} ;

Two.prototype.push = function ( value ) {
return new Three( this.a , this.b , value ) ;
} ;

Two.prototype.cons = function ( value ) {
return new Three( value , this.a , this.b ) ;
} ;

Two.prototype.node = function ( M ) {
throw new Error( "Two should never be converted to Node2 with current implementation" ) ;
} ;

/**
* It is assumed that p(|this|) is true.
*/
Two.prototype.splitDigit = function ( p , i , M ) {
i = M.plus( i , M.measure( this.a ) ) ;
if ( p( i ) ) return new Split( [ ] , this.a , [ this.b ] ) ;
return new Split( [ this.a ] , this.b , [ ] ) ;
} ;
122 changes: 59 additions & 63 deletions js/src/1-digit/3-Three.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,60 @@
class Three {

constructor ( a , b , c ) {
this.a = a ;
this.b = b ;
this.c = c ;
}

*[Symbol.iterator] ( ) {
yield this.a ;
yield this.b ;
yield this.c ;
}

measure ( M ) {
return M.plus(
M.measure( this.a ) ,
M.plus(
M.measure( this.b ) ,
M.measure( this.c )
)
) ;
}

head ( ) {
return this.a ;
}

last ( ) {
return this.c ;
}

init ( ) {
return new Two( this.a , this.b ) ;
}

tail ( ) {
return new Two( this.b , this.c ) ;
}

push ( value ) {
return new Four( this.a , this.b , this.c , value ) ;
}

cons ( value ) {
return new Four( value , this.a , this.b , this.c ) ;
}

node ( M ) {
return node3( M , this.a , this.b , this.c ) ;
}

/**
* It is assumed that p(|this|) is true.
*/
splitDigit ( p , i , M ) {
i = M.plus( i , M.measure( this.a ) ) ;
if ( p( i ) ) return new Split( [ ] , this.a , [ this.b , this.c ] ) ;
i = M.plus( i , M.measure( this.b ) ) ;
if ( p( i ) ) return new Split( [ this.a ] , this.b , [ this.c ] ) ;
return new Split( [ this.a , this.b ] , this.c , [ ] ) ;
}

function Three ( a , b , c ) {
this.a = a ;
this.b = b ;
this.c = c ;
}

Three.prototype[Symbol.iterator] = function* ( ) {
yield this.a ;
yield this.b ;
yield this.c ;
} ;

Three.prototype.measure = function ( M ) {
return M.plus(
M.measure( this.a ) ,
M.plus(
M.measure( this.b ) ,
M.measure( this.c )
)
) ;
} ;

Three.prototype.head = function ( ) {
return this.a ;
} ;

Three.prototype.last = function ( ) {
return this.c ;
} ;

Three.prototype.init = function ( ) {
return new Two( this.a , this.b ) ;
} ;

Three.prototype.tail = function ( ) {
return new Two( this.b , this.c ) ;
} ;

Three.prototype.push = function ( value ) {
return new Four( this.a , this.b , this.c , value ) ;
} ;

Three.prototype.cons = function ( value ) {
return new Four( value , this.a , this.b , this.c ) ;
} ;

Three.prototype.node = function ( M ) {
return node3( M , this.a , this.b , this.c ) ;
} ;

/**
* It is assumed that p(|this|) is true.
*/
Three.prototype.splitDigit = function ( p , i , M ) {
i = M.plus( i , M.measure( this.a ) ) ;
if ( p( i ) ) return new Split( [ ] , this.a , [ this.b , this.c ] ) ;
i = M.plus( i , M.measure( this.b ) ) ;
if ( p( i ) ) return new Split( [ this.a ] , this.b , [ this.c ] ) ;
return new Split( [ this.a , this.b ] , this.c , [ ] ) ;
} ;
Loading

0 comments on commit 295011e

Please sign in to comment.