public void BitSetIteratorTest() { UnsafeBitSet *bitSet = UnsafeBitSet.Allocate(8); for (int i = 0; i < 8; i++) { if (i % 2 == 0) { UnsafeBitSet.Set(bitSet, i, true); } } int index = 0; foreach (var set in UnsafeBitSet.GetEnumerator(bitSet)) { if (index % 2 == 0) { Assert.IsTrue(set.set); } index++; } UnsafeBitSet.Free(bitSet); }
public static void Free(UnsafeBitSet *set) { // clear memory *set = default; // free memory AllocHelper.Free(set); }
public static void Free(UnsafeBitSet *set) { // clear memory *set = default; // free memory Native.Free(set); }
public static bool IsSet(UnsafeBitSet *set, int bit) { if ((uint)bit >= (uint)set->_sizeBits) { throw new IndexOutOfRangeException(); } return((set->_bits[bit / WORD_SIZE_BITS] & (WORD_ONE << (bit % WORD_SIZE_BITS))) != WORD_ZERO); }
public static void Clear(UnsafeBitSet *set, int bit) { if ((uint)bit >= (uint)set->_sizeBits) { throw new IndexOutOfRangeException(); } set->_bits[bit / WORD_SIZE_BITS] &= ~(WORD_ONE << (bit % WORD_SIZE_BITS)); }
public static void Set(UnsafeBitSet *set, int bit) { if ((uint)bit >= (uint)set->_sizeBits) { throw new IndexOutOfRangeException(ThrowHelper.ArgumentOutOfRange_Index); } set->_bits[bit / WORD_SIZE_BITS] |= WORD_ONE << (bit % WORD_SIZE_BITS); }
public static void Set(UnsafeBitSet *set, int bit, bool state) { if (state) { Set(set, bit); } else { Clear(set, bit); } }
public static void Xor(UnsafeBitSet *set, UnsafeBitSet *other) { if (set->_sizeBits != other->_sizeBits) { throw new InvalidOperationException(ThrowHelper.Arg_BitSetLengthsDiffer); } for (var i = (set->_sizeBuckets - 1); i >= 0; --i) { set->_bits[i] ^= other->_bits[i]; } }
public static bool AnySet(UnsafeBitSet *set) { for (var i = (set->_sizeBuckets - 1); i >= 0; --i) { if (set->_bits[i] != WORD_ZERO) { return(true); } } return(false); }
public static void Xor(UnsafeBitSet *set, UnsafeBitSet *other) { if (set->_sizeBits != other->_sizeBits) { throw new InvalidOperationException(SET_DIFFERENT_SIZE); } for (var i = (set->_sizeBuckets - 1); i >= 0; --i) { set->_bits[i] ^= other->_bits[i]; } }
public static void Free(UnsafeBitSet *set) { if (set == null) { return; } // clear memory *set = default; // free memory Memory.Free(set); }
public static bool AreEqual(UnsafeBitSet *set, UnsafeBitSet *other) { if (set->_sizeBits != other->_sizeBits) { throw new InvalidOperationException(ThrowHelper.Arg_BitSetLengthsDiffer); } for (var i = (set->_sizeBuckets - 1); i >= 0; --i) { if (set->_bits[i] != other->_bits[i]) { return(false); } } return(true); }
public void TestBitSet() { UnsafeBitSet *bitSet = UnsafeBitSet.Alloc(64); UnsafeBitSet.Set(bitSet, 1); UnsafeBitSet.Set(bitSet, 2); UnsafeBitSet.Set(bitSet, 3); UnsafeBitSet.Set(bitSet, 61); UnsafeArray *setBits = UnsafeArray.Allocate <int>(UnsafeBitSet.Size(bitSet)); var setBitsCount = UnsafeBitSet.GetSetBits(bitSet, setBits); for (int i = 0; i < setBitsCount; i++) { IsTrue(UnsafeBitSet.IsSet(bitSet, UnsafeArray.Get <int>(setBits, i))); } }
public void BitSetEquals() { UnsafeBitSet *bitsetFirst = UnsafeBitSet.Allocate(8); UnsafeBitSet *bitsetSecond = UnsafeBitSet.Allocate(8); for (int i = 0; i < 8; i++) { if (i % 2 == 0) { UnsafeBitSet.Set(bitsetFirst, i); UnsafeBitSet.Set(bitsetSecond, i); } } Assert.IsTrue(UnsafeBitSet.AreEqual(bitsetFirst, bitsetSecond)); UnsafeBitSet.Free(bitsetFirst); }
public void OddSizeSetTest() { UnsafeBitSet *bs = UnsafeBitSet.Allocate(67); Assert.AreEqual(67, UnsafeBitSet.GetSize(bs)); }
public static Enumerator GetEnumerator(UnsafeBitSet *set) { return(new Enumerator(set)); }
public Iterator(UnsafeBitSet *set) { _set = set; _current = -1; }
public static void Clear(UnsafeBitSet *set) { AllocHelper.MemClear(set->_bits, set->_sizeBuckets * WORD_SIZE); }
public static int Size(UnsafeBitSet *set) { return(set->_sizeBits); }
internal Enumerator(UnsafeBitSet *set) { _set = set; _current = -1; }
public static int GetSetBits(UnsafeBitSet *set, UnsafeArray *array) { Assert.Check(UnsafeArray.GetTypeHandle(array) == typeof(int).TypeHandle.Value); if (UnsafeArray.Length(array) < set->_sizeBits) { throw new InvalidOperationException(SET_ARRAY_LESS_CAPACITY); } var setCount = 0; var bitOffset = 0; var arrayBuffer = (int *)UnsafeArray.GetBuffer(array); for (var i = 0; i < set->_sizeBuckets; ++i) { var word64 = set->_bits[i]; if (word64 == WORD_ZERO) { // since we're skipping whole word, step up offset bitOffset += WORD_SIZE_BITS; continue; } var word32Count = 0; NEXT_WORD32: var word32 = *((uint *)word64 + word32Count); if (word32 != 0) { var word16Count = 0; NEXT_WORD16: var word16 = *((ushort *)word32 + word16Count); if (word16 != 0) { var word8Count = 0; NEXT_WORD8: var word8 = *((byte *)word16 + word8Count); if (word8 != 0) { if ((word8 & (1 << 0)) == 1 << 0) { arrayBuffer[setCount++] = (bitOffset + 0); } if ((word8 & (1 << 1)) == 1 << 1) { arrayBuffer[setCount++] = (bitOffset + 1); } if ((word8 & (1 << 2)) == 1 << 2) { arrayBuffer[setCount++] = (bitOffset + 2); } if ((word8 & (1 << 3)) == 1 << 3) { arrayBuffer[setCount++] = (bitOffset + 3); } if ((word8 & (1 << 4)) == 1 << 4) { arrayBuffer[setCount++] = (bitOffset + 4); } if ((word8 & (1 << 5)) == 1 << 5) { arrayBuffer[setCount++] = (bitOffset + 5); } if ((word8 & (1 << 6)) == 1 << 6) { arrayBuffer[setCount++] = (bitOffset + 6); } if ((word8 & (1 << 7)) == 1 << 7) { arrayBuffer[setCount++] = (bitOffset + 7); } } // always step up bitoffset here bitOffset += (WORD_SIZE_BITS / 8); if (word8Count == 0) { ++word8Count; // go back goto NEXT_WORD8; } } else { bitOffset += (WORD_SIZE_BITS / 4); } if (word16Count == 0) { ++word16Count; // go back goto NEXT_WORD16; } } else { bitOffset += (WORD_SIZE_BITS / 2); } if (word32Count == 0) { ++word32Count; // go back goto NEXT_WORD32; } } return(setCount); }
public static Iterator GetIterator(UnsafeBitSet *set) { return(new Iterator(set)); }
public static void Clear(UnsafeBitSet *set) { Native.MemClear(set->_bits, set->_sizeBuckets * WORD_SIZE); }
public static void Clear(UnsafeBitSet *set) { Memory.ZeroMem(set->_bits, set->_sizeBuckets * WORD_SIZE); }