| /* | ||
| * Toni add, del values out of range. | ||
| */ | ||
| exports.test = function ( done, assertions ) { | ||
| var log = console.log | ||
| , exit = typeof done === 'function' ? done : function () {} | ||
| , assert = assertions || require( 'assert' ) | ||
| , Toni = require( '../' ) | ||
| , n = 10 | ||
| , toni = Toni( n ) | ||
| , i = 0 | ||
| ; | ||
| log( '- created an empty buffer bitmap ( capacity: %d )', n ); | ||
| log( '- add elements out of range.' ); | ||
| toni.add( 10 ); | ||
| toni.add( 11 ); | ||
| toni.add( 12 ); | ||
| toni.add( 13 ) | ||
| toni.add( 14 ) | ||
| toni.add( 15 ) | ||
| log( '- check bitmap, should be empty [0x00, 0x00].' ); | ||
| assert.equal( toni.items, 0 ); | ||
| assert.deepEqual( toni.bitmap, new Buffer( [ 0x0, 0x0 ] ) ); | ||
| log( '- fill bitmap, try to del values out of range.' ); | ||
| toni.bitmap.fill( 0xff ); | ||
| toni.items = 10; | ||
| toni.del( 10 ) | ||
| toni.del( 11 ) | ||
| toni.del( 12 ) | ||
| toni.del( 13 ) | ||
| toni.del( 14 ) | ||
| toni.del( 15 ) | ||
| log( '- check bitmap, should be full [0xff, 0xff].' ); | ||
| assert.equal( toni.items, 10 ); | ||
| assert.deepEqual( toni.bitmap, new Buffer( [ 0xff, 0xff ] ) ); | ||
| exit(); | ||
| }; | ||
| // single test execution with node | ||
| if ( process.argv[ 1 ] === __filename ) exports.test = exports.test(); |
+1
-0
@@ -0,1 +1,2 @@ | ||
| sudo: false | ||
| language: node_js | ||
@@ -2,0 +3,0 @@ node_js: |
+22
-30
@@ -16,33 +16,23 @@ /* | ||
| exports.Toni = ( function () { | ||
| var Bolgia = require( 'bolgia' ) | ||
| , abs = Math.abs | ||
| var abs = Math.abs | ||
| , ceil = Math.ceil | ||
| , max = Math.max | ||
| , floor = Math.floor | ||
| , clone = Bolgia.clone | ||
| , improve = Bolgia.improve | ||
| // toni default opt | ||
| , toni_opt = { | ||
| range : 8 | ||
| } | ||
| // table of powers | ||
| , bpower = [ 128, 64, 32, 16, 8, 4, 2, 1 ] | ||
| , Toni = function ( opt ) { | ||
| , Toni = function ( range ) { | ||
| var me = this | ||
| , is = me instanceof Toni | ||
| ; | ||
| if ( ! is ) return new Toni( opt ); | ||
| var cfg = improve( clone( opt ), toni_opt ) | ||
| // limit range to 4 bytes, (32 bits numbers) using >>> 0 | ||
| , range = cfg.range = max( abs( + cfg.range ) >>> 0, 8 ) | ||
| , bytes = ceil( range / 8 ) | ||
| , btable = new Buffer( bytes ) | ||
| if ( ! is ) return new Toni( range ); | ||
| // limit range to 4 bytes, (32 bits numbers) using >>> 0 | ||
| var r = ( abs( + range ) >>> 0 ) || 1 | ||
| , bytes = max( ceil( r / 8 ), 1 ) | ||
| , bitmap = new Buffer( bytes ) | ||
| ; | ||
| btable.fill( 0x00 ); | ||
| me.options = cfg; | ||
| me.btable = btable; | ||
| me.btlen = bytes << 3 >>> 0; | ||
| bitmap.fill( 0x00 ); | ||
| me.bitmap = bitmap; | ||
| me.bmlen = bytes << 3 >>> 0; | ||
| me.items = 0; | ||
| me.range = r; | ||
| } | ||
@@ -55,3 +45,3 @@ , tproto = Toni.prototype | ||
| ; | ||
| me.btable.fill( 0x00 ); | ||
| me.bitmap.fill( 0x00 ); | ||
| me.items = 0; | ||
@@ -64,6 +54,7 @@ return me; | ||
| , v = abs( value ) | ||
| , btable = me.btable | ||
| , bitmap = me.bitmap | ||
| , range = me.range | ||
| ; | ||
| // check value range | ||
| if ( me.btlen <= v ) return -1; | ||
| if ( ( me.bmlen <= v ) || ( v >= range ) ) return -1; | ||
| /* | ||
@@ -76,5 +67,5 @@ * generally, bucket and mask could be calculated respectively as floor( v / 8 ) | ||
| , mask = bpower[ v & 7 ] | ||
| , up = mask & btable[ buck ] | ||
| , up = mask & bitmap[ buck ] | ||
| ; | ||
| return up ? -1 : ++me.items | ( btable[ buck ] |= mask ); | ||
| return up ? -1 : ++me.items | ( bitmap[ buck ] |= mask ); | ||
| }; | ||
@@ -85,12 +76,13 @@ | ||
| , v = value | ||
| , btable = me.btable | ||
| , range = me.range | ||
| , bitmap = me.bitmap | ||
| ; | ||
| // check value range | ||
| if ( me.btlen <= v ) return -1; | ||
| if ( ( me.bmlen <= v ) || ( v >= range ) ) return -1; | ||
| var buck = v >>> 3 | ||
| , mask = bpower[ v & 7 ] | ||
| , up = mask & btable[ buck ] | ||
| , up = mask & bitmap[ buck ] | ||
| ; | ||
| return up ? --me.items | ( btable[ buck ] ^= mask ) : -1; | ||
| return up ? --me.items | ( bitmap[ buck ] ^= mask ) : -1; | ||
| }; | ||
@@ -97,0 +89,0 @@ |
+1
-1
| { | ||
| "name": "toni" | ||
| , "version": "0.4.2" | ||
| , "version": "0.5.0" | ||
| , "description": "Toni, a simple and efficient bitmap implementation for positive integer sets (max 32 bits), with no element repetition, using bitwise operations and a Buffer. Modifying a single bit instead of an entire byte, obviously saves 87.5% of Buffer space, but it also implies a gain greater than 200% in performances, for accessing values, when it was used with big integer ranges." | ||
@@ -5,0 +5,0 @@ , "homepage": "https://github.com/rootslab/toni" |
+10
-18
@@ -66,19 +66,10 @@ ### Toni | ||
| > minimun range is 1 item/bit, max is 2^32 (from 1 to 4 bytes). | ||
| ```javascript | ||
| Toni( [ Object opt ] ) | ||
| Toni( Number range ) | ||
| // or | ||
| new Toni( [ Object opt ] ) | ||
| new Toni( Number range ) | ||
| ``` | ||
| ####Options | ||
| > Default options are listed. | ||
| ```javascript | ||
| opt = { | ||
| // minimun range is 8 items/bits (1 byte), max is 2^32 (4 bytes) | ||
| range : 8 | ||
| } | ||
| ``` | ||
| ###Properties | ||
@@ -88,10 +79,10 @@ | ||
| /* | ||
| * Instance configuration object. | ||
| * the bitmap buffer. | ||
| */ | ||
| Toni.options : Object | ||
| Toni.bitmap : Buffer | ||
| /* | ||
| * the bitmap buffer. | ||
| * max range for values (from 0 to range - 1). | ||
| */ | ||
| Toni.btable : Buffer | ||
| Toni.range : Number | ||
@@ -106,3 +97,4 @@ /* | ||
| */ | ||
| Toni.btlen : Number | ||
| Toni.bmlen : Number | ||
| ``` | ||
@@ -109,0 +101,0 @@ |
@@ -11,5 +11,3 @@ /* | ||
| , n = 1024 | ||
| , toni = Toni( { | ||
| range : n | ||
| } ) | ||
| , toni = Toni( n ) | ||
| , i = 0 | ||
@@ -22,6 +20,6 @@ ; | ||
| log( '- fill the entire buffer bitmap, adding %d elements', n ); | ||
| for ( ; i < toni.options.range; toni.add( i++ ) ); | ||
| for ( ; i < toni.range; toni.add( i++ ) ); | ||
| log( '- check if all bytes are equal to %d', 0xff ); | ||
| for ( i = 0; i < toni.btable.length; i++ ) assert.equal( toni.btable[ i ], 0xff, 'Something goes wrong with add function!' ); | ||
| for ( i = 0; i < toni.bitmap.length; i++ ) assert.equal( toni.bitmap[ i ], 0xff, 'Something goes wrong with add function!' ); | ||
@@ -32,6 +30,6 @@ log( '- check item counter, should be %d', n ); | ||
| log( '- try to re-add all elements, it should always return %d', -1 ); | ||
| for ( i = 0; i < toni.options.range; ) assert.equal( toni.add( i++ ), -1, 'Something goes wrong with add function' ); | ||
| for ( i = 0; i < toni.range; ) assert.equal( toni.add( i++ ), -1, 'Something goes wrong with add function' ); | ||
| log( '- check if all bytes are equal to %d', 0xff ); | ||
| for ( i = 0; i < toni.btable.length; i++ ) assert.equal( toni.btable[ i ], 0xff, 'Something goes wrong with add function!' ); | ||
| for ( i = 0; i < toni.bitmap.length; i++ ) assert.equal( toni.bitmap[ i ], 0xff, 'Something goes wrong with add function!' ); | ||
@@ -42,3 +40,3 @@ log( '- check item counter, should be %d', n ); | ||
| log( '- try to add %d elements, out of range (>=%d), it should always return %d', n * 2, n, -1 ); | ||
| for ( i = n; i < toni.options.range + n + n; ) assert.equal( toni.add( i++ ), -1, 'Something goes wrong with add function' ); | ||
| for ( i = n; i < toni.range + n + n; ) assert.equal( toni.add( i++ ), -1, 'Something goes wrong with add function' ); | ||
@@ -53,3 +51,3 @@ log( '- check item counter, should be %d', n ); | ||
| log( '- try to del %d elements, out of range (>=%d), it should always return %d', n * 2, n, -1 ); | ||
| for ( i = n; i < toni.options.range + n + n; ) assert.equal( toni.del( i++ ), -1, 'Something goes wrong with del function' ); | ||
| for ( i = n; i < toni.range + n + n; ) assert.equal( toni.del( i++ ), -1, 'Something goes wrong with del function' ); | ||
@@ -60,6 +58,6 @@ log( '- check item counter, should be %d', n ); | ||
| log( '- empty buffer bitmap, removing %d elements', n ); | ||
| for ( i = 0; i < toni.options.range; toni.del( i++ ) ); | ||
| for ( i = 0; i < toni.range; toni.del( i++ ) ); | ||
| log( '- check if all bytes are equal to %d', 0x00 ); | ||
| for ( i = 0; i < toni.btable.length; i++ ) assert.equal( toni.btable[ i ], 0x00, 'Something goes wrong with del function!' ); | ||
| for ( i = 0; i < toni.bitmap.length; i++ ) assert.equal( toni.bitmap[ i ], 0x00, 'Something goes wrong with del function!' ); | ||
@@ -70,6 +68,6 @@ log( '- check item counter, should be %d', 0 ); | ||
| log( '- try to re-delete all elements, it should always return %d', -1 ); | ||
| for ( i = 0; i < toni.options.range; ) assert.equal( toni.del( i++ ), -1, 'Something goes wrong with del function' ); | ||
| for ( i = 0; i < toni.range; ) assert.equal( toni.del( i++ ), -1, 'Something goes wrong with del function' ); | ||
| log( '- check if all bytes are equal to %d', 0x00 ); | ||
| for ( i = 0; i < toni.btable.length; i++ ) assert.equal( toni.btable[ i ], 0x00, 'Something goes wrong with del function!' ); | ||
| for ( i = 0; i < toni.bitmap.length; i++ ) assert.equal( toni.bitmap[ i ], 0x00, 'Something goes wrong with del function!' ); | ||
@@ -80,3 +78,3 @@ log( '- check item counter, should be %d', 0 ); | ||
| log( '- try to del %d elements, out of range (>=%d), it should always return %d', n * 2, n, -1 ); | ||
| for ( i = n; i < toni.options.range + n + n; ) assert.equal( toni.del( i++ ), -1, 'Something goes wrong with del function' ); | ||
| for ( i = n; i < toni.range + n + n; ) assert.equal( toni.del( i++ ), -1, 'Something goes wrong with del function' ); | ||
@@ -90,3 +88,3 @@ log( '- check item counter, should be %d', 0 ); | ||
| log( '- manually fill entire Buffer with %d\'s', 0x01 ); | ||
| toni.btable.fill( 0x01 ); | ||
| toni.bitmap.fill( 0x01 ); | ||
@@ -100,3 +98,3 @@ log( '- manually set items to %d', n ); | ||
| log( '- bitmap should be empty' ); | ||
| for ( i = 0; i < toni.btable.length; i++ ) assert.equal( toni.btable[ i ], 0x00, 'Something goes wrong with clear function!' ); | ||
| for ( i = 0; i < toni.bitmap.length; i++ ) assert.equal( toni.bitmap[ i ], 0x00, 'Something goes wrong with clear function!' ); | ||
@@ -103,0 +101,0 @@ log( '- check item property, should be %d', 0 ); |
23295
4.17%15
7.14%273
12.35%148
-5.13%