123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359 |
- <#/*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" #>
-
- //------------------------------------------------------------------------------
- // <auto-generated>
- // 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.
- // </auto-generated>
- //------------------------------------------------------------------------------
-
- 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<T,U>
- : INativeList<T>
- 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<ushort>();
- }
- }
- }
-
- /// <summary>
- /// The current number of items in this list.
- /// </summary>
- /// <value>The current number of items in this list.</value>
- [CreateProperty]
- public int Length
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- readonly get => length;
-
- set
- {
- FixedList.CheckResize<U,T>(value);
- length = (ushort)value;
- }
- }
-
- /// <summary>
- /// A property in order to display items in the Entity Inspector.
- /// </summary>
- [CreateProperty] IEnumerable<T> Elements => this.ToArray();
-
- /// <summary>
- /// Whether the list is empty.
- /// </summary>
- /// <value>True if this string has no characters or if the container has not been constructed.</value>
- public readonly bool IsEmpty
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => Length == 0;
- }
-
- internal readonly int LengthInBytes => Length * UnsafeUtility.SizeOf<T>();
-
- internal readonly unsafe byte* Buffer
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return buffer + FixedList.PaddingBytes<T>();
- }
- }
-
- /// <summary>
- /// The number of elements that can fit in this list.
- /// </summary>
- /// <value>The number of elements that can fit in this list.</value>
- /// <remarks>The capacity of a FixedList cannot be changed. The setter is included only for conformity with <see cref="INativeList{T}"/>.</remarks>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the new value does not match the current capacity.</exception>
- public int Capacity
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- readonly get
- {
- return FixedList.Capacity<U,T>();
- }
-
- set
- {
- CollectionHelper.CheckCapacityInRange(value, Length);
- }
- }
-
- /// <summary>
- /// The element at a given index.
- /// </summary>
- /// <param name="index">An index.</param>
- /// <value>The value to store at the index.</value>
- /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public T this[int index]
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- readonly get
- {
- CollectionHelper.CheckIndexInRange(index, length);
- unsafe
- {
- return UnsafeUtility.ReadArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index));
- }
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- CollectionHelper.CheckIndexInRange(index, length);
- unsafe
- {
- UnsafeUtility.WriteArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index), value);
- }
- }
- }
-
- /// <summary>
- /// Returns the element at a given index.
- /// </summary>
- /// <param name="index">An index.</param>
- /// <returns>A reference to the element at the index.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public ref T ElementAt(int index)
- {
- CollectionHelper.CheckIndexInRange(index, length);
- unsafe
- {
- return ref UnsafeUtility.ArrayElementAsRef<T>(Buffer, index);
- }
- }
-
- /// <summary>
- /// Returns the hash code of this list.
- /// </summary>
- /// <remarks>
- /// 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.</remarks>
- /// <returns>The hash code of this list.</returns>
- public override int GetHashCode()
- {
- unsafe
- {
- return (int)CollectionHelper.Hash(Buffer, LengthInBytes);
- }
- }
-
- /// <summary>
- /// Appends an element to the end of this list. Increments the length by 1.
- /// </summary>
- /// <remarks>
- /// The same as <see cref="AddNoResize"/>. Included only for consistency with the other list types.
- /// If the element exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged.
- /// </remarks>
- /// <param name="item">The element to append at the end of the list.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
- public void Add(in T item) => AddNoResize(in item);
-
- /// <summary>
- /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
- /// </summary>
- /// <remarks>
- /// The same as <see cref="AddRangeNoResize"/>. Included only for consistency with the other list types.
- /// If the elements exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged.
- /// </remarks>
- /// <param name="ptr">A buffer.</param>
- /// <param name="length">The number of elements from the buffer to append.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
- public unsafe void AddRange(void* ptr, int length) => AddRangeNoResize(ptr, length);
-
- /// <summary>
- /// Appends an element to the end of this list. Increments the length by 1.
- /// </summary>
- /// <remarks>
- /// If the element exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged.
- /// </remarks>
- /// <param name="item">The element to append at the end of the list.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void AddNoResize(in T item)
- {
- this[Length++] = item;
- }
-
- /// <summary>
- /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
- /// </summary>
- /// <remarks>
- /// If the elements exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged.
- /// </remarks>
- /// <param name="ptr">A buffer.</param>
- /// <param name="length">The number of elements from the buffer to append.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
- public unsafe void AddRangeNoResize(void* ptr, int length)
- {
- var idx = Length;
- Length += length;
- UnsafeUtility.MemCpy((T*)Buffer + idx, ptr, UnsafeUtility.SizeOf<T>() * length);
- }
-
- /// <summary>
- /// Appends value count times to the end of this list.
- /// </summary>
- /// <remarks>
- /// If the elements exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged.
- /// </remarks>
- /// <param name="value">The value to add to the end of this list.</param>
- /// <param name="count">The number of times to replicate the value.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
- 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<T>(), count);
- }
-
- /// <summary>
- /// Sets the length to 0.
- /// </summary>
- /// <remarks> Does *not* zero out the bytes.</remarks>
- public void Clear()
- {
- Length = 0;
- }
-
- /// <summary>
- /// Shifts elements toward the end of this list, increasing its length.
- /// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
- /// <param name="begin">The index of the first element that will be shifted up.</param>
- /// <param name="end">The index where the first shifted element will end up.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the new length exceeds the capacity.</exception>
- 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<T>();
- unsafe
- {
- byte *b = Buffer;
- byte *dest = b + end * UnsafeUtility.SizeOf<T>();
- byte *src = b + begin * UnsafeUtility.SizeOf<T>();
- UnsafeUtility.MemMove(dest, src, bytesToCopy);
- }
- }
-
- /// <summary>
- /// Shifts elements toward the end of this list, increasing its length.
- /// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
- /// <param name="index">The index of the first element that will be shifted up.</param>
- /// <param name="count">The number of elements to insert.</param>
- /// <exception cref="ArgumentException">Thrown if `count` is negative.</exception>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if `index` is out of bounds.</exception>
- public void InsertRange(int index, int count) => InsertRangeWithBeginEnd(index, index + count);
-
- /// <summary>
- /// Inserts a single element at an index. Increments the length by 1.
- /// </summary>
- /// <param name="index">The index at which to insert the element.</param>
- /// <param name="item">The element to insert.</param>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public void Insert(int index, in T item)
- {
- InsertRangeWithBeginEnd(index, index+1);
- this[index] = item;
- }
-
- /// <summary>
- /// Copies the last element of this list to an index. Decrements the length by 1.
- /// </summary>
- /// <remarks>Useful as a cheap way to remove elements from a list when you don't care about preserving order.</remarks>
- /// <param name="index">The index to overwrite with the last element.</param>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public void RemoveAtSwapBack(int index)
- {
- RemoveRangeSwapBack(index, 1);
- }
-
- /// <summary>
- /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
- /// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
- /// <param name="index">The first index of the destination range.</param>
- /// <param name="count">The number of elements to copy and the amount by which to decrement the length.</param>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public void RemoveRangeSwapBack(int index, int count)
- {
- if (count > 0)
- {
- int copyFrom = math.max(Length - count, index + count);
-
- unsafe
- {
- var sizeOf = UnsafeUtility.SizeOf<T>();
- void* dst = Buffer + index * sizeOf;
- void* src = Buffer + copyFrom * sizeOf;
- UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
- }
-
- Length -= count;
- }
- }
-
- /// <summary>
- /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1.
- /// </summary>
- /// <param name="index">The index of the element to remove.</param>
- /// <remarks>
- /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element.
- /// </remarks>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public void RemoveAt(int index)
- {
- RemoveRange(index, 1);
- }
-
- /// <summary>
- /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
- /// </summary>
- /// <remarks>
- /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements.
- /// </remarks>
- /// <param name="index">The first index of the range to remove.</param>
- /// <param name="count">The number of elements to remove.</param>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public void RemoveRange(int index, int count)
- {
- if (count > 0)
- {
- int copyFrom = math.min(index + count, Length);
-
- unsafe
- {
- var sizeOf = UnsafeUtility.SizeOf<T>();
- void* dst = Buffer + index * sizeOf;
- void* src = Buffer + copyFrom * sizeOf;
- UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
- }
-
- Length -= count;
- }
- }
-
- /// <summary>
- /// Returns a managed array that is a copy of this list.
- /// </summary>
- /// <returns>A managed array that is a copy of this list.</returns>
- [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;
- }
-
- /// <summary>
- /// Returns an array that is a copy of this list.
- /// </summary>
- /// <param name="allocator">The allocator to use.</param>
- /// <returns>An array that is a copy of this list.</returns>
- public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
- {
- unsafe
- {
- var copy = CollectionHelper.CreateNativeArray<T>(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<T>() where T : unmanaged
- {
- return math.max(0, math.min(6, (1 << math.tzcnt(UnsafeUtility.SizeOf<T>())) - 2));
- }
-
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static int StorageBytes<BUFFER,T>() where BUFFER : unmanaged where T : unmanaged
- {
- return UnsafeUtility.SizeOf<BUFFER>() - UnsafeUtility.SizeOf<ushort>() - PaddingBytes<T>();
- }
-
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static int Capacity<BUFFER,T>() where BUFFER : unmanaged where T : unmanaged
- {
- return StorageBytes<BUFFER,T>() / UnsafeUtility.SizeOf<T>();
- }
-
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
- [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
- internal static void CheckResize<BUFFER,T>(int newLength) where BUFFER : unmanaged where T : unmanaged
- {
- var Capacity = Capacity<BUFFER,T>();
- 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);
-
- #>
- /// <summary>
- /// An unmanaged, resizable list whose content is all stored directly in the <#=BYTES#>-byte struct. Useful for small lists.
- /// </summary>
- /// <typeparam name="T">The type of the elements.</typeparam>
- [Serializable]
- [DebuggerTypeProxy(typeof(<#=TYPENAME#>DebugView<>))]
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int) })]
- public struct <#=TYPENAME#><T>
- : INativeList<T>
- , IEnumerable<T> // Used by collection initializers.
- <#
- foreach(var OTHERBYTES in SIZES)
- {
- var OTHERTYPENAME = String.Format("FixedList{0}Bytes", OTHERBYTES);
- WriteLine(" , IEquatable<{0}<T>>", OTHERTYPENAME);
- WriteLine(" , IComparable<{0}<T>>", 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<ushort>();
- }
- }
- }
-
- /// <summary>
- /// The current number of items in this list.
- /// </summary>
- /// <value>The current number of items in this list.</value>
- [CreateProperty]
- public int Length
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- readonly get => length;
- set
- {
- FixedList.CheckResize<FixedBytes<#=BUFFER_BYTES#>Align8,T>(value);
- length = (ushort)value;
- }
- }
-
- /// <summary>
- /// A property in order to display items in the Entity Inspector.
- /// </summary>
- [CreateProperty] IEnumerable<T> Elements => this.ToArray();
-
- /// <summary>
- /// Whether this list is empty.
- /// </summary>
- /// <value>True if this string has no characters or if the container has not been constructed.</value>
- public readonly bool IsEmpty
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => Length == 0;
- }
-
- internal int LengthInBytes => Length * UnsafeUtility.SizeOf<T>();
-
- /// <summary>
- /// Returns a pointer to the first element of the list buffer.
- /// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
- /// <returns>A pointer to the first element of the list buffer.</returns>
- internal readonly unsafe byte* Buffer
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get
- {
- return buffer + FixedList.PaddingBytes<T>();
- }
- }
-
- /// <summary>
- /// The number of elements that can fit in this list.
- /// </summary>
- /// <value>The number of elements that can fit in this list.</value>
- /// <remarks>The capacity of a FixedList cannot be changed. The setter is included only for conformity with <see cref="INativeList{T}"/>.</remarks>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the new value does not match the current capacity.</exception>
- public int Capacity
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- readonly get
- {
- return FixedList.Capacity<FixedBytes<#=BUFFER_BYTES#>Align8,T>();
- }
-
- set
- {
- CollectionHelper.CheckCapacityInRange(value, Length);
- }
- }
-
- /// <summary>
- /// The element at a given index.
- /// </summary>
- /// <param name="index">An index.</param>
- /// <value>The value to store at the index.</value>
- /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public T this[int index]
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- readonly get
- {
- CollectionHelper.CheckIndexInRange(index, length);
- unsafe
- {
- return UnsafeUtility.ReadArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index));
- }
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- set
- {
- CollectionHelper.CheckIndexInRange(index, length);
- unsafe
- {
- UnsafeUtility.WriteArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index), value);
- }
- }
- }
-
- /// <summary>
- /// Returns the element at a given index.
- /// </summary>
- /// <param name="index">An index.</param>
- /// <returns>The list element at the index.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public ref T ElementAt(int index)
- {
- CollectionHelper.CheckIndexInRange(index, length);
- unsafe
- {
- return ref UnsafeUtility.ArrayElementAsRef<T>(Buffer, index);
- }
- }
-
- /// <summary>
- /// Returns the hash code of this list.
- /// </summary>
- /// <remarks>
- /// 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.</remarks>
- /// <returns>The hash code of this list.</returns>
- public override int GetHashCode()
- {
- unsafe
- {
- return (int)CollectionHelper.Hash(Buffer, LengthInBytes);
- }
- }
-
- /// <summary>
- /// Appends an element to the end of this list. Increments the length by 1.
- /// </summary>
- /// <remarks>
- /// The same as <see cref="AddNoResize"/>. Included only for consistency with the other list types.
- /// If the element exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged.
- /// </remarks>
- /// <param name="item">The element to append at the end of the list.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
- public void Add(in T item) => AddNoResize(in item);
-
- /// <summary>
- /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
- /// </summary>
- /// <remarks>
- /// The same as <see cref="AddRangeNoResize"/>. Included only for consistency with the other list types.
- /// If the elements exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged.
- /// </remarks>
- /// <param name="ptr">A buffer.</param>
- /// <param name="length">The number of elements from the buffer to append.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
- public unsafe void AddRange(void* ptr, int length) => AddRangeNoResize(ptr, length);
-
- /// <summary>
- /// Appends an element to the end of this list. Increments the length by 1.
- /// </summary>
- /// <remarks>
- /// If the element exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged.
- /// </remarks>
- /// <param name="item">The element to append at the end of the list.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void AddNoResize(in T item)
- {
- this[Length++] = item;
- }
-
- /// <summary>
- /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
- /// </summary>
- /// <remarks>
- /// If the elements exceeds the capacity, throws cref="IndexOutOfRangeException", and the list is unchanged.
- /// </remarks>
- /// <param name="ptr">A buffer.</param>
- /// <param name="length">The number of elements from the buffer to append.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
- public unsafe void AddRangeNoResize(void* ptr, int length)
- {
- var idx = Length;
- Length += length;
- UnsafeUtility.MemCpy((T*)Buffer + idx, ptr, UnsafeUtility.SizeOf<T>() * length);
- }
-
- /// <summary>
- /// Appends value count times to the end of this list.
- /// </summary>
- /// <param name="value">The value to add to the end of this list.</param>
- /// <param name="count">The number of times to replicate the value.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
- 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<T>(), count);
- }
-
- /// <summary>
- /// Sets the length to 0.
- /// </summary>
- /// <remarks> Does *not* zero out the bytes.</remarks>
- public void Clear()
- {
- Length = 0;
- }
-
- /// <summary>
- /// Shifts elements toward the end of this list, increasing its length.
- /// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
- /// <param name="begin">The index of the first element that will be shifted up.</param>
- /// <param name="end">The index where the first shifted element will end up.</param>
- /// <exception cref="IndexOutOfRangeException">Thrown if the new length exceeds the capacity.</exception>
- 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<T>();
- unsafe
- {
- byte *b = Buffer;
- byte *dest = b + end * UnsafeUtility.SizeOf<T>();
- byte *src = b + begin * UnsafeUtility.SizeOf<T>();
- UnsafeUtility.MemMove(dest, src, bytesToCopy);
- }
- }
-
- /// <summary>
- /// Shifts elements toward the end of this list, increasing its length.
- /// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
- /// <param name="index">The index of the first element that will be shifted up.</param>
- /// <param name="count">The number of elements to insert.</param>
- /// <exception cref="ArgumentException">Thrown if `count` is negative.</exception>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if `index` is out of bounds.</exception>
- public void InsertRange(int index, int count) => InsertRangeWithBeginEnd(index, index + count);
-
- /// <summary>
- /// Inserts a single element at an index. Increments the length by 1.
- /// </summary>
- /// <param name="index">The index at which to insert the element.</param>
- /// <param name="item">The element to insert.</param>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public void Insert(int index, in T item)
- {
- InsertRangeWithBeginEnd(index, index+1);
- this[index] = item;
- }
-
- /// <summary>
- /// Copies the last element of this list to an index. Decrements the length by 1.
- /// </summary>
- /// <remarks>Useful as a cheap way to remove elements from a list when you don't care about preserving order.</remarks>
- /// <param name="index">The index to overwrite with the last element.</param>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public void RemoveAtSwapBack(int index)
- {
- RemoveRangeSwapBack(index, 1);
- }
-
- /// <summary>
- /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
- /// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
- /// <param name="index">The first index of the destination range.</param>
- /// <param name="count">The number of elements to copy and the amount by which to decrement the length.</param>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public void RemoveRangeSwapBack(int index, int count)
- {
- if (count > 0)
- {
- int copyFrom = math.max(Length - count, index + count);
-
- unsafe
- {
- var sizeOf = UnsafeUtility.SizeOf<T>();
- void* dst = Buffer + index * sizeOf;
- void* src = Buffer + copyFrom * sizeOf;
- UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
- }
-
- Length -= count;
- }
- }
-
- /// <summary>
- /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1.
- /// </summary>
- /// <param name="index">The index of the element to remove.</param>
- /// <remarks>
- /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element.
- /// </remarks>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public void RemoveAt(int index)
- {
- RemoveRange(index, 1);
- }
-
- /// <summary>
- /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
- /// </summary>
- /// <remarks>
- /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements.
- /// </remarks>
- /// <param name="index">The first index of the range to remove.</param>
- /// <param name="count">The number of elements to remove.</param>
- /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
- public void RemoveRange(int index, int count)
- {
- if (count > 0)
- {
- int copyFrom = math.min(index + count, Length);
-
- unsafe
- {
- var sizeOf = UnsafeUtility.SizeOf<T>();
- void* dst = Buffer + index * sizeOf;
- void* src = Buffer + copyFrom * sizeOf;
- UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
- }
-
- Length -= count;
- }
- }
-
- /// <summary>
- /// Returns a managed array that is a copy of this list.
- /// </summary>
- /// <returns>A managed array that is a copy of this list.</returns>
- [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;
- }
-
- /// <summary>
- /// Returns an array that is a copy of this list.
- /// </summary>
- /// <param name="allocator">The allocator to use.</param>
- /// <returns>An array that is a copy of this list.</returns>
- public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
- {
- unsafe
- {
- var copy = CollectionHelper.CreateNativeArray<T>(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);
- #>
-
- /// <summary>
- /// Returns true if two lists are equal.
- /// </summary>
- /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
- /// <param name="a">The first list to compare for equality.</param>
- /// <param name="b">The second list to compare for equality.</param>
- /// <returns>True if the two lists are equal.</returns>
- public static bool operator ==(in <#=TYPENAME#><T> a, in <#=OTHERTYPENAME#><T> b)
- {
- unsafe
- {
- if(a.length != b.length)
- return false;
- return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
- }
- }
-
- /// <summary>
- /// Returns true if two lists are unequal.
- /// </summary>
- /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
- /// <param name="a">The first list to compare for inequality.</param>
- /// <param name="b">The second list to compare for inequality.</param>
- /// <returns>True if the two lists are unequal.</returns>
- public static bool operator !=(in <#=TYPENAME#><T> a, in <#=OTHERTYPENAME#><T> b)
- {
- return !(a == b);
- }
-
- /// <summary>
- /// Returns a number denoting whether this list should be placed before or after another list in a sort.
- /// </summary>
- /// <param name="other">A list to to compare with.</param>
- /// <returns>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.
- /// </returns>
- public int CompareTo(<#=OTHERTYPENAME#><T> other)
- {
- unsafe
- {
- byte* a = buffer;
- byte* b = other.buffer;
- var aa = a + FixedList.PaddingBytes<T>();
- var bb = b + FixedList.PaddingBytes<T>();
- 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);
- }
- }
-
- /// <summary>
- /// Returns true if this list and another list are equal.
- /// </summary>
- /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
- /// <param name="other">The list to compare for equality.</param>
- /// <returns>True if the two lists are equal.</returns>
- public bool Equals(<#=OTHERTYPENAME#><T> other)
- {
- return CompareTo(other) == 0;
- }
-
- <#
- if(BYTES != OTHERBYTES)
- {
- #>
-
- /// <summary>
- /// Initializes and returns an instance of <#=TYPENAME#> with content copied from another list.
- /// </summary>
- /// <param name="other">The list to copy.</param>
- /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of <#=TYPENAME#><T>.</exception>
- public <#=TYPENAME#>(in <#=OTHERTYPENAME#><T> other)
- {
- this = default;
- var error = Initialize(other);
- if(error != 0)
- FixedList.CheckResize<FixedBytes<#=BUFFER_BYTES#>Align8,T>(other.Length);
- }
-
- /// <summary>
- /// Initializes an instance of <#=TYPENAME#> with content copied from another list.
- /// </summary>
- /// <param name="other">The list to copy.</param>
- /// <returns>zero on success, or non-zero on error.</returns>
- internal int Initialize(in <#=OTHERTYPENAME#><T> other)
- {
- if(other.Length > Capacity)
- return (int)CopyError.Truncation;
- length = other.length;
- unsafe
- {
- UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
- }
- return 0;
- }
-
- /// <summary>
- /// Returns a new list that is a copy of another list.
- /// </summary>
- /// <param name="other">The list to copy.</param>
- /// <returns>A new list that is a copy of the other.</returns>
- /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of <#=TYPENAME#><T>.</exception>
- public static implicit operator <#=TYPENAME#><T>(in <#=OTHERTYPENAME#><T> other)
- {
- return new <#=TYPENAME#><T>(other);
- }
- <#
- }
- }
- #>
-
- /// <summary>
- /// Returns true if the list is equal to an object.
- /// </summary>
- /// <remarks>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.
- /// </remarks>
- /// <param name="obj">An object to compare for equality.</param>
- /// <returns>True if the list is equal to the object.</returns>
- [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}<T> a{0}) return Equals(a{0});", OTHERTYPENAME);
- }
- #>
- return false;
- }
-
- /// <summary>
- /// An enumerator over the elements of a <#=TYPENAME#><T>.
- /// </summary>
- /// <remarks>
- /// In an enumerator's initial state, `Current` cannot be read. The first <see cref="MoveNext"/> call advances the enumerator to the first element.
- /// </remarks>
- public struct Enumerator : IEnumerator<T>
- {
- <#=TYPENAME#><T> m_List;
- int m_Index;
-
- /// <summary>
- /// Initializes and returns an instance of <#=TYPENAME#><T>.
- /// </summary>
- /// <param name="list">The list for which to create an enumerator.</param>
- public Enumerator(ref <#=TYPENAME#><T> list)
- {
- m_List = list;
- m_Index = -1;
- }
-
- /// <summary>
- /// Does nothing.
- /// </summary>
- public void Dispose()
- {
- }
-
- /// <summary>
- /// Advances the enumerator to the next element.
- /// </summary>
- /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool MoveNext()
- {
- m_Index++;
- return m_Index < m_List.Length;
- }
-
- /// <summary>
- /// Resets the enumerator to its initial state.
- /// </summary>
- public void Reset()
- {
- m_Index = -1;
- }
-
- /// <summary>
- /// The current element.
- /// </summary>
- /// <value>The current element.</value>
- public T Current
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => m_List[m_Index]; // Let <#=TYPENAME#><T> indexer check for out of range.
- }
-
- object IEnumerator.Current => Current;
- }
-
- /// <summary>
- /// Returns an enumerator for iterating over the elements of this list.
- /// </summary>
- /// <returns>An enumerator for iterating over the elements of this list.</returns>
- public Enumerator GetEnumerator()
- {
- return new Enumerator(ref this);
- }
-
- /// <summary>
- /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
- /// </summary>
- /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
- /// <exception cref="NotImplementedException">Method is not implemented.</exception>
- IEnumerator IEnumerable.GetEnumerator()
- {
- throw new NotImplementedException();
- }
-
- /// <summary>
- /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
- /// </summary>
- /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
- /// <exception cref="NotImplementedException">Method is not implemented.</exception>
- IEnumerator<T> IEnumerable<T>.GetEnumerator()
- {
- throw new NotImplementedException();
- }
- }
-
- /// <summary>
- /// Provides extension methods for <#=TYPENAME#>.
- /// </summary>
- [GenerateTestsForBurstCompatibility]
- public unsafe static class <#=TYPENAME#>Extensions
- {
- /// <summary>
- /// Finds the index of the first occurrence of a particular value in this list.
- /// </summary>
- /// <typeparam name="T">The type of elements in this list.</typeparam>
- /// <typeparam name="U">The value type.</typeparam>
- /// <param name="list">The list to search.</param>
- /// <param name="value">The value to locate.</param>
- /// <returns>The index of the first occurrence of the value. Returns -1 if no occurrence is found.</returns>
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
- public static int IndexOf<T, U>(this ref <#=TYPENAME#><T> list, U value) where T : unmanaged, IEquatable<U>
- {
- return NativeArrayExtensions.IndexOf<T, U>(list.Buffer, list.Length, value);
- }
-
- /// <summary>
- /// Returns true if a particular value is present in this list.
- /// </summary>
- /// <typeparam name="T">The type of elements in this list.</typeparam>
- /// <typeparam name="U">The value type.</typeparam>
- /// <param name="list">The list to search.</param>
- /// <param name="value">The value to locate.</param>
- /// <returns>True if the value is present in this list.</returns>
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
- public static bool Contains<T, U>(this ref <#=TYPENAME#><T> list, U value) where T : unmanaged, IEquatable<U>
- {
- return list.IndexOf(value) != -1;
- }
-
- /// <summary>
- /// Removes the first occurrence of a particular value in this list.
- /// </summary>
- /// <remarks>
- /// 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, <see cref="Unity.Collections.<#=TYPENAME#>Extensions.RemoveSwapBack{T, U}"/> is a cheaper alternative.
- /// </remarks>
- /// <typeparam name="T">The type of elements in this list.</typeparam>
- /// <typeparam name="U">The value type.</typeparam>
- /// <param name="list">The list to search.</param>
- /// <param name="value">The value to locate and remove.</param>
- /// <returns>True if the value was found and removed.</returns>
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
- public static bool Remove<T, U>(this ref <#=TYPENAME#><T> list, U value) where T : unmanaged, IEquatable<U>
- {
- int index = list.IndexOf(value);
- if (index < 0)
- {
- return false;
- }
-
- list.RemoveAt(index);
-
- return true;
- }
-
- /// <summary>
- /// Removes the first occurrence of a particular value in this list.
- /// </summary>
- /// <remarks>
- /// 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 <see cref="Remove"/>, but the order of the remaining elements is not preserved.
- /// </remarks>
- /// <typeparam name="T">The type of elements in this list.</typeparam>
- /// <typeparam name="U">The value type.</typeparam>
- /// <param name="list">The list to search.</param>
- /// <param name="value">The value to locate and remove.</param>
- /// <returns>Returns true if the item is removed.</returns>
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
- public static bool RemoveSwapBack<T, U>(this ref <#=TYPENAME#><T> list, U value) where T : unmanaged, IEquatable<U>
- {
- var index = list.IndexOf(value);
- if (index == -1)
- {
- return false;
- }
-
- list.RemoveAtSwapBack(index);
-
- return true;
- }
- }
-
- sealed class <#=TYPENAME#>DebugView<T> where T : unmanaged
- {
- <#=TYPENAME#><T> m_List;
- public <#=TYPENAME#>DebugView(<#=TYPENAME#><T> 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}>";
- #>
-
-
- <#
- }
- #>
- /// <summary>
- /// Provides extension methods for FixedList*N*.
- /// </summary>
- 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);
- #>
-
- /// <summary>
- /// Sorts the elements in this list in ascending order.
- /// </summary>
- /// <typeparam name="T">The type of the elements.</typeparam>
- /// <param name="list">The list to sort.</param>
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int) })]
- public static void Sort<T>(this ref <#=TYPENAME#><T> list)
- where T : unmanaged, IComparable<T>
- {
- unsafe
- {
- var c = list.buffer + FixedList.PaddingBytes<T>();
- NativeSortExtension.Sort((T*)c, list.Length);
- }
- }
-
- /// <summary>
- /// Sorts the elements in this list using a custom comparison.
- /// </summary>
- /// <typeparam name="T">The type of the elements.</typeparam>
- /// <typeparam name="U">The type of the comparer.</typeparam>
- /// <param name="list">The list to sort.</param>
- /// <param name="comp">The comparison function used to determine the relative order of the elements.</param>
- [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer<int>) })]
- public static void Sort<T, U>(this ref <#=TYPENAME#><T> list, U comp)
- where T : unmanaged, IComparable<T>
- where U : IComparer<T>
- {
- unsafe
- {
- var c = list.buffer + FixedList.PaddingBytes<T>();
- NativeSortExtension.Sort((T*)c, list.Length, comp);
- }
- }
-
- <#
- }
- #>
- }
- }
|