forked from rawify/BitSet.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitset.min.js
12 lines (11 loc) · 2.54 KB
/
bitset.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*
BitSet.js v1.0.0 05/03/2014
http://www.xarg.org/2014/03/javascript-bit-array/
Copyright (c) 2014, Robert Eisele ([email protected])
Dual licensed under the MIT or GPL Version 2 licenses.
*/
function BitSet(f,k){function l(a,b,c,e){void 0===c&&(void 0===b?(b=0,c=31*d-1):c=b);if(0>b||c<b||31*d<=c)return null;for(var f=d;f--;){var g=Math.max(b,31*f),k=Math.min(c,30+31*f);g<=k&&(g=~(1<<1+k%31)+(1<<g%31),a[f]=0===e?a[f]&g:a[f]^~g)}return a}void 0===f?f=31:"string"===typeof f&&(f=f.length);1!==k&&(k=0);for(var d=Math.ceil(f/31),g=d;g--;)this[g]=k;if("string"===typeof f)for(g=f.length;g--;)this.set(g,f.charAt(g));this.size=31*d;this.length=d;this.and=function(a){if(a instanceof BitSet)for(var b=d;b--;)this[b]&=
a[b]||0;return this};this.or=function(a){if(a instanceof BitSet)for(var b=d;b--;)this[b]|=a[b]||0;return this};this.nand=function(a){if(a instanceof BitSet)for(var b=d;b--;)this[b]=~(this[b]&(a[b]||0));return this};this.nor=function(a){if(a instanceof BitSet)for(var b=d;b--;)this[b]=~(this[b]|(a[b]||0));return this};this.not=function(){for(var a=d;a--;)this[a]=~this[a];return this};this.xor=function(a){if(a instanceof BitSet)for(var b=d;b--;)this[b]^=a[b]||0;return this};this.equals=function(a){if(a instanceof BitSet){if(a.length!==
d)return!1;for(var b=d;b--;)if(a[b]!==this[b])return!1}else return!1;return!0};this.clone=function(){for(var a=new BitSet(this.size),b=d;b--;)a[b]=this[b];return a};this.isEmpty=function(){for(var a=d;a--;)if(0!==this[a])return!1;return!0};this.toString=function(a){for(var b="",c=d;c--;){c+1<d&&void 0!==a&&(b+=String(a));var e=this[c].toString(2),b=b+Array(32-e.length).join("0"),b=b+e}return b};this.cardinality=function(){for(var a,b=0,c=d;c--;)for(a=this[c];a;a&=a-1,b++);return b};this.msb=function(){for(var a=
d;a--;){var b=this[a],c=0;if(b){for(;b>>=1;c++);return 31*a+c}}return 0};this.set=function(a,b){void 0===b&&(b=1);if(0<=a&&a<31*d){var c=a/31|0;this[c]^=1<<a%31&(-(b&1)^this[c]);return this}return null};this.setRange=function(a,b,c){if(a<=b&&0<=a&&b<31*d){if("string"===typeof c){if(b-a!==c.length)return null;for(var e=0;e<c.length;e++)this.set(e+a,c.charAt(c.length-e-1))}else for(void 0===c&&(c=1),e=a;e<=b;e++)this.set(e,c);return this}return null};this.get=function(a){return 0<=a&&a<31*d?this[a/
31|0]>>a%31&1:null};this.getRange=function(a,b){if(a<=b&&0<=a&&b<31*d){for(var c=new BitSet(b-a+1),e=a;e<=b;e++)c.set(e-a,this.get(e));return c}return null};this.clear=function(a,b){return l(this,a,b,0)};this.flip=function(a,b){return l(this,a,b,1)}}"undefined"!==typeof module&&module.exports&&(module.exports.BitSet=BitSet);