package BitSet import NoWurst import Integer import Annotations public constant BITSET_SIZE = 32 int array pows int array reversePows @compiletime function initPows() pows[0] = 1 var allPows = 1 for i = 1 to BITSET_SIZE - 1 pows[i] = pows[i - 1] * 2 allPows = allPows.bitOr(pows[i]) for i = 0 to BITSET_SIZE - 1 reversePows[i] = allPows.bitXor(pows[i]) init initPows() /** Bitset represents a fixed-size sequence of BITSET_SIZE bits. The bits are contained in a single int. */ public tuple bitset(int val) /** Creates an empty bitset. */ public function emptyBitset() returns bitset return bitset(0) /** Returns the value of the bit with the specified index. */ public function bitset.get(int index) returns bool return this.val.bitAnd(pows[index]) != 0 /** Sets the bit at the specified index to true. */ public function bitset.set(int index) returns bitset return bitset(this.val.bitOr(pows[index])) /** Sets the bit at the specified index to false. */ public function bitset.reset(int index) returns bitset return bitset(this.val.bitAnd(reversePows[index])) /** Sets the bit at the specified index to the specified value. */ public function bitset.set(int index, bool value) returns bitset return value ? this.set(index) : this.reset(index) /** Sets the bit at the specified index to the complement of its current value. */ public function bitset.flip(int index) returns bitset return bitset(this.val.bitXor(pows[index])) /** Returns true if this bitset contains no bits that are set to true. */ public function bitset.isEmpty() returns bool return this.val == 0 /** Returns true if the specified bitset has any bits set to true that are also set to true in this bitset. */ public function bitset.intersects(bitset other) returns bool return this.val.bitAnd(other.val) != 0 /** Performs a logical AND of this bit set with the bit set argument. */ public function bitset.bitAnd(bitset other) returns bitset return bitset(this.val.bitAnd(other.val)) /** Performs a logical OR of this bit set with the bit set argument. */ public function bitset.bitOr(bitset other) returns bitset return bitset(this.val.bitOr(other.val)) /** Performs a logical XOR of this bit set with the bit set argument. */ public function bitset.bitXor(bitset other) returns bitset return bitset(this.val.bitXor(other.val)) /** Returns an integer representation of the data. */ public function bitset.toInt() returns int return this.val // Conversion methods for generics public function bitsetToIndex(bitset object) returns int return object.toInt() public function bitsetFromIndex(int index) returns bitset return bitset(index)