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