예제 #1
0
        public static T Get <T>(UnsafeArray *array, long index) where T : unmanaged
        {
            Assert.Check(array != null);
            Assert.Check(typeof(T).TypeHandle.Value == array->_typeHandle);

            // cast to uint trick, which eliminates < 0 check
            if ((uint)index >= (uint)array->_length)
            {
                throw new IndexOutOfRangeException(index.ToString());
            }

            return(*((T *)array->_buffer + index));
        }
예제 #2
0
        public static int IndexOf <T>(UnsafeArray *array, T item) where T : unmanaged, IEquatable <T>
        {
            Assert.Check(array != null);
            Assert.Check(typeof(T).TypeHandle.Value == array->_typeHandle);

            for (int i = 0; i < Length(array); ++i)
            {
                if (Get <T>(array, i).Equals(item))
                {
                    return(i);
                }
            }

            return(-1);
        }
        public static int LastIndexOf <T>(UnsafeArray *array, T item) where T : unmanaged, IEquatable <T>
        {
            UDebug.Assert(array != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == array->_typeHandle);

            for (int i = GetLength(array) - 1; i >= 0; --i)
            {
                if (Get <T>(array, i).Equals(item))
                {
                    return(i);
                }
            }

            return(-1);
        }
        public static int FindLastIndex <T>(UnsafeArray *array, Func <T, bool> predicate) where T : unmanaged
        {
            UDebug.Assert(array != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == array->_typeHandle);

            for (int i = GetLength(array) - 1; i >= 0; --i)
            {
                if (predicate(Get <T>(array, i)))
                {
                    return(i);
                }
            }

            return(-1);
        }
예제 #5
0
        public static int FindIndex <T>(UnsafeArray *array, Func <T, bool> predicate) where T : unmanaged
        {
            Assert.Check(array != null);
            Assert.Check(typeof(T).TypeHandle.Value == array->_typeHandle);

            for (int i = 0; i < Length(array); ++i)
            {
                if (predicate(Get <T>(array, i)))
                {
                    return(i);
                }
            }

            return(-1);
        }
예제 #6
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)));
            }
        }
예제 #7
0
        public static NativeDynamicArray Alloc <T>(Allocator allocator, uint newLength = 0) where T : struct
        {
            unsafe
            {
#if DEBUG && !PROFILE_SVELTO
                var rtnStruc = new NativeDynamicArray {
                    _hashType = TypeHash <T> .hash
                };
#else
                NativeDynamicArray rtnStruc = default;
#endif
                UnsafeArray *listData = (UnsafeArray *)MemoryUtilities.Alloc <UnsafeArray>(1, allocator);

                //clear to nullify the pointers
                //MemoryUtilities.MemClear((IntPtr) listData, structSize);

                rtnStruc._allocator = allocator;
                listData->Realloc <T>(newLength, allocator);

                rtnStruc._list = listData;

                return(rtnStruc);
            }
        }
예제 #8
0
        public static NativeDynamicArray Alloc <T>(Allocator allocator, uint newLength = 0) where T : struct
        {
            unsafe
            {
                var rtnStruc = new NativeDynamicArray();
#if DEBUG && !PROFILE_SVELTO
                rtnStruc.hashType = TypeHash <T> .hash;
#endif
                var sizeOf = MemoryUtilities.SizeOf <T>();

                uint         pointerSize = (uint)MemoryUtilities.SizeOf <UnsafeArray>();
                UnsafeArray *listData    = (UnsafeArray *)MemoryUtilities.Alloc(pointerSize, allocator);

                //clear to nullify the pointers
                MemoryUtilities.MemClear((IntPtr)listData, pointerSize);

                listData->allocator = allocator;
                listData->Realloc((uint)(newLength * sizeOf));

                rtnStruc._list = listData;

                return(rtnStruc);
            }
        }
예제 #9
0
 public void Dispose()
 {
     UnsafeArray.Free(m_inner);
     m_inner = null;
 }
예제 #10
0
 internal static void *GetBuffer(UnsafeArray *array)
 {
     return(array->_buffer);
 }
예제 #11
0
 internal static IntPtr GetTypeHandle(UnsafeArray *array)
 {
     return(array->_typeHandle);
 }
예제 #12
0
 internal Enumerator(UnsafeArray *array)
 {
     _index   = 0;
     _array   = array;
     _current = null;
 }
예제 #13
0
 public static void *GetBuffer(UnsafeArray *array)
 {
     return(array->_buffer);
 }
예제 #14
0
 internal Iterator(UnsafeArray *array)
 {
     _index   = -1;
     _array   = array;
     _current = null;
 }
예제 #15
0
 public static void Free(UnsafeArray *array)
 {
     AllocHelper.Free(array);
 }
예제 #16
0
 public static T Get <T>(UnsafeArray *array, long index) where T : unmanaged
 {
     return(*GetPtr <T>(array, index));
 }
예제 #17
0
 public static T *GetPtr <T>(UnsafeArray *array, int index) where T : unmanaged
 {
     return(GetPtr <T>(array, (long)index));
 }
예제 #18
0
 public static int GetLength(UnsafeArray *array)
 {
     UDebug.Assert(array != null);
     return(array->_length);
 }
예제 #19
0
 public static void Clear <T>(UnsafeArray *array) where T : unmanaged
 {
     Memory.ZeroMem(array->_buffer, array->_length * sizeof(T));
 }
예제 #20
0
 public static int Length(UnsafeArray *array)
 {
     Assert.Check(array != null);
     return(array->_length);
 }
예제 #21
0
 public NativeArray(int length)
 {
     m_inner = UnsafeArray.Allocate <T>(length);
 }
예제 #22
0
        public NativeArray(NativeArray <T> array)
        {
            m_inner = UnsafeArray.Allocate <T>(array.Length);

            Copy(array, this);
        }
예제 #23
0
 public static void Set <T>(UnsafeArray *array, int index, T value) where T : unmanaged
 {
     Set(array, (long)index, value);
 }
예제 #24
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);
        }
예제 #25
0
 public static Iterator <T> GetIterator <T>(UnsafeArray *array) where T : unmanaged
 {
     return(new Iterator <T>(array));
 }
예제 #26
0
 public static ref T GetRef <T>(UnsafeArray *array, long index) where T : unmanaged
 {
     return(ref *GetPtr <T>(array, index));
 }
예제 #27
0
 public static void Free(UnsafeArray *array)
 {
     Native.Free(array);
 }