BitSet
JavaのBitSetみたいなのが必要だったので書いてみました。
function BitSet( defaultValue ) { this.value = defaultValue ? defaultValue : 0 ; } BitSet.prototype = { on: function( index ) { if ( index < 0 || index >= 32 ) { throw "out of bounds." } this.value |= 1 << index; return this; }, off: function( index ) { if ( index < 0 || index >= 32 ) { throw "out of bounds." } this.value &= ~(1 << index); return this; }, flip: function( index ) { this.isOn(index) ? this.off(index) : this.on(index) ; return this; }, isOn: function( index ) { if ( index < 0 || index >= 32 ) { throw "out of bounds." } return (this.value & (1 << index)) != 0; }, equals: function( bitSet ) { return bitSet.value == this.value; }, and : function( bitSet ) { return new BitSet(this.value & bitSet.value); }, or : function( bitSet ) { return new BitSet(this.value | bitSet.value); }, xor : function( bitSet ) { return new BitSet(this.value ^ bitSet.value); }, not : function() { return new BitSet(~this.value); }, toString: function() { var str = ""; for (var i=31; i >= 0; i-- ) { str += this.isOn(i) ? "1" : "0"; } return str; } }
文系?プログラマなのでこういうのは苦手です。変なことしてたらゴメンナサイ。一応テストケースも書きました。
→テストケースを実行
var BitSetTest = new YAHOO.tool.TestCase({ name: "BitSetTest", setUp: function () {}, tearDown: function () {}, /** * toStringのテスト */ testToString: function () { var assert = YAHOO.util.Assert; var b = new BitSet(0); assert.areEqual( "00000000000000000000000000000000", b.toString() ); b = new BitSet(1); assert.areEqual( "00000000000000000000000000000001", b.toString() ); b = new BitSet(2); assert.areEqual( "00000000000000000000000000000010", b.toString() ); b = new BitSet(3); assert.areEqual( "00000000000000000000000000000011", b.toString() ); b = new BitSet(); assert.areEqual( "00000000000000000000000000000000", b.toString() ); }, /** * equalsのテスト */ testEquals: function () { var assert = YAHOO.util.Assert; var b = new BitSet(0); var b2 = new BitSet(1); var b3 = new BitSet(3); assert.isTrue( b.equals( new BitSet(0) ) ); assert.isFalse( b2.equals( new BitSet(0) ) ); assert.isFalse( b3.equals( new BitSet(0) ) ); assert.isFalse( b.equals( new BitSet(0).on(0) ) ); assert.isTrue( b2.equals( new BitSet(0).on(0) ) ); assert.isFalse( b3.equals( new BitSet(0).on(0) ) ); assert.isFalse( b.equals( new BitSet(0).on(1) ) ); assert.isFalse( b2.equals( new BitSet(0).on(1) ) ); assert.isFalse( b3.equals( new BitSet(0).on(1) ) ); assert.isFalse( b.equals( new BitSet(0).on(0).on(1) ) ); assert.isFalse( b2.equals( new BitSet(0).on(0).on(1) ) ); assert.isTrue( b3.equals( new BitSet(0).on(0).on(1) ) ); }, /** * on,offのテスト */ testOnOff: function () { var assert = YAHOO.util.Assert; var b = new BitSet(0); assert.areEqual( "00000000000000000000000000000000", b.toString() ); assert.areEqual( false, b.isOn(0), "1-1" ); assert.areEqual( false, b.isOn(1), "1-2" ); assert.areEqual( false, b.isOn(2), "1-3" ); assert.areEqual( false, b.isOn(3), "1-4" ); assert.areEqual( false, b.isOn(4), "1-5" ); assert.areEqual( false, b.isOn(5), "1-6" ); assert.areEqual( false, b.isOn(6), "1-7" ); b.on(0); assert.areEqual( "00000000000000000000000000000001", b.toString() ); assert.areEqual( true, b.isOn(0), "2-1" ); assert.areEqual( false, b.isOn(1), "2-2" ); assert.areEqual( false, b.isOn(2), "2-3" ); assert.areEqual( false, b.isOn(3), "2-4" ); assert.areEqual( false, b.isOn(4), "2-5" ); assert.areEqual( false, b.isOn(5), "2-6" ); assert.areEqual( false, b.isOn(6), "2-7" ); b.on(1); assert.areEqual( "00000000000000000000000000000011", b.toString() ); assert.areEqual( true, b.isOn(0), "3-1" ); assert.areEqual( true, b.isOn(1), "3-2" ); assert.areEqual( false, b.isOn(2), "3-3" ); assert.areEqual( false, b.isOn(3), "3-4" ); assert.areEqual( false, b.isOn(4), "3-5" ); assert.areEqual( false, b.isOn(5), "3-6" ); assert.areEqual( false, b.isOn(6), "3-7" ); b.on(5); assert.areEqual( "00000000000000000000000000100011", b.toString() ); assert.areEqual( true, b.isOn(0), "4-1" ); assert.areEqual( true, b.isOn(1), "4-2" ); assert.areEqual( false, b.isOn(2), "4-3" ); assert.areEqual( false, b.isOn(3), "4-4" ); assert.areEqual( false, b.isOn(4), "4-5" ); assert.areEqual( true, b.isOn(5), "4-6" ); assert.areEqual( false, b.isOn(6), "4-7" ); b.off(0); assert.areEqual( "00000000000000000000000000100010", b.toString() ); assert.areEqual( false, b.isOn(0), "5-1" ); assert.areEqual( true, b.isOn(1), "5-2" ); assert.areEqual( false, b.isOn(2), "5-3" ); assert.areEqual( false, b.isOn(3), "5-4" ); assert.areEqual( false, b.isOn(4), "5-5" ); assert.areEqual( true, b.isOn(5), "5-6" ); assert.areEqual( false, b.isOn(6), "5-7" ); b.off(1); assert.areEqual( "00000000000000000000000000100000", b.toString() ); assert.areEqual( false, b.isOn(0), "6-1" ); assert.areEqual( false, b.isOn(1), "6-2" ); assert.areEqual( false, b.isOn(2), "6-3" ); assert.areEqual( false, b.isOn(3), "6-4" ); assert.areEqual( false, b.isOn(4), "6-5" ); assert.areEqual( true, b.isOn(5), "6-6" ); assert.areEqual( false, b.isOn(6), "6-7" ); b.off(5); assert.areEqual( "00000000000000000000000000000000", b.toString() ); assert.areEqual( false, b.isOn(0), "7-1" ); assert.areEqual( false, b.isOn(1), "7-2" ); assert.areEqual( false, b.isOn(2), "7-3" ); assert.areEqual( false, b.isOn(3), "7-4" ); assert.areEqual( false, b.isOn(4), "7-5" ); assert.areEqual( false, b.isOn(5), "7-6" ); assert.areEqual( false, b.isOn(6), "7-7" ); }, /** * flipのテスト */ testFlip: function () { var assert = YAHOO.util.Assert; var b = new BitSet(0); assert.areEqual( "00000000000000000000000000000000", b.toString() ); b.flip(0); assert.areEqual( "00000000000000000000000000000001", b.toString() ); assert.areEqual( true, b.isOn(0), "2-1" ); assert.areEqual( false, b.isOn(1), "2-2" ); assert.areEqual( false, b.isOn(2), "2-3" ); assert.areEqual( false, b.isOn(3), "2-4" ); assert.areEqual( false, b.isOn(4), "2-5" ); assert.areEqual( false, b.isOn(5), "2-6" ); assert.areEqual( false, b.isOn(6), "2-7" ); b.flip(1); assert.areEqual( "00000000000000000000000000000011", b.toString() ); assert.areEqual( true, b.isOn(0), "3-1" ); assert.areEqual( true, b.isOn(1), "3-2" ); assert.areEqual( false, b.isOn(2), "3-3" ); assert.areEqual( false, b.isOn(3), "3-4" ); assert.areEqual( false, b.isOn(4), "3-5" ); assert.areEqual( false, b.isOn(5), "3-6" ); assert.areEqual( false, b.isOn(6), "3-7" ); b.flip(5); assert.areEqual( "00000000000000000000000000100011", b.toString() ); assert.areEqual( true, b.isOn(0), "4-1" ); assert.areEqual( true, b.isOn(1), "4-2" ); assert.areEqual( false, b.isOn(2), "4-3" ); assert.areEqual( false, b.isOn(3), "4-4" ); assert.areEqual( false, b.isOn(4), "4-5" ); assert.areEqual( true, b.isOn(5), "4-6" ); assert.areEqual( false, b.isOn(6), "4-7" ); b.flip(0); assert.areEqual( "00000000000000000000000000100010", b.toString() ); assert.areEqual( false, b.isOn(0), "5-1" ); assert.areEqual( true, b.isOn(1), "5-2" ); assert.areEqual( false, b.isOn(2), "5-3" ); assert.areEqual( false, b.isOn(3), "5-4" ); assert.areEqual( false, b.isOn(4), "5-5" ); assert.areEqual( true, b.isOn(5), "5-6" ); assert.areEqual( false, b.isOn(6), "5-7" ); b.flip(1); assert.areEqual( "00000000000000000000000000100000", b.toString() ); assert.areEqual( false, b.isOn(0), "6-1" ); assert.areEqual( false, b.isOn(1), "6-2" ); assert.areEqual( false, b.isOn(2), "6-3" ); assert.areEqual( false, b.isOn(3), "6-4" ); assert.areEqual( false, b.isOn(4), "6-5" ); assert.areEqual( true, b.isOn(5), "6-6" ); assert.areEqual( false, b.isOn(6), "6-7" ); b.flip(5); assert.areEqual( "00000000000000000000000000000000", b.toString() ); assert.areEqual( false, b.isOn(0), "7-1" ); assert.areEqual( false, b.isOn(1), "7-2" ); assert.areEqual( false, b.isOn(2), "7-3" ); assert.areEqual( false, b.isOn(3), "7-4" ); assert.areEqual( false, b.isOn(4), "7-5" ); assert.areEqual( false, b.isOn(5), "7-6" ); assert.areEqual( false, b.isOn(6), "7-7" ); }, /** * and,or,xor,notのテスト */ testAndOr: function () { var assert = YAHOO.util.Assert; var b = new BitSet(0); assert.areEqual( "00000000000000000000000000000000", b.toString() ); b = b.or( new BitSet(3) ); assert.areEqual( "00000000000000000000000000000011", b.toString() ); assert.areEqual( true, b.isOn(0) ); assert.areEqual( true, b.isOn(1) ); assert.areEqual( false, b.isOn(2) ); assert.areEqual( false, b.isOn(3) ); assert.areEqual( false, b.isOn(4) ); assert.areEqual( false, b.isOn(5) ); assert.areEqual( false, b.isOn(6) ); b = b.or( new BitSet(9) ); assert.areEqual( "00000000000000000000000000001011", b.toString() ); assert.areEqual( true, b.isOn(0) ); assert.areEqual( true, b.isOn(1) ); assert.areEqual( false, b.isOn(2) ); assert.areEqual( true, b.isOn(3) ); assert.areEqual( false, b.isOn(4) ); assert.areEqual( false, b.isOn(5) ); assert.areEqual( false, b.isOn(6) ); b = b.and( new BitSet(7) ); assert.areEqual( "00000000000000000000000000000011", b.toString() ); assert.areEqual( true, b.isOn(0) ); assert.areEqual( true, b.isOn(1) ); assert.areEqual( false, b.isOn(2) ); assert.areEqual( false, b.isOn(3) ); assert.areEqual( false, b.isOn(4) ); assert.areEqual( false, b.isOn(5) ); assert.areEqual( false, b.isOn(6) ); b = b.and( new BitSet(2) ); assert.areEqual( "00000000000000000000000000000010", b.toString() ); assert.areEqual( false, b.isOn(0) ); assert.areEqual( true, b.isOn(1) ); assert.areEqual( false, b.isOn(2) ); assert.areEqual( false, b.isOn(3) ); assert.areEqual( false, b.isOn(4) ); assert.areEqual( false, b.isOn(5) ); assert.areEqual( false, b.isOn(6) ); b = b.xor( new BitSet(5) ); assert.areEqual( "00000000000000000000000000000111", b.toString() ); assert.areEqual( true, b.isOn(0) ); assert.areEqual( true, b.isOn(1) ); assert.areEqual( true, b.isOn(2) ); assert.areEqual( false, b.isOn(3) ); assert.areEqual( false, b.isOn(4) ); assert.areEqual( false, b.isOn(5) ); assert.areEqual( false, b.isOn(6) ); b = b.xor( new BitSet(5) ); assert.areEqual( "00000000000000000000000000000010", b.toString() ); assert.areEqual( false, b.isOn(0) ); assert.areEqual( true, b.isOn(1) ); assert.areEqual( false, b.isOn(2) ); assert.areEqual( false, b.isOn(3) ); assert.areEqual( false, b.isOn(4) ); assert.areEqual( false, b.isOn(5) ); assert.areEqual( false, b.isOn(6) ); b.on(2).on(4); assert.areEqual( "00000000000000000000000000010110", b.toString() ); b = b.not(); assert.areEqual( "11111111111111111111111111101001", b.toString() ); assert.areEqual( true, b.isOn(0) ); assert.areEqual( false, b.isOn(1) ); assert.areEqual( false, b.isOn(2) ); assert.areEqual( true, b.isOn(3) ); assert.areEqual( false, b.isOn(4) ); assert.areEqual( true, b.isOn(5) ); assert.areEqual( true, b.isOn(6) ); b = b.not(); assert.areEqual( "00000000000000000000000000010110", b.toString() ); assert.areEqual( false, b.isOn(0) ); assert.areEqual( true, b.isOn(1) ); assert.areEqual( true, b.isOn(2) ); assert.areEqual( false, b.isOn(3) ); assert.areEqual( true, b.isOn(4) ); assert.areEqual( false, b.isOn(5) ); assert.areEqual( false, b.isOn(6) ); } });