Comparing version 0.2.0 to 0.3.0
@@ -9,8 +9,7 @@ (function() { | ||
this.addressBitsPerWord = 5; | ||
this.bitIndexMask = this.bitsPerWord - 1; | ||
this.store = []; | ||
} | ||
BitSet.prototype.wordIndex = function(bitIndex) { | ||
return bitIndex >> this.addressBitsPerWord; | ||
BitSet.prototype.wordIndex = function(pos) { | ||
return pos >> this.addressBitsPerWord; | ||
}; | ||
@@ -39,7 +38,9 @@ | ||
BitSet.prototype.wordLength = function() { | ||
if (this.store.length === 1 && this.store[0] === 0) { | ||
return 0; | ||
} else { | ||
return this.store.length; | ||
var length, pos, _ref; | ||
length = this.store.length; | ||
for (pos = _ref = this.store.length - 1; _ref <= 0 ? pos <= 0 : pos >= 0; _ref <= 0 ? pos++ : pos--) { | ||
if (this.store[pos] !== 0) break; | ||
length--; | ||
} | ||
return length; | ||
}; | ||
@@ -61,2 +62,11 @@ | ||
BitSet.prototype.toString = function() { | ||
var pos, result, _ref; | ||
result = []; | ||
for (pos = 0, _ref = this.length(); 0 <= _ref ? pos <= _ref : pos >= _ref; 0 <= _ref ? pos++ : pos--) { | ||
if (this.get(pos)) result.push(pos); | ||
} | ||
return "{" + (result.join(",")) + "}"; | ||
}; | ||
BitSet.prototype.toBinaryString = function() { | ||
var lpad; | ||
@@ -70,3 +80,3 @@ var _this = this; | ||
}; | ||
if (this.store.length > 0) { | ||
if (this.wordLength() > 0) { | ||
return this.store.map(function(word) { | ||
@@ -73,0 +83,0 @@ return lpad(word.toString(2), '0', _this.bitsPerWord); |
@@ -6,3 +6,3 @@ { | ||
"keywords": ["bit", "bitset", "metrics"], | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"repository": { | ||
@@ -19,2 +19,6 @@ "type": "git", | ||
}, | ||
"directories": { | ||
"lib": "./lib" | ||
}, | ||
"files": ["README.md", "./lib", "./src", "./support", "./test"], | ||
"dependencies": {}, | ||
@@ -21,0 +25,0 @@ "devDependencies": { |
@@ -1,21 +0,57 @@ | ||
[![Build Status](https://secure.travis-ci.org/tdegrunt/bitset.png)](http://travis-ci.org/tdegrunt/bitset) | ||
[![Build Status](https://secure.travis-ci.org/tdegrunt/bitset.png)](http://travis-ci.org/tdegrunt/bitset) | ||
BitSet | ||
====== | ||
This module implements an array of bits, that grows as needed. It can be used from both JavaScript as well as CoffeeScript. | ||
This module implements an array of bits, that grows as needed. It can be used from both JavaScript: | ||
var bs = new BitSet(); | ||
_(12).times(function(n){ | ||
for(var n = 0; n < 12; n++) { | ||
bs.set(n); | ||
}); | ||
} | ||
console.dir(bs); | ||
Mind you: The example uses [underscore.js](http://documentcloud.github.com/underscore/) | ||
as well as CoffeeScript: | ||
bs = new BitSet | ||
bs.set 1 | ||
bs.clear 1 | ||
console.log bs.toString() | ||
Usage | ||
----- | ||
This is very useful for realtime metrics with for example Redis. | ||
BitSets are very useful for realtime metrics. If you want to count the number of active users (CoffeeScript): | ||
# Somewhere in application code | ||
todaysUserCount = new Bitset | ||
# The User class | ||
class User | ||
@login: (userName, password) -> | ||
todaysUserCount.set @userId | ||
# In reporting code | ||
todaysUserCount.cardinality() | ||
Documentation | ||
------------- | ||
* `BitSet#set(pos)` - sets the bit on position pos to true | ||
* `BitSet#get(pos)` - returns whether the bit on position pos is set | ||
* `BitSet#clear(pos)` - clears the bit on position pos | ||
* `BitSet#length` - returns the logical length of the bitset | ||
* `BitSet#wordLength` - returns the word-length of the bitset | ||
* `BitSet#cardinality` - returns how many bits are set to true in the bitset | ||
* `BitSet#toString` - returns a string representation of the bitset | ||
* `BitSet#toBinaryString` - returns a binary string representation of the bitset | ||
* `BitSet#or(bitset)` - OR's this bitset with the argument bitset | ||
* `BitSet#and(bitset)` - AND's this bitset with the argument bitset | ||
* `BitSet#andNot(bitset)` - ANDNOT's this bitset with the argument bitset | ||
* `BitSet#xor(bitset)` - XOR's this bitset with the argument bitset | ||
For details see src/bitset.coffee | ||
Installation | ||
@@ -22,0 +58,0 @@ ------------ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
16539
124
71
6