123 lines
2.7 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>
2022-10-09 13:17:48 +04:00
/// Список объектов, которые храним
2022-09-21 20:59:18 +04:00
/// </summary>
2022-10-09 13:17:48 +04:00
private readonly List<T> _places;
private readonly int _maxCount;
2022-09-21 20:59:18 +04:00
/// <summary>
2022-10-09 13:17:48 +04:00
/// Количество объектов в списке
2022-09-21 20:59:18 +04:00
/// </summary>
2022-10-09 13:17:48 +04:00
public int Count => _places.Count;
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)
{
2022-10-09 13:17:48 +04:00
_maxCount = count;
_places = new List<T>();
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
/// <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-09 13:17:48 +04:00
if (Count + 1 <= _maxCount) return Insert(machine, 0);
else return -1;
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-09 13:17:48 +04:00
if (position >= _maxCount && position < 0)
2022-10-05 10:12:04 +04:00
{
2022-10-09 13:17:48 +04:00
return -1;
2022-10-05 10:12:04 +04:00
}
2022-10-09 13:17:48 +04:00
_places.Insert(position, machine);
2022-10-05 10:12:04 +04:00
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-09 13:17:48 +04:00
if (position < _maxCount && position >= 0)
{
if (_places.ElementAt(position) != null)
{
T result = _places.ElementAt(position);
_places.RemoveAt(position);
return result;
}
return null;
}
return null;
2022-09-21 20:59:18 +04:00
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
2022-10-09 13:17:48 +04:00
public T this[int position]
2022-09-21 20:59:18 +04:00
{
2022-10-09 13:17:48 +04:00
get
{
if (position < _maxCount && position >= 0)
{
return _places.ElementAt(position);
}
return null;
}
set
{
if (position < _maxCount && position >= 0)
{
Insert(this[position], position);
}
}
2022-09-21 20:59:18 +04:00
}
2022-10-09 13:17:48 +04:00
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetMachine()
{
foreach (var machine in _places)
{
if (machine != null)
{
yield return machine;
}
else
{
yield break;
}
}
}
2022-09-21 20:59:18 +04:00
}
}