Skip to content

Instantly share code, notes, and snippets.

@NV
Created September 25, 2014 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NV/106b3c60826a5aa996bf to your computer and use it in GitHub Desktop.
Save NV/106b3c60826a5aa996bf to your computer and use it in GitHub Desktop.
requirebin sketch
// example using the raf module from npm. try changing some values!
var BitSet = require("bitterset");
var s = new BitSet();
for (var i = 0; i <= 13; i++) {
s.set(i);
}
log(s.get(0));
log(s.store);
s.clear(0);
log(s.store);
log(s.get(0));
function log(msg) {
document.body.textContent += msg + '\n';
document.body.style.whiteSpace = 'pre';
}
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({bitterset:[function(require,module,exports){(function(){var BitterSet;module.exports=BitterSet=function(){var ADDR_BITS,HAMMING_TABLE,WORD_BITS,bits,bstring,weight;WORD_BITS=32;ADDR_BITS=5;HAMMING_TABLE=[0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4];weight=function(word){return HAMMING_TABLE[word>>0&15]+HAMMING_TABLE[word>>4&15]+HAMMING_TABLE[word>>8&15]+HAMMING_TABLE[word>>12&15]+HAMMING_TABLE[word>>16&15]+HAMMING_TABLE[word>>20&15]+HAMMING_TABLE[word>>24&15]+HAMMING_TABLE[word>>28&15]};bits=function(word,offset){var bit,_i,_ref,_results;if(offset==null){offset=0}_results=[];for(bit=_i=0,_ref=WORD_BITS-1;0<=_ref?_i<=_ref:_i>=_ref;bit=0<=_ref?++_i:--_i){if((word&1<<bit)!==0){_results.push(bit+offset*WORD_BITS)}}return _results};bstring=function(word){return(word>>>0).toString(2)};function BitterSet(){this.store=[]}BitterSet.prototype.get=function(bit){return(this.store[bit>>ADDR_BITS]&1<<bit)!==0};BitterSet.prototype.set=function(bit){this.store[bit>>ADDR_BITS]|=1<<bit};BitterSet.prototype.clear=function(bit){if(bit!=null){this.store[bit>>ADDR_BITS]&=~(1<<bit)}else{this.store=[]}};BitterSet.prototype.flip=function(bit){this.store[bit>>ADDR_BITS]^=1<<bit};BitterSet.prototype.next=function(value,from){var length;length=this.length();while(from<length){if(this.get(from)===value){return from}from+=1}if(value===false){return from}else{return-1}};BitterSet.prototype.previous=function(value,from){while(!(from<0)){if(this.get(from)===value){return from}from-=1}return-1};BitterSet.prototype.length=function(){var fill,tail;this.cull();if(this.store.length===0){return 0}fill=WORD_BITS*(this.store.length-1);tail=bstring(this.store[this.store.length-1]).length;return fill+tail};BitterSet.prototype.cardinality=function(){var reducer;reducer=function(sum,word){return sum+weight(word)};return this.store.reduce(reducer,0)};BitterSet.prototype.cull=function(){var tail;while(this.store.length>0){tail=this.store[this.store.length-1];if(tail==null||tail===0){this.store.pop()}else{break}}};BitterSet.prototype.or=function(set){var i,_i,_ref;if(set===this){return}for(i=_i=0,_ref=set.store.length-1;0<=_ref?_i<=_ref:_i>=_ref;i=0<=_ref?++_i:--_i){this.store[i]|=set.store[i]}};BitterSet.prototype.and=function(set){var i,_i,_ref;if(set===this){return}for(i=_i=0,_ref=this.store.length-1;0<=_ref?_i<=_ref:_i>=_ref;i=0<=_ref?++_i:--_i){this.store[i]&=set.store[i]||0}this.cull()};BitterSet.prototype.andnot=function(set){var i,_i,_ref;if(set===this){this.clear()}else{for(i=_i=0,_ref=this.store.length-1;0<=_ref?_i<=_ref:_i>=_ref;i=0<=_ref?++_i:--_i){this.store[i]&=~(set.store[i]||0)}this.cull()}};BitterSet.prototype.xor=function(set){var i,_i,_ref;if(set===this){this.clear()}else{for(i=_i=0,_ref=set.store.length;0<=_ref?_i<=_ref:_i>=_ref;i=0<=_ref?++_i:--_i){this.store[i]^=set.store[i]}this.cull()}};BitterSet.prototype.toString=function(){var index,word;this.cull();return"{"+function(){var _i,_len,_ref,_results;_ref=this.store;_results=[];for(index=_i=0,_len=_ref.length;_i<_len;index=++_i){word=_ref[index];if(word!=null&&word!==0){_results.push(bits(word,index))}}return _results}.call(this)+"}"};BitterSet.prototype.toBinaryString=function(){var reducer;this.cull();reducer=function(string,word,index){var fill;fill=index>0?Array(index*WORD_BITS-string.length+1).join("0"):"";return bstring(word)+fill+string};return this.store.reduce(reducer,"")};return BitterSet}()}).call(this)},{}]},{},[]);var BitSet=require("bitterset");var s=new BitSet;for(var i=0;i<=13;i++){s.set(i)}log(s.get(0));log(s.store);s.clear(0);log(s.store);log(s.get(0));function log(msg){document.body.textContent+=msg+"\n";document.body.style.whiteSpace="pre"}
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"bitterset": "0.0.3"
}
}
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment