namespace ArmoredVehicle { /// /// Параметризованный набор объектов /// /// internal class SetMachineGeneric where T : class { /// /// Список объектов, которые храним /// private readonly List _places; private readonly int _maxCount; /// /// Количество объектов в списке /// public int Count => _places.Count; private int BusyPlaces = 0; /// /// Конструктор /// /// public SetMachineGeneric(int count) { _maxCount = count; _places = new List(); } /// /// Добавление объекта в набор /// /// Добавляемая машина /// public int Insert(T machine) { if (Count + 1 <= _maxCount) return Insert(machine, 0); else return -1; } /// /// Добавление объекта в набор на конкретную позицию /// /// Добавляемая машина /// Позиция /// public int Insert(T machine, int position) { if (position >= _maxCount && position < 0) { return -1; } _places.Insert(position, machine); return position; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// 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; } /// /// Получение объекта из набора по позиции /// /// /// 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); } } } /// /// Проход по набору до первого пустого /// /// public IEnumerable GetMachine() { foreach (var machine in _places) { if (machine != null) { yield return machine; } else { yield break; } } } } }