//------------------------------------------------------------------------------ // // This code was generated by a tool. // // TextTransform Samples/Packages/com.unity.collections/Unity.Collections/FixedList.tt // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // //------------------------------------------------------------------------------ using System.Collections.Generic; using System.Collections; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System; using Unity.Collections.LowLevel.Unsafe; using Unity.Mathematics; using UnityEngine.Internal; using UnityEngine; #if UNITY_PROPERTIES_EXISTS using Unity.Properties; #endif namespace Unity.Collections { [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(FixedBytes30) })] [Serializable] internal struct FixedList : INativeList where T : unmanaged where U : unmanaged { [SerializeField] internal ushort length; [SerializeField] internal U buffer; /// /// The current number of items in this list. /// /// The current number of items in this list. [CreateProperty] public int Length { get => length; set { FixedList.CheckResize(value); length = (ushort)value; } } /// /// A property in order to display items in the Entity Inspector. /// [CreateProperty] IEnumerable Elements => this.ToArray(); /// /// Whether the list is empty. /// /// True if this string has no characters or if the container has not been constructed. public bool IsEmpty => Length == 0; internal int LengthInBytes => Length * UnsafeUtility.SizeOf(); unsafe internal byte* Buffer { get { fixed(U* u = &buffer) return (byte*)u + FixedList.PaddingBytes(); } } /// /// The number of elements that can fit in this list. /// /// The number of elements that can fit in this list. /// The capacity of a FixedList cannot be changed. The setter is included only for conformity with . /// Thrown if the new value does not match the current capacity. public int Capacity { get { return FixedList.Capacity(); } set { CollectionHelper.CheckCapacityInRange(value, Length); } } /// /// The element at a given index. /// /// An index. /// The value to store at the index. /// Thrown if the index is out of bounds. public T this[int index] { get { CollectionHelper.CheckIndexInRange(index, length); unsafe { return UnsafeUtility.ReadArrayElement(Buffer, CollectionHelper.AssumePositive(index)); } } set { CollectionHelper.CheckIndexInRange(index, length); unsafe { UnsafeUtility.WriteArrayElement(Buffer, CollectionHelper.AssumePositive(index), value); } } } /// /// Returns the element at a given index. /// /// An index. /// A reference to the element at the index. public ref T ElementAt(int index) { CollectionHelper.CheckIndexInRange(index, length); unsafe { return ref UnsafeUtility.ArrayElementAsRef(Buffer, index); } } /// /// Returns the hash code of this list. /// /// /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash. /// The hash code of this list. public override int GetHashCode() { unsafe { return (int)CollectionHelper.Hash(Buffer, LengthInBytes); } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as (because a fixed list is never resized). /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void Add(in T item) { this[Length++] = item; } /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Remember that a fixed list is never resized. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRange(void* ptr, int length) { T* data = (T*)ptr; for (var i = 0; i < length; ++i) { this[Length++] = data[i]; } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Included only for consistency with the other list types. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void AddNoResize(in T item) => Add(item); /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Included only for consistency with the other list types. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length); /// /// Sets the length to 0. /// /// Does *not* zero out the bytes. public void Clear() { Length = 0; } /// /// Shifts elements toward the end of this list, increasing its length. /// /// /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle. /// /// The length is increased by `end - begin`. /// /// If `end` equals `begin`, the method does nothing. /// /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth. /// /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior. /// /// The index of the first element that will be shifted up. /// The index where the first shifted element will end up. /// Thrown if the new length exceeds the capacity. public void InsertRangeWithBeginEnd(int begin, int end) { int items = end - begin; if(items < 1) return; int itemsToCopy = length - begin; Length += items; if(itemsToCopy < 1) return; int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf(); unsafe { byte *b = Buffer; byte *dest = b + end * UnsafeUtility.SizeOf(); byte *src = b + begin * UnsafeUtility.SizeOf(); UnsafeUtility.MemMove(dest, src, bytesToCopy); } } /// /// Inserts a single element at an index. Increments the length by 1. /// /// The index at which to insert the element. /// The element to insert. /// Thrown if the index is out of bounds. public void Insert(int index, in T item) { InsertRangeWithBeginEnd(index, index+1); this[index] = item; } /// /// Copies the last element of this list to an index. Decrements the length by 1. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// The index to overwrite with the last element. /// Thrown if the index is out of bounds. public void RemoveAtSwapBack(int index) { RemoveRangeSwapBack(index, 1); } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// /// Copies the last `count`-numbered elements to the range starting at `index`. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// /// Does nothing if the count is less than 1. /// /// The first index of the destination range. /// The number of elements to copy and the amount by which to decrement the length. /// Thrown if the index is out of bounds. public void RemoveRangeSwapBack(int index, int count) { if (count > 0) { int copyFrom = math.max(Length - count, index + count); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Truncates the list by replacing the item at the specified index range with the items from the end the list. The list /// is shortened by number of elements in range. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin); /// /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1. /// /// The index of the element to remove. /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element. /// /// Thrown if the index is out of bounds. public void RemoveAt(int index) { RemoveRange(index, 1); } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements. /// /// The first index of the range to remove. /// The number of elements to remove. /// Thrown if the index is out of bounds. public void RemoveRange(int index, int count) { if (count > 0) { int copyFrom = math.min(index + count, Length); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Truncates the list by removing the items at the specified index range, and shifting all remaining items to replace removed items. The list /// is shortened by number of elements in range. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`. /// /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin); /// /// Returns a managed array that is a copy of this list. /// /// A managed array that is a copy of this list. [NotBurstCompatible] public T[] ToArray() { var result = new T[Length]; unsafe { byte* s = Buffer; fixed(T* d = result) UnsafeUtility.MemCpy(d, s, LengthInBytes); } return result; } /// /// Returns an array that is a copy of this list. /// /// The allocator to use. /// An array that is a copy of this list. public NativeArray ToNativeArray(AllocatorManager.AllocatorHandle allocator) { unsafe { var copy = CollectionHelper.CreateNativeArray(Length, allocator, NativeArrayOptions.UninitializedMemory); UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes); return copy; } } } [BurstCompatible] struct FixedList { [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] internal static int PaddingBytes() where T : struct { return math.max(0, math.min(6, (1 << math.tzcnt(UnsafeUtility.SizeOf())) - 2)); } [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] internal static int StorageBytes() where BUFFER : struct where T : struct { return UnsafeUtility.SizeOf() - PaddingBytes(); } [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] internal static int Capacity() where BUFFER : struct where T : struct { return StorageBytes() / UnsafeUtility.SizeOf(); } [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")] internal static void CheckResize(int newLength) where BUFFER : struct where T : struct { var Capacity = Capacity(); if (newLength < 0 || newLength > Capacity) throw new IndexOutOfRangeException($"NewLength {newLength} is out of range of '{Capacity}' Capacity."); } } [Obsolete("Renamed to FixedList32Bytes (UnityUpgradable) -> FixedList32Bytes", true)] public struct FixedList32 where T : unmanaged {} /// /// An unmanaged, resizable list whose content is all stored directly in the 32-byte struct. Useful for small lists. /// /// The type of the elements. [Serializable] [DebuggerTypeProxy(typeof(FixedList32BytesDebugView<>))] [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public struct FixedList32Bytes : INativeList , IEnumerable // Used by collection initializers. , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> where T : unmanaged { [SerializeField] internal ushort length; [SerializeField] internal FixedBytes30 buffer; /// /// The current number of items in this list. /// /// The current number of items in this list. [CreateProperty] public int Length { get => length; set { FixedList.CheckResize(value); length = (ushort)value; } } /// /// A property in order to display items in the Entity Inspector. /// [CreateProperty] IEnumerable Elements => this.ToArray(); /// /// Whether this list is empty. /// /// True if this string has no characters or if the container has not been constructed. public bool IsEmpty => Length == 0; internal int LengthInBytes => Length * UnsafeUtility.SizeOf(); unsafe internal byte* Buffer { get { fixed(byte* b = &buffer.offset0000.byte0000) return b + FixedList.PaddingBytes(); } } /// /// The number of elements that can fit in this list. /// /// The number of elements that can fit in this list. /// The capacity of a FixedList cannot be changed. The setter is included only for conformity with . /// Thrown if the new value does not match the current capacity. public int Capacity { get { return FixedList.Capacity(); } set { CollectionHelper.CheckCapacityInRange(value, Length); } } /// /// The element at a given index. /// /// An index. /// The value to store at the index. /// Thrown if the index is out of bounds. public T this[int index] { get { CollectionHelper.CheckIndexInRange(index, length); unsafe { return UnsafeUtility.ReadArrayElement(Buffer, CollectionHelper.AssumePositive(index)); } } set { CollectionHelper.CheckIndexInRange(index, length); unsafe { UnsafeUtility.WriteArrayElement(Buffer, CollectionHelper.AssumePositive(index), value); } } } /// /// Returns the element at a given index. /// /// An index. /// The list element at the index. public ref T ElementAt(int index) { CollectionHelper.CheckIndexInRange(index, length); unsafe { return ref UnsafeUtility.ArrayElementAsRef(Buffer, index); } } /// /// Returns the hash code of this list. /// /// /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash. /// The hash code of this list. public override int GetHashCode() { unsafe { return (int)CollectionHelper.Hash(Buffer, LengthInBytes); } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Remember that a fixed list is never resized. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void Add(in T item) { this[Length++] = item; } /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Remember that a fixed list is never resized. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRange(void* ptr, int length) { T* data = (T*)ptr; for (var i = 0; i < length; ++i) { this[Length++] = data[i]; } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Included only for consistency with the other list types. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void AddNoResize(in T item) => Add(item); /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Included only for consistency with the other list types. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length); /// /// Sets the length to 0. /// /// Does *not* zero out the bytes. public void Clear() { Length = 0; } /// /// Shifts elements toward the end of this list, increasing its length. /// /// /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle. /// /// The length is increased by `end - begin`. /// /// If `end` equals `begin`, the method does nothing. /// /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth. /// /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior. /// /// The index of the first element that will be shifted up. /// The index where the first shifted element will end up. /// Thrown if the new length exceeds the capacity. public void InsertRangeWithBeginEnd(int begin, int end) { int items = end - begin; if(items < 1) return; int itemsToCopy = length - begin; Length += items; if(itemsToCopy < 1) return; int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf(); unsafe { byte *b = Buffer; byte *dest = b + end * UnsafeUtility.SizeOf(); byte *src = b + begin * UnsafeUtility.SizeOf(); UnsafeUtility.MemMove(dest, src, bytesToCopy); } } /// /// Inserts a single element at an index. Increments the length by 1. /// /// The index at which to insert the element. /// The element to insert. /// Thrown if the index is out of bounds. public void Insert(int index, in T item) { InsertRangeWithBeginEnd(index, index+1); this[index] = item; } /// /// Copies the last element of this list to an index. Decrements the length by 1. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// The index to overwrite with the last element. /// Thrown if the index is out of bounds. public void RemoveAtSwapBack(int index) { RemoveRangeSwapBack(index, 1); } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// /// Copies the last `count`-numbered elements to the range starting at `index`. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// /// Does nothing if the count is less than 1. /// /// The first index of the destination range. /// The number of elements to copy and the amount by which to decrement the length. /// Thrown if the index is out of bounds. public void RemoveRangeSwapBack(int index, int count) { if (count > 0) { int copyFrom = math.max(Length - count, index + count); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin); /// /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1. /// /// The index of the element to remove. /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element. /// /// Thrown if the index is out of bounds. public void RemoveAt(int index) { RemoveRange(index, 1); } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements. /// /// The first index of the range to remove. /// The number of elements to remove. /// Thrown if the index is out of bounds. public void RemoveRange(int index, int count) { if (count > 0) { int copyFrom = math.min(index + count, Length); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`. /// /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin); /// /// Returns a managed array that is a copy of this list. /// /// A managed array that is a copy of this list. [NotBurstCompatible] public T[] ToArray() { var result = new T[Length]; unsafe { byte* s = Buffer; fixed(T* d = result) UnsafeUtility.MemCpy(d, s, LengthInBytes); } return result; } /// /// Returns an array that is a copy of this list. /// /// The allocator to use. /// An array that is a copy of this list. public NativeArray ToNativeArray(AllocatorManager.AllocatorHandle allocator) { unsafe { var copy = CollectionHelper.CreateNativeArray(Length, allocator, NativeArrayOptions.UninitializedMemory); UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes); return copy; } } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList32Bytes a, in FixedList32Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList32Bytes a, in FixedList32Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList32Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList32Bytes other) { return CompareTo(other) == 0; } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList32Bytes a, in FixedList64Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList32Bytes a, in FixedList64Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList64Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList64Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList32Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList32Bytes<T>. public FixedList32Bytes(in FixedList64Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList32Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList64Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes30(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList32Bytes<T>. public static implicit operator FixedList32Bytes(in FixedList64Bytes other) { return new FixedList32Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList32Bytes a, in FixedList128Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList32Bytes a, in FixedList128Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList128Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList128Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList32Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList32Bytes<T>. public FixedList32Bytes(in FixedList128Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList32Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList128Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes30(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList32Bytes<T>. public static implicit operator FixedList32Bytes(in FixedList128Bytes other) { return new FixedList32Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList32Bytes a, in FixedList512Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList32Bytes a, in FixedList512Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList512Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList512Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList32Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList32Bytes<T>. public FixedList32Bytes(in FixedList512Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList32Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList512Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes30(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList32Bytes<T>. public static implicit operator FixedList32Bytes(in FixedList512Bytes other) { return new FixedList32Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList32Bytes a, in FixedList4096Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList32Bytes a, in FixedList4096Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList4096Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList4096Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList32Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList32Bytes<T>. public FixedList32Bytes(in FixedList4096Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList32Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList4096Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes30(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList32Bytes<T>. public static implicit operator FixedList32Bytes(in FixedList4096Bytes other) { return new FixedList32Bytes(other); } /// /// Returns true if the list is equal to an object. /// /// Two lists are equal if their length and bytes are equal. /// /// A FixedList*N*<T> can only be equal to another FixedList*N*<T> with the same *N* and T. /// /// An object to compare for equality. /// True if the list is equal to the object. [NotBurstCompatible] public override bool Equals(object obj) { if(obj is FixedList32Bytes aFixedList32Bytes) return Equals(aFixedList32Bytes); if(obj is FixedList64Bytes aFixedList64Bytes) return Equals(aFixedList64Bytes); if(obj is FixedList128Bytes aFixedList128Bytes) return Equals(aFixedList128Bytes); if(obj is FixedList512Bytes aFixedList512Bytes) return Equals(aFixedList512Bytes); if(obj is FixedList4096Bytes aFixedList4096Bytes) return Equals(aFixedList4096Bytes); return false; } /// /// An enumerator over the elements of a FixedList32Bytes<T>. /// /// /// In an enumerator's initial state, `Current` cannot be read. The first call advances the enumerator to the first element. /// public struct Enumerator : IEnumerator { FixedList32Bytes m_List; int m_Index; /// /// Initializes and returns an instance of FixedList32Bytes<T>. /// /// The list for which to create an enumerator. public Enumerator(ref FixedList32Bytes list) { m_List = list; m_Index = -1; } /// /// Does nothing. /// public void Dispose() { } /// /// Advances the enumerator to the next element. /// /// True if is valid to read after the call. public bool MoveNext() { m_Index++; return m_Index < m_List.Length; } /// /// Resets the enumerator to its initial state. /// public void Reset() { m_Index = -1; } /// /// The current element. /// /// The current element. public T Current => m_List[m_Index]; // Let FixedList32Bytes indexer check for out of range. object IEnumerator.Current => Current; } /// /// Returns an enumerator for iterating over the elements of this list. /// /// An enumerator for iterating over the elements of this list. public Enumerator GetEnumerator() { return new Enumerator(ref this); } /// /// This method is not implemented. Use instead. /// /// Nothing because it always throws . /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } /// /// This method is not implemented. Use instead. /// /// Nothing because it always throws . /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } /// /// Provides extension methods for FixedList32Bytes. /// [BurstCompatible] public unsafe static class FixedList32BytesExtensions { /// /// Finds the index of the first occurrence of a particular value in this list. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate. /// The index of the first occurrence of the value. Returns -1 if no occurrence is found. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static int IndexOf(this ref FixedList32Bytes list, U value) where T : unmanaged, IEquatable { return NativeArrayExtensions.IndexOf(list.Buffer, list.Length, value); } /// /// Returns true if a particular value is present in this list. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate. /// True if the value is present in this list. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Contains(this ref FixedList32Bytes list, U value) where T : unmanaged, IEquatable { return list.IndexOf(value) != -1; } /// /// Removes the first occurrence of a particular value in this list. /// /// /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one. /// /// If you don't need to preserve the order of the remaining elements, is a cheaper alternative. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate and remove. /// True if the value was found and removed. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Remove(this ref FixedList32Bytes list, U value) where T : unmanaged, IEquatable { int index = list.IndexOf(value); if (index < 0) { return false; } list.RemoveAt(index); return true; } /// /// Removes the first occurrence of a particular value in this list. /// /// /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one. /// /// This is cheaper than , but the order of the remaining elements is not preserved. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate and remove. /// Returns true if the item is removed. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool RemoveSwapBack(this ref FixedList32Bytes list, U value) where T : unmanaged, IEquatable { var index = list.IndexOf(value); if (index == -1) { return false; } list.RemoveAtSwapBack(index); return true; } } sealed class FixedList32BytesDebugView where T : unmanaged { FixedList32Bytes m_List; public FixedList32BytesDebugView(FixedList32Bytes list) { m_List = list; } public T[] Items => m_List.ToArray(); } [Obsolete("Renamed to FixedList64Bytes (UnityUpgradable) -> FixedList64Bytes", true)] public struct FixedList64 where T : unmanaged {} /// /// An unmanaged, resizable list whose content is all stored directly in the 64-byte struct. Useful for small lists. /// /// The type of the elements. [Serializable] [DebuggerTypeProxy(typeof(FixedList64BytesDebugView<>))] [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public struct FixedList64Bytes : INativeList , IEnumerable // Used by collection initializers. , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> where T : unmanaged { [SerializeField] internal ushort length; [SerializeField] internal FixedBytes62 buffer; /// /// The current number of items in this list. /// /// The current number of items in this list. [CreateProperty] public int Length { get => length; set { FixedList.CheckResize(value); length = (ushort)value; } } /// /// A property in order to display items in the Entity Inspector. /// [CreateProperty] IEnumerable Elements => this.ToArray(); /// /// Whether this list is empty. /// /// True if this string has no characters or if the container has not been constructed. public bool IsEmpty => Length == 0; internal int LengthInBytes => Length * UnsafeUtility.SizeOf(); unsafe internal byte* Buffer { get { fixed(byte* b = &buffer.offset0000.byte0000) return b + FixedList.PaddingBytes(); } } /// /// The number of elements that can fit in this list. /// /// The number of elements that can fit in this list. /// The capacity of a FixedList cannot be changed. The setter is included only for conformity with . /// Thrown if the new value does not match the current capacity. public int Capacity { get { return FixedList.Capacity(); } set { CollectionHelper.CheckCapacityInRange(value, Length); } } /// /// The element at a given index. /// /// An index. /// The value to store at the index. /// Thrown if the index is out of bounds. public T this[int index] { get { CollectionHelper.CheckIndexInRange(index, length); unsafe { return UnsafeUtility.ReadArrayElement(Buffer, CollectionHelper.AssumePositive(index)); } } set { CollectionHelper.CheckIndexInRange(index, length); unsafe { UnsafeUtility.WriteArrayElement(Buffer, CollectionHelper.AssumePositive(index), value); } } } /// /// Returns the element at a given index. /// /// An index. /// The list element at the index. public ref T ElementAt(int index) { CollectionHelper.CheckIndexInRange(index, length); unsafe { return ref UnsafeUtility.ArrayElementAsRef(Buffer, index); } } /// /// Returns the hash code of this list. /// /// /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash. /// The hash code of this list. public override int GetHashCode() { unsafe { return (int)CollectionHelper.Hash(Buffer, LengthInBytes); } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Remember that a fixed list is never resized. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void Add(in T item) { this[Length++] = item; } /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Remember that a fixed list is never resized. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRange(void* ptr, int length) { T* data = (T*)ptr; for (var i = 0; i < length; ++i) { this[Length++] = data[i]; } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Included only for consistency with the other list types. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void AddNoResize(in T item) => Add(item); /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Included only for consistency with the other list types. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length); /// /// Sets the length to 0. /// /// Does *not* zero out the bytes. public void Clear() { Length = 0; } /// /// Shifts elements toward the end of this list, increasing its length. /// /// /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle. /// /// The length is increased by `end - begin`. /// /// If `end` equals `begin`, the method does nothing. /// /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth. /// /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior. /// /// The index of the first element that will be shifted up. /// The index where the first shifted element will end up. /// Thrown if the new length exceeds the capacity. public void InsertRangeWithBeginEnd(int begin, int end) { int items = end - begin; if(items < 1) return; int itemsToCopy = length - begin; Length += items; if(itemsToCopy < 1) return; int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf(); unsafe { byte *b = Buffer; byte *dest = b + end * UnsafeUtility.SizeOf(); byte *src = b + begin * UnsafeUtility.SizeOf(); UnsafeUtility.MemMove(dest, src, bytesToCopy); } } /// /// Inserts a single element at an index. Increments the length by 1. /// /// The index at which to insert the element. /// The element to insert. /// Thrown if the index is out of bounds. public void Insert(int index, in T item) { InsertRangeWithBeginEnd(index, index+1); this[index] = item; } /// /// Copies the last element of this list to an index. Decrements the length by 1. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// The index to overwrite with the last element. /// Thrown if the index is out of bounds. public void RemoveAtSwapBack(int index) { RemoveRangeSwapBack(index, 1); } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// /// Copies the last `count`-numbered elements to the range starting at `index`. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// /// Does nothing if the count is less than 1. /// /// The first index of the destination range. /// The number of elements to copy and the amount by which to decrement the length. /// Thrown if the index is out of bounds. public void RemoveRangeSwapBack(int index, int count) { if (count > 0) { int copyFrom = math.max(Length - count, index + count); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin); /// /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1. /// /// The index of the element to remove. /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element. /// /// Thrown if the index is out of bounds. public void RemoveAt(int index) { RemoveRange(index, 1); } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements. /// /// The first index of the range to remove. /// The number of elements to remove. /// Thrown if the index is out of bounds. public void RemoveRange(int index, int count) { if (count > 0) { int copyFrom = math.min(index + count, Length); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`. /// /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin); /// /// Returns a managed array that is a copy of this list. /// /// A managed array that is a copy of this list. [NotBurstCompatible] public T[] ToArray() { var result = new T[Length]; unsafe { byte* s = Buffer; fixed(T* d = result) UnsafeUtility.MemCpy(d, s, LengthInBytes); } return result; } /// /// Returns an array that is a copy of this list. /// /// The allocator to use. /// An array that is a copy of this list. public NativeArray ToNativeArray(AllocatorManager.AllocatorHandle allocator) { unsafe { var copy = CollectionHelper.CreateNativeArray(Length, allocator, NativeArrayOptions.UninitializedMemory); UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes); return copy; } } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList64Bytes a, in FixedList32Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList64Bytes a, in FixedList32Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList32Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList32Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList64Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList64Bytes<T>. public FixedList64Bytes(in FixedList32Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList64Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList32Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes62(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList64Bytes<T>. public static implicit operator FixedList64Bytes(in FixedList32Bytes other) { return new FixedList64Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList64Bytes a, in FixedList64Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList64Bytes a, in FixedList64Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList64Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList64Bytes other) { return CompareTo(other) == 0; } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList64Bytes a, in FixedList128Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList64Bytes a, in FixedList128Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList128Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList128Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList64Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList64Bytes<T>. public FixedList64Bytes(in FixedList128Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList64Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList128Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes62(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList64Bytes<T>. public static implicit operator FixedList64Bytes(in FixedList128Bytes other) { return new FixedList64Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList64Bytes a, in FixedList512Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList64Bytes a, in FixedList512Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList512Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList512Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList64Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList64Bytes<T>. public FixedList64Bytes(in FixedList512Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList64Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList512Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes62(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList64Bytes<T>. public static implicit operator FixedList64Bytes(in FixedList512Bytes other) { return new FixedList64Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList64Bytes a, in FixedList4096Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList64Bytes a, in FixedList4096Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList4096Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList4096Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList64Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList64Bytes<T>. public FixedList64Bytes(in FixedList4096Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList64Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList4096Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes62(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList64Bytes<T>. public static implicit operator FixedList64Bytes(in FixedList4096Bytes other) { return new FixedList64Bytes(other); } /// /// Returns true if the list is equal to an object. /// /// Two lists are equal if their length and bytes are equal. /// /// A FixedList*N*<T> can only be equal to another FixedList*N*<T> with the same *N* and T. /// /// An object to compare for equality. /// True if the list is equal to the object. [NotBurstCompatible] public override bool Equals(object obj) { if(obj is FixedList32Bytes aFixedList32Bytes) return Equals(aFixedList32Bytes); if(obj is FixedList64Bytes aFixedList64Bytes) return Equals(aFixedList64Bytes); if(obj is FixedList128Bytes aFixedList128Bytes) return Equals(aFixedList128Bytes); if(obj is FixedList512Bytes aFixedList512Bytes) return Equals(aFixedList512Bytes); if(obj is FixedList4096Bytes aFixedList4096Bytes) return Equals(aFixedList4096Bytes); return false; } /// /// An enumerator over the elements of a FixedList64Bytes<T>. /// /// /// In an enumerator's initial state, `Current` cannot be read. The first call advances the enumerator to the first element. /// public struct Enumerator : IEnumerator { FixedList64Bytes m_List; int m_Index; /// /// Initializes and returns an instance of FixedList64Bytes<T>. /// /// The list for which to create an enumerator. public Enumerator(ref FixedList64Bytes list) { m_List = list; m_Index = -1; } /// /// Does nothing. /// public void Dispose() { } /// /// Advances the enumerator to the next element. /// /// True if is valid to read after the call. public bool MoveNext() { m_Index++; return m_Index < m_List.Length; } /// /// Resets the enumerator to its initial state. /// public void Reset() { m_Index = -1; } /// /// The current element. /// /// The current element. public T Current => m_List[m_Index]; // Let FixedList64Bytes indexer check for out of range. object IEnumerator.Current => Current; } /// /// Returns an enumerator for iterating over the elements of this list. /// /// An enumerator for iterating over the elements of this list. public Enumerator GetEnumerator() { return new Enumerator(ref this); } /// /// This method is not implemented. Use instead. /// /// Nothing because it always throws . /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } /// /// This method is not implemented. Use instead. /// /// Nothing because it always throws . /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } /// /// Provides extension methods for FixedList64Bytes. /// [BurstCompatible] public unsafe static class FixedList64BytesExtensions { /// /// Finds the index of the first occurrence of a particular value in this list. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate. /// The index of the first occurrence of the value. Returns -1 if no occurrence is found. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static int IndexOf(this ref FixedList64Bytes list, U value) where T : unmanaged, IEquatable { return NativeArrayExtensions.IndexOf(list.Buffer, list.Length, value); } /// /// Returns true if a particular value is present in this list. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate. /// True if the value is present in this list. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Contains(this ref FixedList64Bytes list, U value) where T : unmanaged, IEquatable { return list.IndexOf(value) != -1; } /// /// Removes the first occurrence of a particular value in this list. /// /// /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one. /// /// If you don't need to preserve the order of the remaining elements, is a cheaper alternative. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate and remove. /// True if the value was found and removed. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Remove(this ref FixedList64Bytes list, U value) where T : unmanaged, IEquatable { int index = list.IndexOf(value); if (index < 0) { return false; } list.RemoveAt(index); return true; } /// /// Removes the first occurrence of a particular value in this list. /// /// /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one. /// /// This is cheaper than , but the order of the remaining elements is not preserved. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate and remove. /// Returns true if the item is removed. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool RemoveSwapBack(this ref FixedList64Bytes list, U value) where T : unmanaged, IEquatable { var index = list.IndexOf(value); if (index == -1) { return false; } list.RemoveAtSwapBack(index); return true; } } sealed class FixedList64BytesDebugView where T : unmanaged { FixedList64Bytes m_List; public FixedList64BytesDebugView(FixedList64Bytes list) { m_List = list; } public T[] Items => m_List.ToArray(); } [Obsolete("Renamed to FixedList128Bytes (UnityUpgradable) -> FixedList128Bytes", true)] public struct FixedList128 where T : unmanaged {} /// /// An unmanaged, resizable list whose content is all stored directly in the 128-byte struct. Useful for small lists. /// /// The type of the elements. [Serializable] [DebuggerTypeProxy(typeof(FixedList128BytesDebugView<>))] [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public struct FixedList128Bytes : INativeList , IEnumerable // Used by collection initializers. , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> where T : unmanaged { [SerializeField] internal ushort length; [SerializeField] internal FixedBytes126 buffer; /// /// The current number of items in this list. /// /// The current number of items in this list. [CreateProperty] public int Length { get => length; set { FixedList.CheckResize(value); length = (ushort)value; } } /// /// A property in order to display items in the Entity Inspector. /// [CreateProperty] IEnumerable Elements => this.ToArray(); /// /// Whether this list is empty. /// /// True if this string has no characters or if the container has not been constructed. public bool IsEmpty => Length == 0; internal int LengthInBytes => Length * UnsafeUtility.SizeOf(); unsafe internal byte* Buffer { get { fixed(byte* b = &buffer.offset0000.byte0000) return b + FixedList.PaddingBytes(); } } /// /// The number of elements that can fit in this list. /// /// The number of elements that can fit in this list. /// The capacity of a FixedList cannot be changed. The setter is included only for conformity with . /// Thrown if the new value does not match the current capacity. public int Capacity { get { return FixedList.Capacity(); } set { CollectionHelper.CheckCapacityInRange(value, Length); } } /// /// The element at a given index. /// /// An index. /// The value to store at the index. /// Thrown if the index is out of bounds. public T this[int index] { get { CollectionHelper.CheckIndexInRange(index, length); unsafe { return UnsafeUtility.ReadArrayElement(Buffer, CollectionHelper.AssumePositive(index)); } } set { CollectionHelper.CheckIndexInRange(index, length); unsafe { UnsafeUtility.WriteArrayElement(Buffer, CollectionHelper.AssumePositive(index), value); } } } /// /// Returns the element at a given index. /// /// An index. /// The list element at the index. public ref T ElementAt(int index) { CollectionHelper.CheckIndexInRange(index, length); unsafe { return ref UnsafeUtility.ArrayElementAsRef(Buffer, index); } } /// /// Returns the hash code of this list. /// /// /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash. /// The hash code of this list. public override int GetHashCode() { unsafe { return (int)CollectionHelper.Hash(Buffer, LengthInBytes); } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Remember that a fixed list is never resized. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void Add(in T item) { this[Length++] = item; } /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Remember that a fixed list is never resized. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRange(void* ptr, int length) { T* data = (T*)ptr; for (var i = 0; i < length; ++i) { this[Length++] = data[i]; } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Included only for consistency with the other list types. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void AddNoResize(in T item) => Add(item); /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Included only for consistency with the other list types. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length); /// /// Sets the length to 0. /// /// Does *not* zero out the bytes. public void Clear() { Length = 0; } /// /// Shifts elements toward the end of this list, increasing its length. /// /// /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle. /// /// The length is increased by `end - begin`. /// /// If `end` equals `begin`, the method does nothing. /// /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth. /// /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior. /// /// The index of the first element that will be shifted up. /// The index where the first shifted element will end up. /// Thrown if the new length exceeds the capacity. public void InsertRangeWithBeginEnd(int begin, int end) { int items = end - begin; if(items < 1) return; int itemsToCopy = length - begin; Length += items; if(itemsToCopy < 1) return; int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf(); unsafe { byte *b = Buffer; byte *dest = b + end * UnsafeUtility.SizeOf(); byte *src = b + begin * UnsafeUtility.SizeOf(); UnsafeUtility.MemMove(dest, src, bytesToCopy); } } /// /// Inserts a single element at an index. Increments the length by 1. /// /// The index at which to insert the element. /// The element to insert. /// Thrown if the index is out of bounds. public void Insert(int index, in T item) { InsertRangeWithBeginEnd(index, index+1); this[index] = item; } /// /// Copies the last element of this list to an index. Decrements the length by 1. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// The index to overwrite with the last element. /// Thrown if the index is out of bounds. public void RemoveAtSwapBack(int index) { RemoveRangeSwapBack(index, 1); } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// /// Copies the last `count`-numbered elements to the range starting at `index`. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// /// Does nothing if the count is less than 1. /// /// The first index of the destination range. /// The number of elements to copy and the amount by which to decrement the length. /// Thrown if the index is out of bounds. public void RemoveRangeSwapBack(int index, int count) { if (count > 0) { int copyFrom = math.max(Length - count, index + count); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin); /// /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1. /// /// The index of the element to remove. /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element. /// /// Thrown if the index is out of bounds. public void RemoveAt(int index) { RemoveRange(index, 1); } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements. /// /// The first index of the range to remove. /// The number of elements to remove. /// Thrown if the index is out of bounds. public void RemoveRange(int index, int count) { if (count > 0) { int copyFrom = math.min(index + count, Length); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`. /// /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin); /// /// Returns a managed array that is a copy of this list. /// /// A managed array that is a copy of this list. [NotBurstCompatible] public T[] ToArray() { var result = new T[Length]; unsafe { byte* s = Buffer; fixed(T* d = result) UnsafeUtility.MemCpy(d, s, LengthInBytes); } return result; } /// /// Returns an array that is a copy of this list. /// /// The allocator to use. /// An array that is a copy of this list. public NativeArray ToNativeArray(AllocatorManager.AllocatorHandle allocator) { unsafe { var copy = CollectionHelper.CreateNativeArray(Length, allocator, NativeArrayOptions.UninitializedMemory); UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes); return copy; } } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList128Bytes a, in FixedList32Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList128Bytes a, in FixedList32Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList32Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList32Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList128Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList128Bytes<T>. public FixedList128Bytes(in FixedList32Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList128Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList32Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes126(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList128Bytes<T>. public static implicit operator FixedList128Bytes(in FixedList32Bytes other) { return new FixedList128Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList128Bytes a, in FixedList64Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList128Bytes a, in FixedList64Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList64Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList64Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList128Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList128Bytes<T>. public FixedList128Bytes(in FixedList64Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList128Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList64Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes126(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList128Bytes<T>. public static implicit operator FixedList128Bytes(in FixedList64Bytes other) { return new FixedList128Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList128Bytes a, in FixedList128Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList128Bytes a, in FixedList128Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList128Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList128Bytes other) { return CompareTo(other) == 0; } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList128Bytes a, in FixedList512Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList128Bytes a, in FixedList512Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList512Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList512Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList128Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList128Bytes<T>. public FixedList128Bytes(in FixedList512Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList128Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList512Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes126(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList128Bytes<T>. public static implicit operator FixedList128Bytes(in FixedList512Bytes other) { return new FixedList128Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList128Bytes a, in FixedList4096Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList128Bytes a, in FixedList4096Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList4096Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList4096Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList128Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList128Bytes<T>. public FixedList128Bytes(in FixedList4096Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList128Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList4096Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes126(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList128Bytes<T>. public static implicit operator FixedList128Bytes(in FixedList4096Bytes other) { return new FixedList128Bytes(other); } /// /// Returns true if the list is equal to an object. /// /// Two lists are equal if their length and bytes are equal. /// /// A FixedList*N*<T> can only be equal to another FixedList*N*<T> with the same *N* and T. /// /// An object to compare for equality. /// True if the list is equal to the object. [NotBurstCompatible] public override bool Equals(object obj) { if(obj is FixedList32Bytes aFixedList32Bytes) return Equals(aFixedList32Bytes); if(obj is FixedList64Bytes aFixedList64Bytes) return Equals(aFixedList64Bytes); if(obj is FixedList128Bytes aFixedList128Bytes) return Equals(aFixedList128Bytes); if(obj is FixedList512Bytes aFixedList512Bytes) return Equals(aFixedList512Bytes); if(obj is FixedList4096Bytes aFixedList4096Bytes) return Equals(aFixedList4096Bytes); return false; } /// /// An enumerator over the elements of a FixedList128Bytes<T>. /// /// /// In an enumerator's initial state, `Current` cannot be read. The first call advances the enumerator to the first element. /// public struct Enumerator : IEnumerator { FixedList128Bytes m_List; int m_Index; /// /// Initializes and returns an instance of FixedList128Bytes<T>. /// /// The list for which to create an enumerator. public Enumerator(ref FixedList128Bytes list) { m_List = list; m_Index = -1; } /// /// Does nothing. /// public void Dispose() { } /// /// Advances the enumerator to the next element. /// /// True if is valid to read after the call. public bool MoveNext() { m_Index++; return m_Index < m_List.Length; } /// /// Resets the enumerator to its initial state. /// public void Reset() { m_Index = -1; } /// /// The current element. /// /// The current element. public T Current => m_List[m_Index]; // Let FixedList128Bytes indexer check for out of range. object IEnumerator.Current => Current; } /// /// Returns an enumerator for iterating over the elements of this list. /// /// An enumerator for iterating over the elements of this list. public Enumerator GetEnumerator() { return new Enumerator(ref this); } /// /// This method is not implemented. Use instead. /// /// Nothing because it always throws . /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } /// /// This method is not implemented. Use instead. /// /// Nothing because it always throws . /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } /// /// Provides extension methods for FixedList128Bytes. /// [BurstCompatible] public unsafe static class FixedList128BytesExtensions { /// /// Finds the index of the first occurrence of a particular value in this list. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate. /// The index of the first occurrence of the value. Returns -1 if no occurrence is found. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static int IndexOf(this ref FixedList128Bytes list, U value) where T : unmanaged, IEquatable { return NativeArrayExtensions.IndexOf(list.Buffer, list.Length, value); } /// /// Returns true if a particular value is present in this list. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate. /// True if the value is present in this list. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Contains(this ref FixedList128Bytes list, U value) where T : unmanaged, IEquatable { return list.IndexOf(value) != -1; } /// /// Removes the first occurrence of a particular value in this list. /// /// /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one. /// /// If you don't need to preserve the order of the remaining elements, is a cheaper alternative. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate and remove. /// True if the value was found and removed. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Remove(this ref FixedList128Bytes list, U value) where T : unmanaged, IEquatable { int index = list.IndexOf(value); if (index < 0) { return false; } list.RemoveAt(index); return true; } /// /// Removes the first occurrence of a particular value in this list. /// /// /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one. /// /// This is cheaper than , but the order of the remaining elements is not preserved. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate and remove. /// Returns true if the item is removed. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool RemoveSwapBack(this ref FixedList128Bytes list, U value) where T : unmanaged, IEquatable { var index = list.IndexOf(value); if (index == -1) { return false; } list.RemoveAtSwapBack(index); return true; } } sealed class FixedList128BytesDebugView where T : unmanaged { FixedList128Bytes m_List; public FixedList128BytesDebugView(FixedList128Bytes list) { m_List = list; } public T[] Items => m_List.ToArray(); } [Obsolete("Renamed to FixedList512Bytes (UnityUpgradable) -> FixedList512Bytes", true)] public struct FixedList512 where T : unmanaged {} /// /// An unmanaged, resizable list whose content is all stored directly in the 512-byte struct. Useful for small lists. /// /// The type of the elements. [Serializable] [DebuggerTypeProxy(typeof(FixedList512BytesDebugView<>))] [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public struct FixedList512Bytes : INativeList , IEnumerable // Used by collection initializers. , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> where T : unmanaged { [SerializeField] internal ushort length; [SerializeField] internal FixedBytes510 buffer; /// /// The current number of items in this list. /// /// The current number of items in this list. [CreateProperty] public int Length { get => length; set { FixedList.CheckResize(value); length = (ushort)value; } } /// /// A property in order to display items in the Entity Inspector. /// [CreateProperty] IEnumerable Elements => this.ToArray(); /// /// Whether this list is empty. /// /// True if this string has no characters or if the container has not been constructed. public bool IsEmpty => Length == 0; internal int LengthInBytes => Length * UnsafeUtility.SizeOf(); unsafe internal byte* Buffer { get { fixed(byte* b = &buffer.offset0000.byte0000) return b + FixedList.PaddingBytes(); } } /// /// The number of elements that can fit in this list. /// /// The number of elements that can fit in this list. /// The capacity of a FixedList cannot be changed. The setter is included only for conformity with . /// Thrown if the new value does not match the current capacity. public int Capacity { get { return FixedList.Capacity(); } set { CollectionHelper.CheckCapacityInRange(value, Length); } } /// /// The element at a given index. /// /// An index. /// The value to store at the index. /// Thrown if the index is out of bounds. public T this[int index] { get { CollectionHelper.CheckIndexInRange(index, length); unsafe { return UnsafeUtility.ReadArrayElement(Buffer, CollectionHelper.AssumePositive(index)); } } set { CollectionHelper.CheckIndexInRange(index, length); unsafe { UnsafeUtility.WriteArrayElement(Buffer, CollectionHelper.AssumePositive(index), value); } } } /// /// Returns the element at a given index. /// /// An index. /// The list element at the index. public ref T ElementAt(int index) { CollectionHelper.CheckIndexInRange(index, length); unsafe { return ref UnsafeUtility.ArrayElementAsRef(Buffer, index); } } /// /// Returns the hash code of this list. /// /// /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash. /// The hash code of this list. public override int GetHashCode() { unsafe { return (int)CollectionHelper.Hash(Buffer, LengthInBytes); } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Remember that a fixed list is never resized. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void Add(in T item) { this[Length++] = item; } /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Remember that a fixed list is never resized. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRange(void* ptr, int length) { T* data = (T*)ptr; for (var i = 0; i < length; ++i) { this[Length++] = data[i]; } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Included only for consistency with the other list types. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void AddNoResize(in T item) => Add(item); /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Included only for consistency with the other list types. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length); /// /// Sets the length to 0. /// /// Does *not* zero out the bytes. public void Clear() { Length = 0; } /// /// Shifts elements toward the end of this list, increasing its length. /// /// /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle. /// /// The length is increased by `end - begin`. /// /// If `end` equals `begin`, the method does nothing. /// /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth. /// /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior. /// /// The index of the first element that will be shifted up. /// The index where the first shifted element will end up. /// Thrown if the new length exceeds the capacity. public void InsertRangeWithBeginEnd(int begin, int end) { int items = end - begin; if(items < 1) return; int itemsToCopy = length - begin; Length += items; if(itemsToCopy < 1) return; int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf(); unsafe { byte *b = Buffer; byte *dest = b + end * UnsafeUtility.SizeOf(); byte *src = b + begin * UnsafeUtility.SizeOf(); UnsafeUtility.MemMove(dest, src, bytesToCopy); } } /// /// Inserts a single element at an index. Increments the length by 1. /// /// The index at which to insert the element. /// The element to insert. /// Thrown if the index is out of bounds. public void Insert(int index, in T item) { InsertRangeWithBeginEnd(index, index+1); this[index] = item; } /// /// Copies the last element of this list to an index. Decrements the length by 1. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// The index to overwrite with the last element. /// Thrown if the index is out of bounds. public void RemoveAtSwapBack(int index) { RemoveRangeSwapBack(index, 1); } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// /// Copies the last `count`-numbered elements to the range starting at `index`. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// /// Does nothing if the count is less than 1. /// /// The first index of the destination range. /// The number of elements to copy and the amount by which to decrement the length. /// Thrown if the index is out of bounds. public void RemoveRangeSwapBack(int index, int count) { if (count > 0) { int copyFrom = math.max(Length - count, index + count); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin); /// /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1. /// /// The index of the element to remove. /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element. /// /// Thrown if the index is out of bounds. public void RemoveAt(int index) { RemoveRange(index, 1); } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements. /// /// The first index of the range to remove. /// The number of elements to remove. /// Thrown if the index is out of bounds. public void RemoveRange(int index, int count) { if (count > 0) { int copyFrom = math.min(index + count, Length); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`. /// /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin); /// /// Returns a managed array that is a copy of this list. /// /// A managed array that is a copy of this list. [NotBurstCompatible] public T[] ToArray() { var result = new T[Length]; unsafe { byte* s = Buffer; fixed(T* d = result) UnsafeUtility.MemCpy(d, s, LengthInBytes); } return result; } /// /// Returns an array that is a copy of this list. /// /// The allocator to use. /// An array that is a copy of this list. public NativeArray ToNativeArray(AllocatorManager.AllocatorHandle allocator) { unsafe { var copy = CollectionHelper.CreateNativeArray(Length, allocator, NativeArrayOptions.UninitializedMemory); UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes); return copy; } } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList512Bytes a, in FixedList32Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList512Bytes a, in FixedList32Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList32Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList32Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList512Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList512Bytes<T>. public FixedList512Bytes(in FixedList32Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList512Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList32Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes510(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList512Bytes<T>. public static implicit operator FixedList512Bytes(in FixedList32Bytes other) { return new FixedList512Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList512Bytes a, in FixedList64Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList512Bytes a, in FixedList64Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList64Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList64Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList512Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList512Bytes<T>. public FixedList512Bytes(in FixedList64Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList512Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList64Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes510(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList512Bytes<T>. public static implicit operator FixedList512Bytes(in FixedList64Bytes other) { return new FixedList512Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList512Bytes a, in FixedList128Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList512Bytes a, in FixedList128Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList128Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList128Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList512Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList512Bytes<T>. public FixedList512Bytes(in FixedList128Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList512Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList128Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes510(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList512Bytes<T>. public static implicit operator FixedList512Bytes(in FixedList128Bytes other) { return new FixedList512Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList512Bytes a, in FixedList512Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList512Bytes a, in FixedList512Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList512Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList512Bytes other) { return CompareTo(other) == 0; } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList512Bytes a, in FixedList4096Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList512Bytes a, in FixedList4096Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList4096Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList4096Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList512Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList512Bytes<T>. public FixedList512Bytes(in FixedList4096Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList512Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList4096Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes510(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList512Bytes<T>. public static implicit operator FixedList512Bytes(in FixedList4096Bytes other) { return new FixedList512Bytes(other); } /// /// Returns true if the list is equal to an object. /// /// Two lists are equal if their length and bytes are equal. /// /// A FixedList*N*<T> can only be equal to another FixedList*N*<T> with the same *N* and T. /// /// An object to compare for equality. /// True if the list is equal to the object. [NotBurstCompatible] public override bool Equals(object obj) { if(obj is FixedList32Bytes aFixedList32Bytes) return Equals(aFixedList32Bytes); if(obj is FixedList64Bytes aFixedList64Bytes) return Equals(aFixedList64Bytes); if(obj is FixedList128Bytes aFixedList128Bytes) return Equals(aFixedList128Bytes); if(obj is FixedList512Bytes aFixedList512Bytes) return Equals(aFixedList512Bytes); if(obj is FixedList4096Bytes aFixedList4096Bytes) return Equals(aFixedList4096Bytes); return false; } /// /// An enumerator over the elements of a FixedList512Bytes<T>. /// /// /// In an enumerator's initial state, `Current` cannot be read. The first call advances the enumerator to the first element. /// public struct Enumerator : IEnumerator { FixedList512Bytes m_List; int m_Index; /// /// Initializes and returns an instance of FixedList512Bytes<T>. /// /// The list for which to create an enumerator. public Enumerator(ref FixedList512Bytes list) { m_List = list; m_Index = -1; } /// /// Does nothing. /// public void Dispose() { } /// /// Advances the enumerator to the next element. /// /// True if is valid to read after the call. public bool MoveNext() { m_Index++; return m_Index < m_List.Length; } /// /// Resets the enumerator to its initial state. /// public void Reset() { m_Index = -1; } /// /// The current element. /// /// The current element. public T Current => m_List[m_Index]; // Let FixedList512Bytes indexer check for out of range. object IEnumerator.Current => Current; } /// /// Returns an enumerator for iterating over the elements of this list. /// /// An enumerator for iterating over the elements of this list. public Enumerator GetEnumerator() { return new Enumerator(ref this); } /// /// This method is not implemented. Use instead. /// /// Nothing because it always throws . /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } /// /// This method is not implemented. Use instead. /// /// Nothing because it always throws . /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } /// /// Provides extension methods for FixedList512Bytes. /// [BurstCompatible] public unsafe static class FixedList512BytesExtensions { /// /// Finds the index of the first occurrence of a particular value in this list. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate. /// The index of the first occurrence of the value. Returns -1 if no occurrence is found. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static int IndexOf(this ref FixedList512Bytes list, U value) where T : unmanaged, IEquatable { return NativeArrayExtensions.IndexOf(list.Buffer, list.Length, value); } /// /// Returns true if a particular value is present in this list. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate. /// True if the value is present in this list. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Contains(this ref FixedList512Bytes list, U value) where T : unmanaged, IEquatable { return list.IndexOf(value) != -1; } /// /// Removes the first occurrence of a particular value in this list. /// /// /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one. /// /// If you don't need to preserve the order of the remaining elements, is a cheaper alternative. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate and remove. /// True if the value was found and removed. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Remove(this ref FixedList512Bytes list, U value) where T : unmanaged, IEquatable { int index = list.IndexOf(value); if (index < 0) { return false; } list.RemoveAt(index); return true; } /// /// Removes the first occurrence of a particular value in this list. /// /// /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one. /// /// This is cheaper than , but the order of the remaining elements is not preserved. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate and remove. /// Returns true if the item is removed. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool RemoveSwapBack(this ref FixedList512Bytes list, U value) where T : unmanaged, IEquatable { var index = list.IndexOf(value); if (index == -1) { return false; } list.RemoveAtSwapBack(index); return true; } } sealed class FixedList512BytesDebugView where T : unmanaged { FixedList512Bytes m_List; public FixedList512BytesDebugView(FixedList512Bytes list) { m_List = list; } public T[] Items => m_List.ToArray(); } [Obsolete("Renamed to FixedList4096Bytes (UnityUpgradable) -> FixedList4096Bytes", true)] public struct FixedList4096 where T : unmanaged {} /// /// An unmanaged, resizable list whose content is all stored directly in the 4096-byte struct. Useful for small lists. /// /// The type of the elements. [Serializable] [DebuggerTypeProxy(typeof(FixedList4096BytesDebugView<>))] [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public struct FixedList4096Bytes : INativeList , IEnumerable // Used by collection initializers. , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> , IEquatable> , IComparable> where T : unmanaged { [SerializeField] internal ushort length; [SerializeField] internal FixedBytes4094 buffer; /// /// The current number of items in this list. /// /// The current number of items in this list. [CreateProperty] public int Length { get => length; set { FixedList.CheckResize(value); length = (ushort)value; } } /// /// A property in order to display items in the Entity Inspector. /// [CreateProperty] IEnumerable Elements => this.ToArray(); /// /// Whether this list is empty. /// /// True if this string has no characters or if the container has not been constructed. public bool IsEmpty => Length == 0; internal int LengthInBytes => Length * UnsafeUtility.SizeOf(); unsafe internal byte* Buffer { get { fixed(byte* b = &buffer.offset0000.byte0000) return b + FixedList.PaddingBytes(); } } /// /// The number of elements that can fit in this list. /// /// The number of elements that can fit in this list. /// The capacity of a FixedList cannot be changed. The setter is included only for conformity with . /// Thrown if the new value does not match the current capacity. public int Capacity { get { return FixedList.Capacity(); } set { CollectionHelper.CheckCapacityInRange(value, Length); } } /// /// The element at a given index. /// /// An index. /// The value to store at the index. /// Thrown if the index is out of bounds. public T this[int index] { get { CollectionHelper.CheckIndexInRange(index, length); unsafe { return UnsafeUtility.ReadArrayElement(Buffer, CollectionHelper.AssumePositive(index)); } } set { CollectionHelper.CheckIndexInRange(index, length); unsafe { UnsafeUtility.WriteArrayElement(Buffer, CollectionHelper.AssumePositive(index), value); } } } /// /// Returns the element at a given index. /// /// An index. /// The list element at the index. public ref T ElementAt(int index) { CollectionHelper.CheckIndexInRange(index, length); unsafe { return ref UnsafeUtility.ArrayElementAsRef(Buffer, index); } } /// /// Returns the hash code of this list. /// /// /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash. /// The hash code of this list. public override int GetHashCode() { unsafe { return (int)CollectionHelper.Hash(Buffer, LengthInBytes); } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Remember that a fixed list is never resized. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void Add(in T item) { this[Length++] = item; } /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Remember that a fixed list is never resized. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRange(void* ptr, int length) { T* data = (T*)ptr; for (var i = 0; i < length; ++i) { this[Length++] = data[i]; } } /// /// Appends an element to the end of this list. Increments the length by 1. /// /// The same as . Included only for consistency with the other list types. /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void AddNoResize(in T item) => Add(item); /// /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements. /// /// The same as . Included only for consistency with the other list types. /// A buffer. /// The number of elements from the buffer to append. /// Thrown if the append exceeds the capacity. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length); /// /// Sets the length to 0. /// /// Does *not* zero out the bytes. public void Clear() { Length = 0; } /// /// Shifts elements toward the end of this list, increasing its length. /// /// /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle. /// /// The length is increased by `end - begin`. /// /// If `end` equals `begin`, the method does nothing. /// /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth. /// /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior. /// /// The index of the first element that will be shifted up. /// The index where the first shifted element will end up. /// Thrown if the new length exceeds the capacity. public void InsertRangeWithBeginEnd(int begin, int end) { int items = end - begin; if(items < 1) return; int itemsToCopy = length - begin; Length += items; if(itemsToCopy < 1) return; int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf(); unsafe { byte *b = Buffer; byte *dest = b + end * UnsafeUtility.SizeOf(); byte *src = b + begin * UnsafeUtility.SizeOf(); UnsafeUtility.MemMove(dest, src, bytesToCopy); } } /// /// Inserts a single element at an index. Increments the length by 1. /// /// The index at which to insert the element. /// The element to insert. /// Thrown if the index is out of bounds. public void Insert(int index, in T item) { InsertRangeWithBeginEnd(index, index+1); this[index] = item; } /// /// Copies the last element of this list to an index. Decrements the length by 1. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// The index to overwrite with the last element. /// Thrown if the index is out of bounds. public void RemoveAtSwapBack(int index) { RemoveRangeSwapBack(index, 1); } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// /// Copies the last `count`-numbered elements to the range starting at `index`. /// /// Useful as a cheap way to remove elements from a list when you don't care about preserving order. /// /// Does nothing if the count is less than 1. /// /// The first index of the destination range. /// The number of elements to copy and the amount by which to decrement the length. /// Thrown if the index is out of bounds. public void RemoveRangeSwapBack(int index, int count) { if (count > 0) { int copyFrom = math.max(Length - count, index + count); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin); /// /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1. /// /// The index of the element to remove. /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element. /// /// Thrown if the index is out of bounds. public void RemoveAt(int index) { RemoveRange(index, 1); } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements. /// /// The first index of the range to remove. /// The number of elements to remove. /// Thrown if the index is out of bounds. public void RemoveRange(int index, int count) { if (count > 0) { int copyFrom = math.min(index + count, Length); unsafe { var sizeOf = UnsafeUtility.SizeOf(); void* dst = Buffer + index * sizeOf; void* src = Buffer + copyFrom * sizeOf; UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf); } Length -= count; } } /// /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*. /// /// The first index of the item to remove. /// The index past-the-last item to remove. /// /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`. /// /// Thrown if end argument is less than begin argument. /// Thrown if begin or end arguments are not positive or out of bounds. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)] public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin); /// /// Returns a managed array that is a copy of this list. /// /// A managed array that is a copy of this list. [NotBurstCompatible] public T[] ToArray() { var result = new T[Length]; unsafe { byte* s = Buffer; fixed(T* d = result) UnsafeUtility.MemCpy(d, s, LengthInBytes); } return result; } /// /// Returns an array that is a copy of this list. /// /// The allocator to use. /// An array that is a copy of this list. public NativeArray ToNativeArray(AllocatorManager.AllocatorHandle allocator) { unsafe { var copy = CollectionHelper.CreateNativeArray(Length, allocator, NativeArrayOptions.UninitializedMemory); UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes); return copy; } } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList4096Bytes a, in FixedList32Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList4096Bytes a, in FixedList32Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList32Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList32Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList4096Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList4096Bytes<T>. public FixedList4096Bytes(in FixedList32Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList4096Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList32Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes4094(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList4096Bytes<T>. public static implicit operator FixedList4096Bytes(in FixedList32Bytes other) { return new FixedList4096Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList4096Bytes a, in FixedList64Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList4096Bytes a, in FixedList64Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList64Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList64Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList4096Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList4096Bytes<T>. public FixedList4096Bytes(in FixedList64Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList4096Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList64Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes4094(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList4096Bytes<T>. public static implicit operator FixedList4096Bytes(in FixedList64Bytes other) { return new FixedList4096Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList4096Bytes a, in FixedList128Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList4096Bytes a, in FixedList128Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList128Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList128Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList4096Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList4096Bytes<T>. public FixedList4096Bytes(in FixedList128Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList4096Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList128Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes4094(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList4096Bytes<T>. public static implicit operator FixedList4096Bytes(in FixedList128Bytes other) { return new FixedList4096Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList4096Bytes a, in FixedList512Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList4096Bytes a, in FixedList512Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList512Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList512Bytes other) { return CompareTo(other) == 0; } /// /// Initializes and returns an instance of FixedList4096Bytes with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of FixedList4096Bytes<T>. public FixedList4096Bytes(in FixedList512Bytes other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResize(other.Length); } /// /// Initializes an instance of FixedList4096Bytes with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in FixedList512Bytes other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; buffer = new FixedBytes4094(); unsafe { UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes); } return 0; } /// /// Returns a new list that is a copy of another list. /// /// The list to copy. /// A new list that is a copy of the other. /// Throws if the other list's length exceeds the capacity of FixedList4096Bytes<T>. public static implicit operator FixedList4096Bytes(in FixedList512Bytes other) { return new FixedList4096Bytes(other); } /// /// Returns true if two lists are equal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for equality. /// The second list to compare for equality. /// True if the two lists are equal. public static bool operator ==(in FixedList4096Bytes a, in FixedList4096Bytes b) { unsafe { if(a.length != b.length) return false; return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0; } } /// /// Returns true if two lists are unequal. /// /// Two lists are equal if their length and bytes are equal. /// The first list to compare for inequality. /// The second list to compare for inequality. /// True if the two lists are unequal. public static bool operator !=(in FixedList4096Bytes a, in FixedList4096Bytes b) { return !(a == b); } /// /// Returns a number denoting whether this list should be placed before or after another list in a sort. /// /// A list to to compare with. /// An integer denoting the respective sort position of the list relative to the other: /// /// 0 denotes that both lists should have the same position in a sort. /// -1 denotes that this list should precede the other list in a sort. /// +1 denotes that this list should follow the other list in a sort. /// public int CompareTo(FixedList4096Bytes other) { unsafe { fixed(byte* a = &buffer.offset0000.byte0000) { byte* b = &other.buffer.offset0000.byte0000; var aa = a + FixedList.PaddingBytes(); var bb = b + FixedList.PaddingBytes(); var mini = math.min(Length, other.Length); for(var i = 0; i < mini; ++i) { var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T)); if(j != 0) return j; } return Length.CompareTo(other.Length); } } } /// /// Returns true if this list and another list are equal. /// /// Two lists are equal if their length and bytes are equal. /// The list to compare for equality. /// True if the two lists are equal. public bool Equals(FixedList4096Bytes other) { return CompareTo(other) == 0; } /// /// Returns true if the list is equal to an object. /// /// Two lists are equal if their length and bytes are equal. /// /// A FixedList*N*<T> can only be equal to another FixedList*N*<T> with the same *N* and T. /// /// An object to compare for equality. /// True if the list is equal to the object. [NotBurstCompatible] public override bool Equals(object obj) { if(obj is FixedList32Bytes aFixedList32Bytes) return Equals(aFixedList32Bytes); if(obj is FixedList64Bytes aFixedList64Bytes) return Equals(aFixedList64Bytes); if(obj is FixedList128Bytes aFixedList128Bytes) return Equals(aFixedList128Bytes); if(obj is FixedList512Bytes aFixedList512Bytes) return Equals(aFixedList512Bytes); if(obj is FixedList4096Bytes aFixedList4096Bytes) return Equals(aFixedList4096Bytes); return false; } /// /// An enumerator over the elements of a FixedList4096Bytes<T>. /// /// /// In an enumerator's initial state, `Current` cannot be read. The first call advances the enumerator to the first element. /// public struct Enumerator : IEnumerator { FixedList4096Bytes m_List; int m_Index; /// /// Initializes and returns an instance of FixedList4096Bytes<T>. /// /// The list for which to create an enumerator. public Enumerator(ref FixedList4096Bytes list) { m_List = list; m_Index = -1; } /// /// Does nothing. /// public void Dispose() { } /// /// Advances the enumerator to the next element. /// /// True if is valid to read after the call. public bool MoveNext() { m_Index++; return m_Index < m_List.Length; } /// /// Resets the enumerator to its initial state. /// public void Reset() { m_Index = -1; } /// /// The current element. /// /// The current element. public T Current => m_List[m_Index]; // Let FixedList4096Bytes indexer check for out of range. object IEnumerator.Current => Current; } /// /// Returns an enumerator for iterating over the elements of this list. /// /// An enumerator for iterating over the elements of this list. public Enumerator GetEnumerator() { return new Enumerator(ref this); } /// /// This method is not implemented. Use instead. /// /// Nothing because it always throws . /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } /// /// This method is not implemented. Use instead. /// /// Nothing because it always throws . /// Method is not implemented. IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } /// /// Provides extension methods for FixedList4096Bytes. /// [BurstCompatible] public unsafe static class FixedList4096BytesExtensions { /// /// Finds the index of the first occurrence of a particular value in this list. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate. /// The index of the first occurrence of the value. Returns -1 if no occurrence is found. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static int IndexOf(this ref FixedList4096Bytes list, U value) where T : unmanaged, IEquatable { return NativeArrayExtensions.IndexOf(list.Buffer, list.Length, value); } /// /// Returns true if a particular value is present in this list. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate. /// True if the value is present in this list. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Contains(this ref FixedList4096Bytes list, U value) where T : unmanaged, IEquatable { return list.IndexOf(value) != -1; } /// /// Removes the first occurrence of a particular value in this list. /// /// /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one. /// /// If you don't need to preserve the order of the remaining elements, is a cheaper alternative. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate and remove. /// True if the value was found and removed. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Remove(this ref FixedList4096Bytes list, U value) where T : unmanaged, IEquatable { int index = list.IndexOf(value); if (index < 0) { return false; } list.RemoveAt(index); return true; } /// /// Removes the first occurrence of a particular value in this list. /// /// /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one. /// /// This is cheaper than , but the order of the remaining elements is not preserved. /// /// The type of elements in this list. /// The value type. /// The list to search. /// The value to locate and remove. /// Returns true if the item is removed. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool RemoveSwapBack(this ref FixedList4096Bytes list, U value) where T : unmanaged, IEquatable { var index = list.IndexOf(value); if (index == -1) { return false; } list.RemoveAtSwapBack(index); return true; } } sealed class FixedList4096BytesDebugView where T : unmanaged { FixedList4096Bytes m_List; public FixedList4096BytesDebugView(FixedList4096Bytes list) { m_List = list; } public T[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of byte that does not allocate memory. /// It is 32 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=32)] [Obsolete("FixedListByte32 is deprecated, please use FixedList32Bytes instead. (UnityUpgradable) -> FixedList32Bytes", true)] public struct FixedListByte32 {} [Obsolete("FixedListByte32DebugView is deprecated. (UnityUpgradable) -> FixedList32BytesDebugView", true)] sealed class FixedListByte32DebugView { FixedList32Bytes m_List; public FixedListByte32DebugView(FixedList32Bytes list) { m_List = list; } public byte[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of byte that does not allocate memory. /// It is 64 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=64)] [Obsolete("FixedListByte64 is deprecated, please use FixedList64Bytes instead. (UnityUpgradable) -> FixedList64Bytes", true)] public struct FixedListByte64 {} [Obsolete("FixedListByte64DebugView is deprecated. (UnityUpgradable) -> FixedList64BytesDebugView", true)] sealed class FixedListByte64DebugView { FixedList64Bytes m_List; public FixedListByte64DebugView(FixedList64Bytes list) { m_List = list; } public byte[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of byte that does not allocate memory. /// It is 128 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=128)] [Obsolete("FixedListByte128 is deprecated, please use FixedList128Bytes instead. (UnityUpgradable) -> FixedList128Bytes", true)] public struct FixedListByte128 {} [Obsolete("FixedListByte128DebugView is deprecated. (UnityUpgradable) -> FixedList128BytesDebugView", true)] sealed class FixedListByte128DebugView { FixedList128Bytes m_List; public FixedListByte128DebugView(FixedList128Bytes list) { m_List = list; } public byte[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of byte that does not allocate memory. /// It is 512 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=512)] [Obsolete("FixedListByte512 is deprecated, please use FixedList512Bytes instead. (UnityUpgradable) -> FixedList512Bytes", true)] public struct FixedListByte512 {} [Obsolete("FixedListByte512DebugView is deprecated. (UnityUpgradable) -> FixedList512BytesDebugView", true)] sealed class FixedListByte512DebugView { FixedList512Bytes m_List; public FixedListByte512DebugView(FixedList512Bytes list) { m_List = list; } public byte[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of byte that does not allocate memory. /// It is 4096 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=4096)] [Obsolete("FixedListByte4096 is deprecated, please use FixedList4096Bytes instead. (UnityUpgradable) -> FixedList4096Bytes", true)] public struct FixedListByte4096 {} [Obsolete("FixedListByte4096DebugView is deprecated. (UnityUpgradable) -> FixedList4096BytesDebugView", true)] sealed class FixedListByte4096DebugView { FixedList4096Bytes m_List; public FixedListByte4096DebugView(FixedList4096Bytes list) { m_List = list; } public byte[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of int that does not allocate memory. /// It is 32 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=32)] [Obsolete("FixedListInt32 is deprecated, please use FixedList32Bytes instead. (UnityUpgradable) -> FixedList32Bytes", true)] public struct FixedListInt32 {} [Obsolete("FixedListInt32DebugView is deprecated. (UnityUpgradable) -> FixedList32BytesDebugView", true)] sealed class FixedListInt32DebugView { FixedList32Bytes m_List; public FixedListInt32DebugView(FixedList32Bytes list) { m_List = list; } public int[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of int that does not allocate memory. /// It is 64 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=64)] [Obsolete("FixedListInt64 is deprecated, please use FixedList64Bytes instead. (UnityUpgradable) -> FixedList64Bytes", true)] public struct FixedListInt64 {} [Obsolete("FixedListInt64DebugView is deprecated. (UnityUpgradable) -> FixedList64BytesDebugView", true)] sealed class FixedListInt64DebugView { FixedList64Bytes m_List; public FixedListInt64DebugView(FixedList64Bytes list) { m_List = list; } public int[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of int that does not allocate memory. /// It is 128 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=128)] [Obsolete("FixedListInt128 is deprecated, please use FixedList128Bytes instead. (UnityUpgradable) -> FixedList128Bytes", true)] public struct FixedListInt128 {} [Obsolete("FixedListInt128DebugView is deprecated. (UnityUpgradable) -> FixedList128BytesDebugView", true)] sealed class FixedListInt128DebugView { FixedList128Bytes m_List; public FixedListInt128DebugView(FixedList128Bytes list) { m_List = list; } public int[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of int that does not allocate memory. /// It is 512 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=512)] [Obsolete("FixedListInt512 is deprecated, please use FixedList512Bytes instead. (UnityUpgradable) -> FixedList512Bytes", true)] public struct FixedListInt512 {} [Obsolete("FixedListInt512DebugView is deprecated. (UnityUpgradable) -> FixedList512BytesDebugView", true)] sealed class FixedListInt512DebugView { FixedList512Bytes m_List; public FixedListInt512DebugView(FixedList512Bytes list) { m_List = list; } public int[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of int that does not allocate memory. /// It is 4096 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=4096)] [Obsolete("FixedListInt4096 is deprecated, please use FixedList4096Bytes instead. (UnityUpgradable) -> FixedList4096Bytes", true)] public struct FixedListInt4096 {} [Obsolete("FixedListInt4096DebugView is deprecated. (UnityUpgradable) -> FixedList4096BytesDebugView", true)] sealed class FixedListInt4096DebugView { FixedList4096Bytes m_List; public FixedListInt4096DebugView(FixedList4096Bytes list) { m_List = list; } public int[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of float that does not allocate memory. /// It is 32 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=32)] [Obsolete("FixedListFloat32 is deprecated, please use FixedList32Bytes instead. (UnityUpgradable) -> FixedList32Bytes", true)] public struct FixedListFloat32 {} [Obsolete("FixedListFloat32DebugView is deprecated. (UnityUpgradable) -> FixedList32BytesDebugView", true)] sealed class FixedListFloat32DebugView { FixedList32Bytes m_List; public FixedListFloat32DebugView(FixedList32Bytes list) { m_List = list; } public float[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of float that does not allocate memory. /// It is 64 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=64)] [Obsolete("FixedListFloat64 is deprecated, please use FixedList64Bytes instead. (UnityUpgradable) -> FixedList64Bytes", true)] public struct FixedListFloat64 {} [Obsolete("FixedListFloat64DebugView is deprecated. (UnityUpgradable) -> FixedList64BytesDebugView", true)] sealed class FixedListFloat64DebugView { FixedList64Bytes m_List; public FixedListFloat64DebugView(FixedList64Bytes list) { m_List = list; } public float[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of float that does not allocate memory. /// It is 128 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=128)] [Obsolete("FixedListFloat128 is deprecated, please use FixedList128Bytes instead. (UnityUpgradable) -> FixedList128Bytes", true)] public struct FixedListFloat128 {} [Obsolete("FixedListFloat128DebugView is deprecated. (UnityUpgradable) -> FixedList128BytesDebugView", true)] sealed class FixedListFloat128DebugView { FixedList128Bytes m_List; public FixedListFloat128DebugView(FixedList128Bytes list) { m_List = list; } public float[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of float that does not allocate memory. /// It is 512 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=512)] [Obsolete("FixedListFloat512 is deprecated, please use FixedList512Bytes instead. (UnityUpgradable) -> FixedList512Bytes", true)] public struct FixedListFloat512 {} [Obsolete("FixedListFloat512DebugView is deprecated. (UnityUpgradable) -> FixedList512BytesDebugView", true)] sealed class FixedListFloat512DebugView { FixedList512Bytes m_List; public FixedListFloat512DebugView(FixedList512Bytes list) { m_List = list; } public float[] Items => m_List.ToArray(); } /// /// An unmanaged, resizable list of float that does not allocate memory. /// It is 4096 bytes in size, and contains all the memory it needs. /// [Serializable] [StructLayout(LayoutKind.Explicit, Size=4096)] [Obsolete("FixedListFloat4096 is deprecated, please use FixedList4096Bytes instead. (UnityUpgradable) -> FixedList4096Bytes", true)] public struct FixedListFloat4096 {} [Obsolete("FixedListFloat4096DebugView is deprecated. (UnityUpgradable) -> FixedList4096BytesDebugView", true)] sealed class FixedListFloat4096DebugView { FixedList4096Bytes m_List; public FixedListFloat4096DebugView(FixedList4096Bytes list) { m_List = list; } public float[] Items => m_List.ToArray(); } /// /// Provides extension methods for FixedList*N*. /// public static class FixedListExtensions { /// /// Sorts the elements in this list in ascending order. /// /// The type of the elements. /// The list to sort. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public static void Sort(this ref FixedList32Bytes list) where T : unmanaged, IComparable { unsafe { fixed(byte* b = &list.buffer.offset0000.byte0000) { var c = b + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length); } } } /// /// Sorts the elements in this list using a custom comparison. /// /// The type of the elements. /// The type of the comparer. /// The list to sort. /// The comparison function used to determine the relative order of the elements. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer) })] public static void Sort(this ref FixedList32Bytes list, U comp) where T : unmanaged, IComparable where U : IComparer { unsafe { fixed(byte* b = &list.buffer.offset0000.byte0000) { var c = b + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length, comp); } } } /// /// Sorts the elements in this list in ascending order. /// /// The type of the elements. /// The list to sort. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public static void Sort(this ref FixedList64Bytes list) where T : unmanaged, IComparable { unsafe { fixed(byte* b = &list.buffer.offset0000.byte0000) { var c = b + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length); } } } /// /// Sorts the elements in this list using a custom comparison. /// /// The type of the elements. /// The type of the comparer. /// The list to sort. /// The comparison function used to determine the relative order of the elements. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer) })] public static void Sort(this ref FixedList64Bytes list, U comp) where T : unmanaged, IComparable where U : IComparer { unsafe { fixed(byte* b = &list.buffer.offset0000.byte0000) { var c = b + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length, comp); } } } /// /// Sorts the elements in this list in ascending order. /// /// The type of the elements. /// The list to sort. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public static void Sort(this ref FixedList128Bytes list) where T : unmanaged, IComparable { unsafe { fixed(byte* b = &list.buffer.offset0000.byte0000) { var c = b + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length); } } } /// /// Sorts the elements in this list using a custom comparison. /// /// The type of the elements. /// The type of the comparer. /// The list to sort. /// The comparison function used to determine the relative order of the elements. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer) })] public static void Sort(this ref FixedList128Bytes list, U comp) where T : unmanaged, IComparable where U : IComparer { unsafe { fixed(byte* b = &list.buffer.offset0000.byte0000) { var c = b + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length, comp); } } } /// /// Sorts the elements in this list in ascending order. /// /// The type of the elements. /// The list to sort. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public static void Sort(this ref FixedList512Bytes list) where T : unmanaged, IComparable { unsafe { fixed(byte* b = &list.buffer.offset0000.byte0000) { var c = b + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length); } } } /// /// Sorts the elements in this list using a custom comparison. /// /// The type of the elements. /// The type of the comparer. /// The list to sort. /// The comparison function used to determine the relative order of the elements. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer) })] public static void Sort(this ref FixedList512Bytes list, U comp) where T : unmanaged, IComparable where U : IComparer { unsafe { fixed(byte* b = &list.buffer.offset0000.byte0000) { var c = b + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length, comp); } } } /// /// Sorts the elements in this list in ascending order. /// /// The type of the elements. /// The list to sort. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })] public static void Sort(this ref FixedList4096Bytes list) where T : unmanaged, IComparable { unsafe { fixed(byte* b = &list.buffer.offset0000.byte0000) { var c = b + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length); } } } /// /// Sorts the elements in this list using a custom comparison. /// /// The type of the elements. /// The type of the comparer. /// The list to sort. /// The comparison function used to determine the relative order of the elements. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer) })] public static void Sort(this ref FixedList4096Bytes list, U comp) where T : unmanaged, IComparable where U : IComparer { unsafe { fixed(byte* b = &list.buffer.offset0000.byte0000) { var c = b + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length, comp); } } } } }