2022-09-24 17:12:16 +04:00

112 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetMachineGeneric(int count)
{
_places = new T[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="machine">Добавляемая машина</param>
/// <returns></returns>
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;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="machine">Добавляемая машина</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if(_places[position] != null)
{
_places[position] = null;
return true;
}
return false;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
{
// TODO проверка позиции
if(_places[position] == null)
{
return null;
}
return _places[position];
}
}
}