Comparing version 0.1.5 to 0.2.1
/** | ||
* JavaScript BitArray - v0.1.5 | ||
* JavaScript BitArray - v0.2.0 | ||
* | ||
@@ -10,18 +10,18 @@ * Licensed under the revised BSD License. | ||
/** | ||
* Creates a new empty BitArray, or initialises the BitArray with the given BitArray serialisation (an Array of integers.) | ||
* Creates a new empty BitArray with the given length or initialises the BitArray with the given hex representation. | ||
*/ | ||
var BitArray = function (values) { | ||
if (values && values.toString() === values) { | ||
this.values = []; | ||
var hex = values.slice(/^0x/.exec(values) ? 2 : 0); | ||
while (hex.length % 8 !== 0) { | ||
hex = '0' + hex; | ||
if (values && values.toString() === values) { | ||
this.values = []; | ||
var hex = values.slice(/^0x/.exec(values) ? 2 : 0); | ||
while (hex.length % 8 !== 0) { | ||
hex = '0' + hex; | ||
} | ||
for (var i = 0; i < (hex.length / 8); i++) { | ||
var slice = hex.slice(i * 8, i * 8 + 8); | ||
this.values.push(parseInt(slice, 16)); | ||
} | ||
} else { | ||
this.values = Array((values || 32) / 32); | ||
} | ||
for (var i = 0; i < (hex.length / 8); i++) { | ||
var slice = hex.slice(i * 8, i * 8 + 8); | ||
this.values.push(parseInt(slice, 16)); | ||
} | ||
} else { | ||
this.values = values || []; | ||
} | ||
}; | ||
@@ -137,9 +137,2 @@ | ||
/** | ||
* Returns the internal representation of the BitArray. | ||
*/ | ||
BitArray.prototype.valueOf = function () { | ||
return this.values; | ||
}; | ||
/** | ||
* Convert the BitArray to an Array of boolean values. | ||
@@ -149,5 +142,6 @@ */ | ||
var result = []; | ||
this.forEach(function (value, index) { | ||
result.push(value); | ||
}); | ||
for (var i = 0; i < this.values.length * 32; i += 1) { | ||
result.push(this.get(i)); | ||
} | ||
return result; | ||
@@ -157,15 +151,2 @@ }; | ||
/** | ||
* Convert the BitArray to an Array of integers specifying which bits are set. | ||
*/ | ||
BitArray.prototype.toIntArray = function () { | ||
var result = []; | ||
this.forEach(function (value, index) { | ||
if (value) { | ||
result.push(index); | ||
} | ||
}); | ||
return result; | ||
}; | ||
/** | ||
* Returns the total number of bits set to one in this BitArray. | ||
@@ -190,22 +171,2 @@ */ | ||
/** | ||
* Iterate over each value in the BitArray. | ||
*/ | ||
BitArray.prototype.forEach = function (fn, scope) { | ||
var i = 0, b = 0, index = 0, | ||
len = this.values.length, | ||
value, word; | ||
for (; i < len; i += 1) { | ||
word = this.values[i]; | ||
for (b = 0; b < 32; b += 1) { | ||
value = (word & 1) !== 0; | ||
fn.call(scope, value, index, this); | ||
word = word >> 1; | ||
index += 1; | ||
} | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Inverts this BitArray. | ||
@@ -212,0 +173,0 @@ */ |
{ | ||
"name": "bit-array", | ||
"description": "JavaScript implementation of bit arrays", | ||
"version": "0.1.5", | ||
"author": "Bram Stein <b.l.stein@gmail.com> (http://www.bramstein.com)", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/bramstein/bit-array.git" | ||
}, | ||
"keywords": ["bit array", "bit-array", "bit vector", "bitset", "bitmap", "bitstring"], | ||
"main": "./lib/bit-array.js", | ||
"directories": { | ||
"lib": "./lib" | ||
}, | ||
"devDependencies": { | ||
"test": "0.4.x" | ||
}, | ||
"scripts": { | ||
"test": "node tests/bit-array.test.js" | ||
} | ||
"name": "bit-array", | ||
"description": "JavaScript implementation of bit arrays", | ||
"version": "0.2.1", | ||
"author": "Bram Stein <b.l.stein@gmail.com> (http://www.bramstein.com)", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/bramstein/bit-array.git" | ||
}, | ||
"keywords": ["bit array", "bit-array", "bit vector", "bitset", "bitmap", "bitstring"], | ||
"main": "./lib/bit-typed-array.js", | ||
"directories": { | ||
"lib": "./lib" | ||
}, | ||
"devDependencies": { | ||
"test": "0.4.x" | ||
}, | ||
"scripts": { | ||
"test": "node tests/bit-array.test.js" | ||
} | ||
} |
@@ -12,5 +12,5 @@ ## JavaScript Bit Array Library | ||
The bit array also grows automatically depending on your usage and is sparse. The following example shows how to set and get individual bits within the array: | ||
The bit array is sparse. The following example shows how to set and get individual bits within the array: | ||
a = new BitArray(); | ||
a = new BitArray(32); | ||
a.set(0, true); | ||
@@ -22,3 +22,3 @@ a.set(31, true); | ||
Note that the array internally uses 32 bit integers and thus grows by 32 bits if necessary (Actually, JavaScript's number type is 64 bit, but only 32 bits can be addressed using bitwise operations.) | ||
Note that the array internally uses 32 bit integers (actually, JavaScript's number type is 64 bit, but only 32 bits can be addressed using bitwise operations.) | ||
@@ -35,10 +35,7 @@ a.set(32, true); | ||
<dl> | ||
<dt>BitArray()</dt> | ||
<dd>Creates a new empty bit array.</dd> | ||
<dt>BitArray(length)</dt> | ||
<dd>Creates a new empty bit array with the given length.</dd> | ||
<dt>BitArray(string)</dt> | ||
<dd>Creates a new bit array using the hex values in the string</dd> | ||
<dt>BitArray([integer, ...])</dt> | ||
<dd>Creates a new bit array using the array of integers as internal representation.</dd> | ||
</dl> | ||
@@ -54,3 +51,3 @@ | ||
<dd>Sets the bit at index to a value (boolean.)</dd> | ||
<dt>get(index)</dt> | ||
@@ -61,3 +58,3 @@ <dd>Returns the value of the bit at index (boolean.)</dd> | ||
<dd>Toggles the bit at index. If the bit is on, it is turned off. Likewise, if the bit is off it is turned on.</dd> | ||
<dt>reset()</dt> | ||
@@ -68,36 +65,30 @@ <dd>Resets the BitArray so that it is empty and can be re-used.</dd> | ||
<dd>Returns a copy of this BitArray.</dd> | ||
<dt>equals(other)</dt> | ||
<dd>Returns true if this BitArray equals another. Two BitArrays are considered equal if both have the same length and bit pattern.</dd> | ||
<dt>toJSON()</dt> | ||
<dd>Returns the JSON representation of this BitArray.</dd> | ||
<dt>toString()</dt> | ||
<dd>Returns a string representation of the BitArray with bits in logical order.</dd> | ||
<dt>toHexString()</dt> | ||
<dd>Returns a hex representation of the BitArray.</dd> | ||
<dt>valueOf()</dt> | ||
<dd>Returns the internal representation of the BitArray.</dd> | ||
<dt>toArray()</dt> | ||
<dd>Convert the BitArray to an Array of boolean values.</dd> | ||
<dt>count()</dt> | ||
<dd>Returns the total number of bits set to 1 in this BitArray.</dd> | ||
<dt>forEach(fn, scope)</dt> | ||
<dd>Iterate over each value in the BitArray.</dd> | ||
<dt>not()</dt> | ||
<dd>Inverts this BitArray.</dd> | ||
<dt>or(other)</dt> | ||
<dd>Bitwise OR on the values of this BitArray using BitArray `other`.</dd> | ||
<dt>and(other)</dt> | ||
<dd>Bitwise AND on the values of this BitArray using BitArray `other`.</dd> | ||
<dt>xor(other)</dt> | ||
@@ -117,3 +108,3 @@ <dd>Bitwise XOR on the values of this BitArray using BitArray `other`.</dd> | ||
Licensed under the revised BSD License. Copyright 2010 Bram Stein. All rights reserved. | ||
Licensed under the revised BSD License. Copyright 2010-2012 Bram Stein. All rights reserved. | ||
@@ -120,0 +111,0 @@ Ports |
var assert = require('assert'), | ||
BitArray = require('../lib/bit-array.js'); | ||
BitArray = require('../lib/bit-typed-array.js'); | ||
exports.testConstructor = function () { | ||
assert.equal((new BitArray()).toString().length, 0, 'Empty bit array: toString()'); | ||
assert.equal((new BitArray()).size(), 0, 'Empty bit array: size().'); | ||
assert.equal(new BitArray(0).toString().length, 0, 'Empty bit array: toString()'); | ||
assert.equal(new BitArray(0).size(), 0, 'Empty bit array: size().'); | ||
}; | ||
exports.testValueConstructor = function () { | ||
var b = new BitArray([1610614016, 90112]); | ||
// b.set(10, true); | ||
// b.set(29, true); | ||
// b.set(30, true); | ||
// b.set(8, true); | ||
// b.set(45, true); | ||
// b.set(46, true); | ||
// b.set(48, true); | ||
assert.equal(b.toString(), '0000000010100000000000000000011000000000000001101000000000000000', 'BitArray([..])'); | ||
var c = new BitArray([0xffffeeee, 0xddddcccc]); | ||
assert.equal(c.toHexString(), 'ffffeeeeddddcccc'); | ||
}; | ||
exports.testHexConstructor = function () { | ||
var binaryDeadBeef = '11011110101011011011111011101111'; | ||
var binaryDeadBeef = '11011110101011011011111011101111'; | ||
var b = new BitArray('deadbeef'); | ||
// Note that the "logical order" returned by the BitArray type is reversed | ||
// from the "mathematical order" used to convert between representations. | ||
// Note that the "logical order" returned by the BitArray type is reversed | ||
// from the "mathematical order" used to convert between representations. | ||
assert.equal(b.toString().split('').reverse().join(''), binaryDeadBeef, 'BitArray(0x...)'); | ||
@@ -37,10 +19,9 @@ assert.equal(b.toBinaryString(), binaryDeadBeef, 'BitArray(0x...)'); | ||
// Test zero-padding | ||
var c = new BitArray('c0ffeec0ffee').and(new BitArray('ffffffffff000000')); | ||
assert.equal(c.toHexString(), '0000c0ffee000000', 'BitArray(0x...)'); | ||
assert.equal(c.toHexString(), 'c0ffee000000', 'BitArray(0x...)'); | ||
}; | ||
exports.testSimpleSet = function () { | ||
var b = new BitArray(); | ||
var b = new BitArray(32); | ||
@@ -76,3 +57,3 @@ b.set(31, true); // 0 | 1 << 30 | ||
exports.testArraySet = function () { | ||
var b = new BitArray(); | ||
var b = new BitArray(64); | ||
@@ -85,15 +66,4 @@ b.set(32, true); | ||
exports.testToHexString = function () { | ||
var b = new BitArray() | ||
b.set(512, true); | ||
assert.equal(b.toHexString(), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'); | ||
}; | ||
exports.testToHexStringConstructor = function () { | ||
var b = new BitArray('deadbeef'); | ||
assert.equal(b.toHexString(), 'deadbeef'); | ||
}; | ||
exports.testSimpleGet = function () { | ||
var b = new BitArray(); | ||
var b = new BitArray(32); | ||
b.set(0, true); | ||
@@ -109,3 +79,3 @@ b.set(4, true); | ||
exports.testArrayGet = function () { | ||
var b = new BitArray(); | ||
var b = new BitArray(64); | ||
b.set(32, true); | ||
@@ -118,3 +88,3 @@ | ||
exports.testSimpleToggle = function () { | ||
var b = new BitArray(); | ||
var b = new BitArray(32); | ||
b.set(0, true); | ||
@@ -129,3 +99,3 @@ b.set(31, true); | ||
exports.testArrayToggle = function () { | ||
var b = new BitArray(); | ||
var b = new BitArray(64); | ||
b.set(32, true); | ||
@@ -139,3 +109,3 @@ | ||
exports.testSize = function () { | ||
var b = new BitArray(); | ||
var b = new BitArray(224); | ||
b.set(200, true); // Math.floor(200 / 32) + 1 * 32; | ||
@@ -146,3 +116,3 @@ assert.equal(b.size(), 224, 'size()'); | ||
exports.testBitCount = function () { | ||
var b = new BitArray(); | ||
var b = new BitArray(72); | ||
@@ -158,6 +128,6 @@ b.set(32, true); | ||
exports.testEquals = function () { | ||
var a = new BitArray(), | ||
b = new BitArray(), | ||
c = new BitArray(), | ||
d = new BitArray(); | ||
var a = new BitArray(224), | ||
b = new BitArray(224), | ||
c = new BitArray(224), | ||
d = new BitArray(224); | ||
@@ -188,3 +158,3 @@ a.set(0, true); | ||
exports.testCopy = function () { | ||
var a = new BitArray(), | ||
var a = new BitArray(224), | ||
b; | ||
@@ -202,3 +172,3 @@ | ||
exports.testNot = function () { | ||
var b = new BitArray(), | ||
var b = new BitArray(64), | ||
a; | ||
@@ -222,4 +192,4 @@ | ||
exports.testOr = function () { | ||
var a = new BitArray(), | ||
b = new BitArray(); | ||
var a = new BitArray(32), | ||
b = new BitArray(32); | ||
@@ -240,4 +210,4 @@ a.set(0, true);//1110 | ||
exports.testAnd = function () { | ||
var a = new BitArray(), | ||
b = new BitArray(); | ||
var a = new BitArray(32), | ||
b = new BitArray(32); | ||
@@ -258,4 +228,4 @@ a.set(0, true); | ||
exports.testXor = function () { | ||
var a = new BitArray(), | ||
b = new BitArray(); | ||
var a = new BitArray(32), | ||
b = new BitArray(32); | ||
@@ -262,0 +232,0 @@ a.set(0, true); |
21811
6
594
107