* List<T> 구현
public class MyList<T> : IEnumerable<T>, IEnumerable
{
private T[] data;
private int capacity;
private int count;
// interface start
IEnumerator<T> IEnumerable<T>.GetEnumerator() { return new MyList<T>.Enumerator(this, count); }
IEnumerator IEnumerable.GetEnumerator() { return new MyList<T>.Enumerator(this, count); }
// interface end
public int Capacity { get { return capacity; } }
public int Count { get { return count; } }
public MyList(int capacity)
{
this.data = new T[capacity];
this.capacity = capacity;
}
public T this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
public void Add(T item)
{
if (capacity <= count)
{
int newCapacity = capacity * 2;
Array.Resize<T>(ref data, newCapacity);
Console.WriteLine($"Resized : [{capacity}] -> [{newCapacity}]");
capacity = newCapacity;
}
data[count++] = item;
}
public void Clear()
{
Array.Resize<T>(ref data, 0);
capacity = 0;
count = 0;
}
public struct Enumerator : IEnumerator<T>, IEnumerator
{
private MyList<T> parent;
private int count;
private int position;
// interface start
T IEnumerator<T>.Current { get { return parent[position]; } }
object IEnumerator.Current { get { return parent![position]!; } }
public bool MoveNext()
{
++position;
if(position >= count)
{
return false;
}
return true;
}
public void Reset() { position = -1; }
public void Dispose() { Reset(); }
// interface end
public Enumerator(MyList<T> parent, int count)
{
this.parent = parent;
this.count = count;
this.position = -1;
}
}
}