mirror of
https://github.com/thebreaddev/Supercell.GUT.git
synced 2024-11-10 07:44:37 +00:00
8c6a533918
todo: improve code and finish base structures
131 lines
No EOL
2.6 KiB
C#
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;
|
|
}
|
|
} |