85 lines
2.4 KiB
C#
Raw Normal View History

2022-09-21 20:59:18 +04:00
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;
2022-10-05 10:12:04 +04:00
private int BusyPlaces = 0;
2022-09-21 20:59:18 +04:00
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetMachineGeneric(int count)
{
_places = new T[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
2022-09-24 17:12:16 +04:00
/// <param name="machine">Добавляемая машина</param>
2022-09-21 20:59:18 +04:00
/// <returns></returns>
2022-10-05 10:12:04 +04:00
public int Insert(T machine)
2022-09-21 20:59:18 +04:00
{
2022-10-05 10:12:04 +04:00
return Insert(machine, 0);
2022-09-21 20:59:18 +04:00
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
2022-09-24 17:12:16 +04:00
/// <param name="machine">Добавляемая машина</param>
2022-09-21 20:59:18 +04:00
/// <param name="position">Позиция</param>
/// <returns></returns>
2022-10-05 10:12:04 +04:00
public int Insert(T machine, int position)
2022-09-21 20:59:18 +04:00
{
2022-10-05 10:12:04 +04:00
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)
2022-09-24 17:12:16 +04:00
{
2022-10-05 10:12:04 +04:00
if (_places[i] == null && _places[i - 1] != null)
2022-09-24 17:12:16 +04:00
{
_places[i] = _places[i - 1];
2022-10-05 10:12:04 +04:00
_places[i - 1] = null;
2022-09-24 17:12:16 +04:00
}
}
2022-10-05 10:12:04 +04:00
}
_places[position] = machine;
return position;
2022-09-21 20:59:18 +04:00
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
2022-10-05 10:12:04 +04:00
public T Remove(int position)
2022-09-21 20:59:18 +04:00
{
2022-10-05 10:12:04 +04:00
if (position < 0 || position >= _places.Length) return null;
T deletemashine = _places[position];
_places[position] = null;
return deletemashine;
2022-09-21 20:59:18 +04:00
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
{
2022-10-05 10:12:04 +04:00
if (position < 0 || position >= _places.Length) return null;
else if (_places[position] == null) return null;
2022-09-21 20:59:18 +04:00
return _places[position];
}
}
}