/// <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);
/// <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);
/// 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>();