60 lines
1.7 KiB
C#

namespace ProjectBulldozer.Generics
{
internal class SetGeneric<T> where T : class
{
private readonly List<T?> _places;
public int Count => _places.Count;
/// Максимальное количество объектов в списке
private readonly int _maxCount;
public SetGeneric(int count)
{
_maxCount = count;
_places = new List<T?>(count);
}
public int Insert(T tract)
{
return Insert(tract, 0);
}
public int Insert(T tract, int position)
{
if (position < 0 || position >= _maxCount) return -1;
_places.Insert(position, tract);
return position;
}
public T? Remove(int position)
{
if (position >= Count || position < 0)
return null;
T? tmp = _places[position];
_places[position] = null;
return tmp;
}
public T? this[int position]
{
get
{
if (position < 0 || position >= Count) return null;
return _places[position];
}
set
{
if (position < 0 || position >= Count || Count == _maxCount) return;
_places.Insert(position, value);
}
}
public IEnumerable<T?> GetTractors(int? maxTracts = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTracts.HasValue && i == maxTracts.Value)
{
yield break;
}
}
}
}
}