85 lines
2.4 KiB
C#
85 lines
2.4 KiB
C#
namespace ArmoredVehicle
|
||
{
|
||
/// <summary>
|
||
/// Параметризованный набор объектов
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
internal class SetMachineGeneric<T>
|
||
where T : class
|
||
{
|
||
/// <summary>
|
||
/// Массив объектов, которые храним
|
||
/// </summary>
|
||
private readonly T[] _places;
|
||
/// <summary>
|
||
/// Количество объектов в массиве
|
||
/// </summary>
|
||
public int Count => _places.Length;
|
||
private int BusyPlaces = 0;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="count"></param>
|
||
public SetMachineGeneric(int count)
|
||
{
|
||
_places = new T[count];
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="machine">Добавляемая машина</param>
|
||
/// <returns></returns>
|
||
public int Insert(T machine)
|
||
{
|
||
return Insert(machine, 0);
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор на конкретную позицию
|
||
/// </summary>
|
||
/// <param name="machine">Добавляемая машина</param>
|
||
/// <param name="position">Позиция</param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора с конкретной позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public T Remove(int position)
|
||
{
|
||
if (position < 0 || position >= _places.Length) return null;
|
||
T deletemashine = _places[position];
|
||
_places[position] = null;
|
||
return deletemashine;
|
||
}
|
||
/// <summary>
|
||
/// Получение объекта из набора по позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public T Get(int position)
|
||
{
|
||
if (position < 0 || position >= _places.Length) return null;
|
||
else if (_places[position] == null) return null;
|
||
return _places[position];
|
||
}
|
||
}
|
||
} |