2023-12-13 03:10:36 +04:00

61 lines
1.6 KiB
C#

namespace AirFighter.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 fighter)
{
return Insert(fighter, 0);
}
public int Insert(T fighter, int position)
{
if (position < 0 || position > Count || Count >= _maxCount)
return -1;
_places.Insert(position, fighter);
return position;
}
public bool Remove(int position)
{
if ((position < 0) || (position > _maxCount)) return false;
_places.RemoveAt(position);
return true;
}
public T? this[int position]
{
get
{
if (position < 0 || position > _maxCount)
return null;
return _places[position];
}
set
{
if (position < 0 || position > _maxCount)
return;
_places[position] = value;
}
}
public IEnumerable<T?> GetAirFighter(int? maxAirFighter = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxAirFighter.HasValue && i == maxAirFighter.Value)
{
yield break;
}
}
}
}
}