Skip to content

Instantly share code, notes, and snippets.

@RevenantX
Created May 14, 2016 13:54
Show Gist options
  • Save RevenantX/425afef3f21163e8a0162fad35c723b6 to your computer and use it in GitHub Desktop.
Save RevenantX/425afef3f21163e8a0162fad35c723b6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Game.Shared.Helpers
{
public class FastList<T> : IList<T>
{
private T[] _items;
private int _count;
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
public FastList()
{
_items = new T[8];
}
public FastList(int capacity)
{
_items = new T[capacity];
}
public void CloneFrom(T[] array)
{
_count = array.Length;
if (_count > _items.Length)
{
Array.Resize(ref _items, _items.Length * 2);
}
for (int i = 0; i < _count; i++)
{
_items[i] = array[i];
}
}
public void CloneFrom(T[] array, int count)
{
_count = count;
if (_count > _items.Length)
{
Array.Resize(ref _items, _items.Length * 2);
}
for (int i = 0; i < _count; i++)
{
_items[i] = array[i];
}
}
public void CloneFrom(List<T> list)
{
_count = list.Count;
if (_count > _items.Length)
{
Array.Resize(ref _items, _items.Length * 2);
}
for (int i = 0; i < _count; i++)
{
_items[i] = list[i];
}
}
public void CloneFrom(FastList<T> list)
{
_count = list._count;
if (_count > _items.Length)
{
Array.Resize(ref _items, _items.Length * 2);
}
for (int i = 0; i < _count; i++)
{
_items[i] = list._items[i];
}
}
public T[] ToArray()
{
T[] arr = new T[_count];
for (int i = 0; i < _count; i++)
{
arr[i] = _items[i];
}
return arr;
}
public void Add(T item)
{
if (_items.Length == _count)
{
Array.Resize(ref _items, _items.Length * 2);
}
_items[_count] = item;
_count++;
}
public void Clear()
{
_count = 0;
}
public void FullClear()
{
if (_count == 0)
return;
Array.Clear(_items, 0, _count);
_count = 0;
}
public bool Contains(T item)
{
for (int i = 0; i < _count; i++)
{
if (_items[i].Equals(item))
{
return true;
}
}
return false;
}
public void CopyTo(T[] array, int arrayIndex)
{
Array.Copy(_items, 0, array, arrayIndex, _count);
}
public bool Remove(T item)
{
for (int i = 0; i < _count; i++)
{
if (_items[i].Equals(item))
{
_items[i] = _items[_count - 1];
_items[_count - 1] = default(T);
_count--;
return true;
}
}
return false;
}
public int Count
{
get { return _count; }
}
public bool IsReadOnly
{
get { return false; }
}
public int IndexOf(T item)
{
for (int i = 0; i < _count; i++)
{
if (_items[i].Equals(item))
{
return i;
}
}
return -1;
}
public void Insert(int index, T item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
_items[index] = _items[_count - 1];
_items[_count - 1] = default(T);
_count--;
}
public T this[int index]
{
get { return _items[index]; }
set { _items[index] = value; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment