Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public static void Free(UnsafeBitSet *set)
        {
            // clear memory
            *set = default;

            // free memory
            AllocHelper.Free(set);
        }
Ejemplo n.º 3
0
        public static void Free(UnsafeBitSet *set)
        {
            // clear memory
            *set = default;

            // free memory
            Native.Free(set);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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];
            }
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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];
            }
        }
Ejemplo n.º 11
0
        public static void Free(UnsafeBitSet *set)
        {
            if (set == null)
            {
                return;
            }

            // clear memory
            *set = default;

            // free memory
            Memory.Free(set);
        }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 13
0
        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)));
            }
        }
Ejemplo n.º 14
0
        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);
        }
Ejemplo n.º 15
0
        public void OddSizeSetTest()
        {
            UnsafeBitSet *bs = UnsafeBitSet.Allocate(67);

            Assert.AreEqual(67, UnsafeBitSet.GetSize(bs));
        }
Ejemplo n.º 16
0
 public static Enumerator GetEnumerator(UnsafeBitSet *set)
 {
     return(new Enumerator(set));
 }
 public Iterator(UnsafeBitSet *set)
 {
     _set     = set;
     _current = -1;
 }
Ejemplo n.º 18
0
 public static void Clear(UnsafeBitSet *set)
 {
     AllocHelper.MemClear(set->_bits, set->_sizeBuckets * WORD_SIZE);
 }
Ejemplo n.º 19
0
 public static int Size(UnsafeBitSet *set)
 {
     return(set->_sizeBits);
 }
Ejemplo n.º 20
0
 internal Enumerator(UnsafeBitSet *set)
 {
     _set     = set;
     _current = -1;
 }
Ejemplo n.º 21
0
        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);
        }
Ejemplo n.º 22
0
 public static Iterator GetIterator(UnsafeBitSet *set)
 {
     return(new Iterator(set));
 }
Ejemplo n.º 23
0
 public static void Clear(UnsafeBitSet *set)
 {
     Native.MemClear(set->_bits, set->_sizeBuckets * WORD_SIZE);
 }
Ejemplo n.º 24
0
 public static void Clear(UnsafeBitSet *set)
 {
     Memory.ZeroMem(set->_bits, set->_sizeBuckets * WORD_SIZE);
 }