<#/*THIS IS A T4 FILE - see t4_text_templating.md for what it is and how to run codegen*/#> <#@ template debug="True" #> <#@ output extension=".gen.cs" encoding="utf-8" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Globalization" #> <#@ import namespace="System.Security.Cryptography" #> //------------------------------------------------------------------------------ // // 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; using Unity.Properties; namespace Unity.Collections { [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(FixedBytes32Align8) })] [Serializable] internal struct FixedList : INativeList where T : unmanaged where U : unmanaged { [SerializeField] internal U data; internal ushort length { [MethodImpl(MethodImplOptions.AggressiveInlining)] readonly get { unsafe { fixed(void* ptr = &data) return *((ushort*)ptr); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] set { unsafe { fixed (void* ptr = &data) *((ushort*)ptr) = value; } } } internal readonly unsafe byte* buffer { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { unsafe { fixed (void* ptr = &data) return ((byte*)ptr) + UnsafeUtility.SizeOf(); } } } /// /// The current number of items in this list. /// /// The current number of items in this list. [CreateProperty] public int Length { [MethodImpl(MethodImplOptions.AggressiveInlining)] readonly 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 readonly bool IsEmpty { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => Length == 0; } internal readonly int LengthInBytes => Length * UnsafeUtility.SizeOf(); internal readonly unsafe byte* Buffer { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return buffer + 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 { [MethodImpl(MethodImplOptions.AggressiveInlining)] readonly 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] { [MethodImpl(MethodImplOptions.AggressiveInlining)] readonly get { CollectionHelper.CheckIndexInRange(index, length); unsafe { return UnsafeUtility.ReadArrayElement(Buffer, CollectionHelper.AssumePositive(index)); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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. [MethodImpl(MethodImplOptions.AggressiveInlining)] 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 . Included only for consistency with the other list types. /// If the element exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged. /// /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void Add(in T item) => AddNoResize(in 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. /// If the elements exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged. /// /// 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) => AddRangeNoResize(ptr, length); /// /// Appends an element to the end of this list. Increments the length by 1. /// /// /// If the element exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged. /// /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddNoResize(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. /// /// /// If the elements exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged. /// /// 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) { var idx = Length; Length += length; UnsafeUtility.MemCpy((T*)Buffer + idx, ptr, UnsafeUtility.SizeOf() * length); } /// /// Appends value count times to the end of this list. /// /// /// If the elements exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged. /// /// The value to add to the end of this list. /// The number of times to replicate the value. /// Thrown if the append exceeds the capacity. public unsafe void AddReplicate(in T value, int count) { var idx = Length; Length += count; fixed (T* ptr = &value) UnsafeUtility.MemCpyReplicate((T*)Buffer + idx, ptr, UnsafeUtility.SizeOf(), count); } /// /// 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); } } /// /// 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 `count`. If necessary, the capacity will be increased accordingly. /// /// If `count` equals `0`, the method does nothing. /// /// The element at index `index` will be copied to index `index + count`, the element at index `index + 1` will be copied to `index + count + 1`, and so forth. /// /// The indexes `index` up to `index + count` are not cleared: they will contain whatever values they held prior. /// /// The index of the first element that will be shifted up. /// The number of elements to insert. /// Thrown if `count` is negative. /// Thrown if `index` is out of bounds. public void InsertRange(int index, int count) => InsertRangeWithBeginEnd(index, index + count); /// /// 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; } } /// /// 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; } } /// /// Returns a managed array that is a copy of this list. /// /// A managed array that is a copy of this list. [ExcludeFromBurstCompatTesting("Returns managed array")] 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; } } } [GenerateTestsForBurstCompatibility] struct FixedList { [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int) })] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static int PaddingBytes() where T : unmanaged { return math.max(0, math.min(6, (1 << math.tzcnt(UnsafeUtility.SizeOf())) - 2)); } [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static int StorageBytes() where BUFFER : unmanaged where T : unmanaged { return UnsafeUtility.SizeOf() - UnsafeUtility.SizeOf() - PaddingBytes(); } [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })] [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static int Capacity() where BUFFER : unmanaged where T : unmanaged { return StorageBytes() / UnsafeUtility.SizeOf(); } [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })] [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")] internal static void CheckResize(int newLength) where BUFFER : unmanaged where T : unmanaged { var Capacity = Capacity(); if (newLength < 0 || newLength > Capacity) throw new IndexOutOfRangeException($"NewLength {newLength} is out of range of '{Capacity}' Capacity."); } } <# var SIZES = new int[]{32,64,128,512,4096}; for(var size = 0; size < 5; ++size) { var BYTES = SIZES[size]; var BUFFER_BYTES = BYTES; var TYPENAME = String.Format("FixedList{0}Bytes", BYTES); var OLD_TYPENAME = String.Format("FixedList{0}", BYTES); #> /// /// An unmanaged, resizable list whose content is all stored directly in the <#=BYTES#>-byte struct. Useful for small lists. /// /// The type of the elements. [Serializable] [DebuggerTypeProxy(typeof(<#=TYPENAME#>DebugView<>))] [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int) })] public struct <#=TYPENAME#> : INativeList , IEnumerable // Used by collection initializers. <# foreach(var OTHERBYTES in SIZES) { var OTHERTYPENAME = String.Format("FixedList{0}Bytes", OTHERBYTES); WriteLine(" , IEquatable<{0}>", OTHERTYPENAME); WriteLine(" , IComparable<{0}>", OTHERTYPENAME); } #> where T : unmanaged { [SerializeField] internal FixedBytes<#=BUFFER_BYTES#>Align8 data; internal ushort length { [MethodImpl(MethodImplOptions.AggressiveInlining)] readonly get { unsafe { fixed(void* ptr = &data) return *((ushort*)ptr); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] set { unsafe { fixed (void* ptr = &data) *((ushort*)ptr) = value; } } } internal readonly unsafe byte* buffer { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { unsafe { fixed (void* ptr = &data) return ((byte*)ptr) + UnsafeUtility.SizeOf(); } } } /// /// The current number of items in this list. /// /// The current number of items in this list. [CreateProperty] public int Length { [MethodImpl(MethodImplOptions.AggressiveInlining)] readonly get => length; set { FixedList.CheckResizeAlign8,T>(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 readonly bool IsEmpty { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => Length == 0; } internal int LengthInBytes => Length * UnsafeUtility.SizeOf(); /// /// Returns a pointer to the first element of the list buffer. /// /// /// The pointer returned by this method points into the internals of the target list object. It is the /// caller's responsibility to ensure that the pointer is not used after the list is destroyed or goes /// out of scope. /// /// A pointer to the first element of the list buffer. internal readonly unsafe byte* Buffer { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return buffer + 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 { [MethodImpl(MethodImplOptions.AggressiveInlining)] readonly get { return FixedList.CapacityAlign8,T>(); } 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] { [MethodImpl(MethodImplOptions.AggressiveInlining)] readonly get { CollectionHelper.CheckIndexInRange(index, length); unsafe { return UnsafeUtility.ReadArrayElement(Buffer, CollectionHelper.AssumePositive(index)); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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. [MethodImpl(MethodImplOptions.AggressiveInlining)] 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 . Included only for consistency with the other list types. /// If the element exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged. /// /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. public void Add(in T item) => AddNoResize(in 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. /// If the elements exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged. /// /// 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) => AddRangeNoResize(ptr, length); /// /// Appends an element to the end of this list. Increments the length by 1. /// /// /// If the element exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged. /// /// The element to append at the end of the list. /// Thrown if the append exceeds the capacity. [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddNoResize(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. /// /// /// If the elements exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged. /// /// 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) { var idx = Length; Length += length; UnsafeUtility.MemCpy((T*)Buffer + idx, ptr, UnsafeUtility.SizeOf() * length); } /// /// Appends value count times to the end of this list. /// /// The value to add to the end of this list. /// The number of times to replicate the value. /// Thrown if the append exceeds the capacity. public unsafe void AddReplicate(in T value, int count) { var idx = Length; Length += count; fixed (T* ptr = &value) UnsafeUtility.MemCpyReplicate((T*)Buffer + idx, ptr, UnsafeUtility.SizeOf(), count); } /// /// 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); } } /// /// 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 `count`. If necessary, the capacity will be increased accordingly. /// /// If `count` equals `0`, the method does nothing. /// /// The element at index `index` will be copied to index `index + count`, the element at index `index + 1` will be copied to `index + count + 1`, and so forth. /// /// The indexes `index` up to `index + count` are not cleared: they will contain whatever values they held prior. /// /// The index of the first element that will be shifted up. /// The number of elements to insert. /// Thrown if `count` is negative. /// Thrown if `index` is out of bounds. public void InsertRange(int index, int count) => InsertRangeWithBeginEnd(index, index + count); /// /// 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; } } /// /// 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; } } /// /// Returns a managed array that is a copy of this list. /// /// A managed array that is a copy of this list. [ExcludeFromBurstCompatTesting("Returns managed array")] 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; } } <# foreach(var OTHERBYTES in SIZES) { var OTHER_BUFFER_BYTES = OTHERBYTES; var OTHERTYPENAME = String.Format("FixedList{0}Bytes", OTHERBYTES); #> /// /// 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 <#=TYPENAME#> a, in <#=OTHERTYPENAME#> 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 <#=TYPENAME#> a, in <#=OTHERTYPENAME#> 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(<#=OTHERTYPENAME#> other) { unsafe { byte* a = buffer; byte* b = other.buffer; 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(<#=OTHERTYPENAME#> other) { return CompareTo(other) == 0; } <# if(BYTES != OTHERBYTES) { #> /// /// Initializes and returns an instance of <#=TYPENAME#> with content copied from another list. /// /// The list to copy. /// Throws if the other list's length exceeds the capacity of <#=TYPENAME#><T>. public <#=TYPENAME#>(in <#=OTHERTYPENAME#> other) { this = default; var error = Initialize(other); if(error != 0) FixedList.CheckResizeAlign8,T>(other.Length); } /// /// Initializes an instance of <#=TYPENAME#> with content copied from another list. /// /// The list to copy. /// zero on success, or non-zero on error. internal int Initialize(in <#=OTHERTYPENAME#> other) { if(other.Length > Capacity) return (int)CopyError.Truncation; length = other.length; 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 <#=TYPENAME#><T>. public static implicit operator <#=TYPENAME#>(in <#=OTHERTYPENAME#> other) { return new <#=TYPENAME#>(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. [ExcludeFromBurstCompatTesting("Takes managed object")] public override bool Equals(object obj) { <# foreach(var OTHERBYTES in SIZES) { var OTHERTYPENAME = String.Format("FixedList{0}Bytes", OTHERBYTES); WriteLine(" if(obj is {0} a{0}) return Equals(a{0});", OTHERTYPENAME); } #> return false; } /// /// An enumerator over the elements of a <#=TYPENAME#><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 { <#=TYPENAME#> m_List; int m_Index; /// /// Initializes and returns an instance of <#=TYPENAME#><T>. /// /// The list for which to create an enumerator. public Enumerator(ref <#=TYPENAME#> 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. [MethodImpl(MethodImplOptions.AggressiveInlining)] 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 { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => m_List[m_Index]; // Let <#=TYPENAME#> 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 <#=TYPENAME#>. /// [GenerateTestsForBurstCompatibility] public unsafe static class <#=TYPENAME#>Extensions { /// /// 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. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static int IndexOf(this ref <#=TYPENAME#> 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. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Contains(this ref <#=TYPENAME#> 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. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool Remove(this ref <#=TYPENAME#> 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. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })] public static bool RemoveSwapBack(this ref <#=TYPENAME#> list, U value) where T : unmanaged, IEquatable { var index = list.IndexOf(value); if (index == -1) { return false; } list.RemoveAtSwapBack(index); return true; } } sealed class <#=TYPENAME#>DebugView where T : unmanaged { <#=TYPENAME#> m_List; public <#=TYPENAME#>DebugView(<#=TYPENAME#> list) { m_List = list; } public T[] Items => m_List.ToArray(); } <# } #> <# var TYPES = new string[]{"byte","int","float"}; var TYPESIZES = new int[]{1,4,4}; for(var type = 0; type < 3; ++type) for(var size = 0; size < 5; ++size) { var BYTES = SIZES[size]; var TYPE = TYPES[type]; var TYPESIZE = TYPESIZES[type]; var BUFFER_BYTES = BYTES; var TYPENAME = String.Format("FixedList{0}{1}", new CultureInfo("en-US").TextInfo.ToTitleCase(TYPE), BYTES); var NEW_TYPENAME = $"FixedList{BYTES}Bytes<{TYPE}>"; #> <# } #> /// /// Provides extension methods for FixedList*N*. /// public static class FixedListExtensions { <# for(var size = 0; size < 5; ++size) { var BYTES = SIZES[size]; var BUFFER_BYTES = BYTES; var TYPENAME = String.Format("FixedList{0}Bytes", BYTES); #> /// /// Sorts the elements in this list in ascending order. /// /// The type of the elements. /// The list to sort. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int) })] public static void Sort(this ref <#=TYPENAME#> list) where T : unmanaged, IComparable { unsafe { var c = list.buffer + 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. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer) })] public static void Sort(this ref <#=TYPENAME#> list, U comp) where T : unmanaged, IComparable where U : IComparer { unsafe { var c = list.buffer + FixedList.PaddingBytes(); NativeSortExtension.Sort((T*)c, list.Length, comp); } } <# } #> } }