123 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 List<T> _places;
private readonly int _maxCount;
/// <summary>
/// Количество объектов в списке
/// </summary>
public int Count => _places.Count;
private int BusyPlaces = 0;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetMachineGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="machine">Добавляемая машина</param>
/// <returns></returns>
public int Insert(T machine)
{
if (Count + 1 <= _maxCount) return Insert(machine, 0);
else return -1;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="machine">Добавляемая машина</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T machine, int position)
{
if (position >= _maxCount && position < 0)
{
return -1;
}
_places.Insert(position, machine);
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Remove(int position)
{
if (position < _maxCount && position >= 0)
{
if (_places.ElementAt(position) != null)
{
T result = _places.ElementAt(position);
_places.RemoveAt(position);
return result;
}
return null;
}
return null;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T this[int position]
{
get
{
if (position < _maxCount && position >= 0)
{
return _places.ElementAt(position);
}
return null;
}
set
{
if (position < _maxCount && position >= 0)
{
Insert(this[position], position);
}
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetMachine()
{
foreach (var machine in _places)
{
if (machine != null)
{
yield return machine;
}
else
{
yield break;
}
}
}
}
}