Skip to content

Instantly share code, notes, and snippets.

@NV
Last active August 29, 2015 14:06
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/3a6e57bacfbfeb34a66d to your computer and use it in GitHub Desktop.
Save NV/3a6e57bacfbfeb34a66d 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("bitset");
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}({bitset:[function(require,module,exports){(function(){var BitSet;module.exports=BitSet=function(){function BitSet(){this.bitsPerWord=32;this.addressBitsPerWord=5;this.store=[]}BitSet.prototype.wordIndex=function(pos){return pos>>this.addressBitsPerWord};BitSet.prototype.set=function(pos){return this.store[this.wordIndex(pos-1)]|=1<<pos-1};BitSet.prototype.clear=function(pos){return this.store[this.wordIndex(pos-1)]&=255^1<<pos-1};BitSet.prototype.get=function(pos){return(this.store[this.wordIndex(pos-1)]&1<<pos-1)!==0};BitSet.prototype.length=function(){if(this.wordLength()===0){return 0}else{return this.bitsPerWord*(this.wordLength()-1)+(this.store[this.wordLength()-1].toString(2).length+1)}};BitSet.prototype.wordLength=function(){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};BitSet.prototype.store=function(){return this.store};BitSet.prototype.cardinality=function(){var pos,sum,_ref;sum=0;for(pos=0,_ref=this.length();0<=_ref?pos<=_ref:pos>=_ref;0<=_ref?pos++:pos--){if(this.get(pos))sum++}return sum};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;var _this=this;lpad=function(str,padString,length){while(str.length<length){str=padString+str}return str};if(this.wordLength()>0){return this.store.map(function(word){return lpad(word.toString(2),"0",_this.bitsPerWord)}).join("")}else{return lpad("",0,this.bitsPerWord)}};BitSet.prototype.or=function(set){var pos,wordsInCommon,_ref;if(this===set)return;wordsInCommon=Math.min(this.wordLength(),set.wordLength());for(pos=0,_ref=wordsInCommon-1;0<=_ref?pos<=_ref:pos>=_ref;0<=_ref?pos++:pos--){this.store[pos]|=set.store[pos]}if(wordsInCommon<set.wordLength()){this.store=this.store.concat(set.store.slice(wordsInCommon,set.wordLength()))}return null};BitSet.prototype.and=function(set){var pos,_ref,_ref2,_ref3;if(this===set)return;for(pos=_ref=this.wordLength,_ref2=set.wordLength();_ref<=_ref2?pos<=_ref2:pos>=_ref2;_ref<=_ref2?pos++:pos--){this.store[pos]=0}for(pos=0,_ref3=this.wordLength();0<=_ref3?pos<=_ref3:pos>=_ref3;0<=_ref3?pos++:pos--){this.store[pos]&=set.store[pos]}return null};BitSet.prototype.andNot=function(set){var pos,_ref;for(pos=0,_ref=Math.min(this.wordLength(),set.wordLength)-1;0<=_ref?pos<=_ref:pos>=_ref;0<=_ref?pos++:pos--){this.store[pos]&=~set.store[pos]}return null};BitSet.prototype.xor=function(set){var pos,_ref;if(this===set)return;for(pos=0,_ref=this.wordLength();0<=_ref?pos<=_ref:pos>=_ref;0<=_ref?pos++:pos--){this.store[pos]^=set.store[pos]}return null};return BitSet}()}).call(this)},{}]},{},[]);var BitSet=require("bitset");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": {
"bitset": "0.3.0"
}
}
<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