Exemplo n.º 1
0
        public static T[] ToArray <T>(this AiNativeList <T> list) where T : unmanaged
        {
            T[] array  = new T[list.Length];
            int length = list.Length * UnsafeCollectionUtility.SizeOf <T>();

            MemoryCopy.Copy((T *)list.GetUnsafePtr(), array, length);
            return(array);
        }
Exemplo n.º 2
0
        public T ReadElement <T>(int index) where T : unmanaged
        {
#if DEBUG || ENABLE_UNITY_COLLECTIONS_CHECKS
            RequireCreated();
            RequireIndexInBounds(index);
#endif
            return(UnsafeCollectionUtility.ReadArrayElement <T>(Ptr, index));
        }
Exemplo n.º 3
0
        public static byte[] ToByteArray <T>(this AiNativeList <T> list) where T : unmanaged
        {
            int bytesToCopy = list.Length * UnsafeCollectionUtility.SizeOf <T>();

            byte[] target = new byte[bytesToCopy];
            MemoryCopy.Copy(list.GetUnsafePtr(), 0, target, 0, bytesToCopy);
            return(target);
        }
Exemplo n.º 4
0
        public static byte[] ToByteArray <T>(this T[] source) where T : unmanaged
        {
            int sizeInBytes = source.Length * UnsafeCollectionUtility.SizeOf <T>();

            byte[] target = new byte[sizeInBytes];
            MemoryCopy.Copy(source, target);
            return(target);
        }
Exemplo n.º 5
0
        public void WriteElement <T>(int index, T value) where T : unmanaged
        {
#if DEBUG || ENABLE_UNITY_COLLECTIONS_CHECKS
            RequireCreated();
            RequireIndexInBounds(index);
#endif
            UnsafeCollectionUtility.WriteArrayElement <T>(Ptr, index, value);
        }
Exemplo n.º 6
0
        public AiNativeArray(int length)
        {
            if (length <= 0)
            {
                throw new ArgumentException("Length must be > 0");
            }

            m_Data = UnsafeArrayData.Create(UnsafeCollectionUtility.SizeOf <T>(), length);
        }
Exemplo n.º 7
0
        public static UnsafeArrayData *Create(int sizeOf, int length)
        {
            UnsafeArrayData *arrayData = (UnsafeArrayData *)Marshal.AllocHGlobal(UnsafeCollectionUtility.SizeOf <UnsafeArrayData>());

            var bytesToMalloc = sizeOf * length;

            arrayData->Ptr    = (void *)Marshal.AllocHGlobal(bytesToMalloc);
            arrayData->Length = length;

            return(arrayData);
        }
Exemplo n.º 8
0
 public static int IndexOf <T, U>(void *ptr, int length, U value) where T : unmanaged, IEquatable <U>
 {
     for (int i = 0; i != length; i++)
     {
         if (UnsafeCollectionUtility.ReadArrayElement <T>(ptr, i).Equals(value))
         {
             return(i);
         }
     }
     return(-1);
 }
Exemplo n.º 9
0
        public static T[] ToArray <T>(this byte[] source) where T : unmanaged
        {
            if (source.Length % UnsafeCollectionUtility.SizeOf <T>() != 0)
            {
                throw new ArgumentException("Invalid length for type");
            }

            int size = source.Length / UnsafeCollectionUtility.SizeOf <T>();

            T[] target = new T[size];
            MemoryCopy.Copy <T>(source, target);
            return(target);
        }
Exemplo n.º 10
0
        public static AiNativeArray <T> ToNativeArray <T>(this byte[] source) where T : unmanaged
        {
            if (source.Length % UnsafeCollectionUtility.SizeOf <T>() != 0)
            {
                throw new ArgumentException("Invalid length for type");
            }

            int size = source.Length / UnsafeCollectionUtility.SizeOf <T>();
            AiNativeArray <T> destination = new AiNativeArray <T>(size);

            MemoryCopy.Copy(source, 0, destination.GetUnsafePtr(), 0, source.Length);
            return(destination);
        }
