Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Created April 21, 2018 22:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mjs3339/36130dbfbcab6b19a9565b1d4266b5cc to your computer and use it in GitHub Desktop.
Save mjs3339/36130dbfbcab6b19a9565b1d4266b5cc to your computer and use it in GitHub Desktop.
C# Limited Size List Class
[Serializable]
public class LimitedList<T> : IEnumerable<T>
{
private T[] _thing;
public LimitedList(int maxsize)
{
_thing = new T[maxsize];
MaxSize = maxsize;
}
private int MaxSize { get; }
public int Count { get; private set; }
public T this[int index]
{
get
{
if (index > _thing.Length - 1)
throw new Exception($"Index {index} is out of range {_thing.Length - 1}");
return _thing[index];
}
set
{
if (index > _thing.Length - 1)
throw new Exception($"Index {index} is out of range {_thing.Length - 1}");
if (index < MaxSize)
{
_thing[index] = value;
Count++;
if (Count > MaxSize)
Count = MaxSize;
}
}
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new Enumerator<T>(this);
}
public IEnumerator GetEnumerator()
{
return new Enumerator<T>(this);
}
public void Add(T value)
{
if (Count == MaxSize)
{
LeftShift();
_thing[MaxSize - 1] = value;
}
else
{
_thing[Count] = value;
Count++;
}
}
public void LeftShift()
{
var TArray = new T[MaxSize];
Array.Copy(_thing, 1, TArray, 0, MaxSize - 1);
_thing = TArray;
}
public void Clear()
{
_thing = new T[MaxSize];
Count = 0;
}
public bool Contains(T item)
{
var size = MaxSize;
var equalityComparer = EqualityComparer<T>.Default;
while (size-- > 0)
if (item == null)
{
if (_thing[size] == null)
return true;
}
else if (_thing[size] != null && equalityComparer.Equals(_thing[size], item))
{
return true;
}
return false;
}
public T[] ToArray()
{
var objArray = new T[MaxSize];
Array.Copy(_thing, 0, objArray, 0, MaxSize);
return objArray;
}
public List<T> ToList()
{
return new List<T>(_thing);
}
public HashSet<T> ToHashSet()
{
return new HashSet<T>(_thing);
}
public void CopyTo(T[] array)
{
Array.Copy(_thing, 0, array, 0, MaxSize);
}
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Enumerator<T> : IEnumerator<T>
{
private readonly LimitedList<T> thing;
private int index;
internal Enumerator(LimitedList<T> thing)
{
this.thing = thing;
index = 0;
Current = default;
}
public void Dispose()
{
}
public bool MoveNext()
{
var tthing = thing;
if (index < tthing.MaxSize)
{
Current = tthing._thing[index];
index++;
return true;
}
index = thing.MaxSize + 1;
Current = default;
return false;
}
public T Current { get; private set; }
object IEnumerator.Current => Current;
void IEnumerator.Reset()
{
index = 0;
Current = default;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment