namespace ProjectTank.Generics
{
///
/// Параметризованный набор объектов
///
///
internal class SetGeneric where T : class
{
///
/// Массив объектов, которые храним
///
private readonly T?[] _places;
///
/// Количество объектов в массиве
///
public int Count => _places.Length;
///
/// Конструктор
///
///
public SetGeneric(int count)
{
_places = new T?[count];
}
///
/// Добавление объекта в набор
///
/// Добавляемый автомобиль
///
public int Insert(T tank)
{
if (_places[Count-1] != null)
return -1;
return Insert(tank, 0);
}
///
/// Добавление объекта в набор на конкретную позицию
///
/// Добавляемый автомобиль
/// Позиция
///
public int Insert(T tank, int position)
{
if (!(position >= 0 && position < Count)) return -1;
if (_places[position] != null)
{
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return -1;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = tank;
return position;
}
///
/// Удаление объекта из набора с конкретной позиции
///
///
///
public bool Remove(int position)
{
if (!(position >= 0 && position < Count) || _places[position] == null)
return false;
_places[position] = null;
return true;
}
///
/// Получение объекта из набора по позиции
///
///
///
public T? Get(int position)
{
if (!(position >= 0 && position < Count)) return null;
return _places[position];
}
}
}