Supercell.GUT/Supercell.GUT.Titan/Logic/Util/LogicArrayList.cs
BreadDEV 8c6a533918 [v0.0.2] you can enter menu now. but still early state
todo: improve code and finish base structures
2024-03-05 17:37:18 +07:00

131 lines
No EOL
2.6 KiB
C#

using Supercell.GUT.Titan.Logic.Debug;
namespace Supercell.GUT.Titan.Logic.Util;
public class LogicArrayList<T>
{
private T[] m_items;
private int m_size;
public LogicArrayList()
{
this.m_items = Array.Empty<T>();
}
public LogicArrayList(int capacity)
{
this.m_items = new T[capacity];
}
public T this[int index]
{
get
{
if (this.m_size <= index)
{
Debugger.Error($"LogicArrayList.get out of bounds {index}/{this.m_size}");
}
return this.m_items[index];
}
set
{
if (this.m_size <= index)
{
Debugger.Error($"LogicArrayList.set out of bounds {index}/{this.m_size}");
}
this.m_items[index] = value;
}
}
public void Add(T item)
{
int size = this.m_items.Length;
if (size == m_size)
{
this.EnsureCapacity(size != 0 ? size * 2 : 5);
}
this.m_items[this.m_size++] = item;
}
public void Add(int index, T item)
{
int size = this.m_items.Length;
if (size == this.m_size)
{
this.EnsureCapacity(size != 0 ? size * 2 : 5);
}
if (this.m_size > index)
{
Array.Copy(this.m_items, index, this.m_items, index + 1, this.m_size - index);
}
this.m_items[index] = item;
this.m_size += 1;
}
public void AddAll(LogicArrayList<T> array)
{
this.EnsureCapacity(this.m_size + array.m_size);
for (int i = 0, cnt = array.m_size; i < cnt; i++)
{
this.m_items[this.m_size++] = array[i];
}
}
public int IndexOf(T item)
{
return Array.IndexOf(this.m_items, item, 0, this.m_size);
}
public T Remove(int index)
{
T item = default;
if ((uint)index < m_size)
{
item = this.m_items[index];
this.m_size -= 1;
if (index != m_size)
{
Array.Copy(this.m_items, index + 1, this.m_items, index, this.m_size - index);
}
}
return item;
}
public void EnsureCapacity(int count)
{
int size = this.m_items.Length;
if (size < count)
{
Array.Resize(ref this.m_items, count);
}
}
public int Size()
{
return this.m_size;
}
public void Clear()
{
this.m_size = 0;
}
public void Destruct()
{
this.m_items = null;
this.m_size = 0;
}
}