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