Exemplo n.º 11
0
        public static UnsafeListData *Create(int sizeOf, int initialCapacity)
        {
            UnsafeListData *listData = (UnsafeListData *)Marshal.AllocHGlobal(UnsafeCollectionUtility.SizeOf <UnsafeListData>());

            listData->Length   = 0;
            listData->Capacity = 0;

            if (initialCapacity != 0)
            {
                listData->SetCapacity(sizeOf, initialCapacity);
            }

            return(listData);
        }
Exemplo n.º 12
0
        public static UnsafeListData *CreateFrom <T>(byte[] array) where T : unmanaged
        {
            if (array.Length % UnsafeCollectionUtility.SizeOf <T>() != 0)
            {
                throw new ArgumentException("Invalid length for type");
            }

            int size = array.Length / UnsafeCollectionUtility.SizeOf <T>();

            UnsafeListData *listData = Create(UnsafeCollectionUtility.SizeOf <T>(), size);

            listData->Length = size;

            MemoryCopy.Copy(array, 0, listData->Ptr, 0, array.Length);

            return(listData);
        }
Exemplo n.º 13
0
        public static UnsafeListData *CreateFrom <T>(AiNativeArray <T> array) where T : unmanaged
        {
            UnsafeListData *listData = Create(UnsafeCollectionUtility.SizeOf <T>(), array.Length);

            if (listData->Capacity < array.Length)
            {
                listData->Dispose();
                throw new Exception("Unknown Error");
            }
            listData->Length = array.Length;

            int bytesToCopy = array.Length * UnsafeCollectionUtility.SizeOf <T>();

            Buffer.MemoryCopy(array.GetUnsafePtr(), listData->Ptr, bytesToCopy, bytesToCopy);

            return(listData);
        }
Exemplo n.º 14
0
        private void SetCapacity(int sizeOf, int capacity)
        {
            if (capacity > 0)
            {
                var itemsPerCacheLine = 64 / sizeOf;

                if (capacity < itemsPerCacheLine)
                {
                    capacity = itemsPerCacheLine;
                }

                capacity = UnsafeCollectionUtility.CeilPow2(capacity);
            }

            var newCapacity = capacity;

            if (newCapacity == Capacity)
            {
                return;
            }

            void *newPointer = null;

            if (newCapacity > 0)
            {
                var bytesToMalloc = sizeOf * newCapacity;
                newPointer = (void *)Marshal.AllocHGlobal(bytesToMalloc);

                if (Capacity > 0)
                {
                    var itemsToCopy = newCapacity < Capacity ? newCapacity : Capacity;
                    var bytesToCopy = itemsToCopy * sizeOf;
                    Buffer.MemoryCopy(Ptr, newPointer, bytesToCopy, bytesToCopy);
                }
            }

            if (Capacity > 0)
            {
                Marshal.FreeHGlobal((IntPtr)Ptr);
            }

            Ptr      = newPointer;
            Capacity = newCapacity;
            Length   = Math.Min(Length, Capacity);
        }
Exemplo n.º 15
0
 public AiNativeList(int initialCapacity)
 {
     m_ListData = UnsafeListData.Create(UnsafeCollectionUtility.SizeOf <T>(), initialCapacity);
 }
Exemplo n.º 16
0
 public void RemoveRangeSwapBack <T>(int begin, int end) where T : unmanaged
 {
     RemoveRangeSwapBack(UnsafeCollectionUtility.SizeOf <T>(), begin, end);
 }
Exemplo n.º 17
0
 public void AddRange <T>(UnsafeListData list) where T : unmanaged
 {
     AddRange(UnsafeCollectionUtility.SizeOf <T>(), list.Ptr, list.Length);
 }
Exemplo n.º 18
0
 public void AddRange <T>(void *ptr, int length) where T : unmanaged
 {
     AddRange(UnsafeCollectionUtility.SizeOf <T>(), ptr, length);
 }
Exemplo n.º 19
0
 public void Add <T>(T value) where T : unmanaged
 {
     Resize(UnsafeCollectionUtility.SizeOf <T>(), Length + 1);
     WriteElement(Length - 1, value);
 }