Comparing version 0.0.34 to 0.0.35
@@ -5,3 +5,3 @@ var log = console.log | ||
, input = fs.createReadStream( './example/test-data.txt' ) | ||
, sts = new require( '../lib/streams/sequence-transform' )( 10, 10, {} ) | ||
, sts = new require( '../lib/streams/sequence-transform' )( 16, 10, {} ) | ||
, dstream = input.pipe( sts ) | ||
@@ -12,3 +12,3 @@ , onRead = function () { | ||
; | ||
while ( data = me.read( 3 ) ) log( 'data:', data ); | ||
while ( data = me.read(3) ) log( 'data:', data ); | ||
} | ||
@@ -15,0 +15,0 @@ , onEnd = function () { |
@@ -19,4 +19,2 @@ /* | ||
, transform = require( './streams/' ) | ||
// mask to appy for restricting items | ||
// , imask = 0xffffffff | ||
; | ||
@@ -31,3 +29,2 @@ return { | ||
; | ||
// restrict items with mask: i &= imask; | ||
// with no repetition, items should be always <= range | ||
@@ -46,3 +43,2 @@ return seq ? new random.sequence( i, r ) : | ||
; | ||
// restrict items with mask : i &= imask; | ||
// with no repetition, items should be always <= range | ||
@@ -49,0 +45,0 @@ return seq ? new transform.sequence( i, r, stream_opt ) : |
@@ -106,3 +106,3 @@ /* | ||
* algo checks for values with p+1 bits, but those values are in the range 0 to 2^(p + 1), | ||
* so the probability to find a value in the correct range is: | ||
* so, on the average, the probability to find a value in the correct range is: | ||
* | ||
@@ -112,3 +112,4 @@ * (2^(p) + 1) / 2^(p + 1), or 1/2 + 2^-(p+1) ~ 1/2 | ||
* Then, using result from the previous case, the probability to find the k-th value, is | ||
* 1/2 - (k - 1)/(range*2). | ||
* (1/2) - (1/2)*(k - 1)/range. When we set items ~ range/2, the probability to find the | ||
* last value is, on the average, =~ 1/4 + 1/(2*r) or ~ 25%. | ||
* | ||
@@ -115,0 +116,0 @@ */ |
/* | ||
* Transform Stream for Random Sequence, unlimited repetitions. | ||
* Transform Stream for Random Sequence, unlimited repetitions. | ||
*/ | ||
@@ -11,5 +11,3 @@ | ||
, dlog = Math.log | ||
, floor = Math.floor | ||
, max = Math.max | ||
, random = Math.random | ||
, log2 = dlog( 2 ) | ||
@@ -21,3 +19,2 @@ , util = require( 'util' ) | ||
, compare = Bice.compare | ||
, swap = Bice.swap | ||
, clone = Bolgia.clone | ||
@@ -47,3 +44,4 @@ , improve = Bolgia.improve | ||
var range = r >>> 0 ? abs( r ) : 1 | ||
, items = i >>> 0 ? abs( i ) : 1 | ||
// 0 items means no limits. | ||
, items = abs( i ) >>> 0 | ||
// , repeat = Infinity | ||
@@ -53,8 +51,5 @@ , bits = max( ceil( dlog( range ) / log2 ), 1 ) | ||
, ibits = ibytes << 3 >>> 0 | ||
, tbytes = ibytes * items | ||
// choose appropriate method for reading/writing ints/uints from 8 to 32 bits | ||
, ruint = [ 'readUInt' + ibits + ( ibytes > 1 ? 'BE' : '' ) ] | ||
, wuint = [ 'writeUInt' + ibits + ( ibytes > 1 ? 'BE' : '' ) ] | ||
// current writing position in the result Buffer, when parsing random data | ||
, wpos = 0 | ||
// use a Buffer for range value, for faster comparisons when parsing data. | ||
@@ -64,3 +59,2 @@ , brange = new Buffer( ibytes ) | ||
, bcompare = compare.bind( me, brange, 0 ) | ||
, bsize = 8 * 1024 | ||
; | ||
@@ -74,13 +68,11 @@ | ||
, items : items | ||
, left : items ? items : -1 | ||
, bits : bits | ||
, ibytes : ibytes | ||
, ibits : ibits | ||
, tbytes : tbytes | ||
, runit : ruint | ||
, wuint : wuint | ||
, bcompare : bcompare | ||
, cmask : masks[ ibits - bits ] | ||
, compare : bcompare | ||
, brange : brange | ||
, buffer : new Buffer( bsize ) | ||
, bsize : bsize | ||
, bpos : 0 | ||
}; | ||
@@ -99,38 +91,32 @@ | ||
, seq = me._sequence | ||
, bcomp = seq.bcompare | ||
, brange = seq.brange | ||
, result = seq.result | ||
, bcomp = seq.compare | ||
, ibytes = seq.ibytes | ||
, tbytes = seq.tbytes | ||
, ibits = seq.ibits | ||
, bits = seq.bits | ||
, ibits = seq.ibits | ||
, cmask = masks[ ibits - bits ] | ||
, cmask = seq.cmask | ||
, dlen = data.length | ||
, l = dlen - ibytes | ||
, o = 0 | ||
, s = 0 | ||
; | ||
/* | ||
* Bufferize the incoming random data. | ||
* | ||
* NOTE: without buffering, push could be: | ||
* if ( ~ bcomp( data, o ) ) | ||
* me.push( data.slice( o, o + ibytes ) ); | ||
*/ | ||
for ( ; o <= l; o += ibytes ) { | ||
data[ o ] &= cmask; | ||
if ( ~ bcomp( data, o ) ) { | ||
if ( seq.bpos + ibytes >= seq.bsize ) { | ||
// current buffer is not big enough, create a new one, | ||
me.push( seq.buffer.slice( 0, seq.bpos ) ); | ||
seq.bpos = 0; | ||
seq.buffer = new Buffer( seq.bsize ); | ||
// value is in the selected range | ||
if ( ( ~ seq.left ) && ( --seq.left <= 0 ) ) { | ||
// items limit was reached, push data | ||
if ( s <= o ) me.push( data.slice( s, o + ibytes ) ); | ||
// reset the counter for left items | ||
seq.left = seq.items ? seq.items : -1 | ||
// end the stream | ||
return me.push( null ) & done(); | ||
} | ||
// TODO: it's better to copy a slice, not a value at a time. | ||
data.copy( seq.buffer, seq.bpos, o, o + ibytes ); | ||
seq.bpos += ibytes; | ||
// limit result to items. | ||
if ( seq.bpos === tbytes ) | ||
me.push( seq.buffer.slice( 0, tbytes ) ); | ||
continue; | ||
} | ||
// value is out of range, slice all good values | ||
if ( s < o ) me.push( data.slice( s, o ) ); | ||
s = o + ibytes; | ||
} | ||
// before calling done(), check for previous skipped values to push | ||
if ( s + ibytes < o ) me.push( data.slice( s, dlen ) ); | ||
done(); | ||
@@ -142,7 +128,3 @@ }; | ||
, seq = me._sequence | ||
, bpos = seq.bpos | ||
, ibytes = seq.ibytes | ||
, buff = seq.buffer | ||
; | ||
seq.bpos = 0; | ||
done(); | ||
@@ -149,0 +131,0 @@ }; |
{ | ||
"name": "brando" | ||
, "version": "0.0.34" | ||
, "version": "0.0.35" | ||
, "description": "Brando." | ||
@@ -5,0 +5,0 @@ , "homepage": "https://github.com/rootslab/brando" |
@@ -58,5 +58,3 @@ ### Brando | ||
/* | ||
* A simple factory method, it returns a Sequence EventEmitter,for filling a | ||
* random (Buffer) sequence/permutation only with values the current selected | ||
* range. | ||
* A simple factory method, it returns a Sequence EventEmitter. | ||
* | ||
@@ -63,0 +61,0 @@ * NOTE: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46915
915
106