using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; namespace ProjectBomber.Generics { /// /// Параметризованный набор объектов /// /// internal class SetGeneric where T : class { /// /// Массив объектов, которые храним /// private readonly List _places; /// /// Количество объектов в массиве /// public int Count => _places.Count; /// /// Максимальное количество объектов в списке /// private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { _maxCount = count; _places = new List(count); } /// /// Добавление объекта в набор /// /// Добавляемая установка /// public int Insert(T plane) { return Insert(plane, 0); } /// /// Добавление объекта в набор на конкретную позицию /// /// Добавляемая установка /// Позиция /// public int Insert(T plane, int position) { if (position < 0 || position >= _maxCount) return -1; if (Count >= _maxCount) return -1; _places.Insert(position, plane); return position; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// public bool Remove(int position) { /// Проверка позиции if (position < 0 || position >= _places.Count) return false; /// Удаление объекта из массива, присвоив элементу массива значение null _places[position] = null; return true; } /// /// Получение объекта из набора по позиции /// /// /// public T? this[int position] { get { if (position < 0 || position > _maxCount) return null; return _places[position]; } set { if (position < 0 || position > _maxCount) return; _places[position] = value; } } public IEnumerable GetPlane(int? maxPlane = null) { for (int i = 0; i < _places.Count; ++i) { yield return _places[i]; if (maxPlane.HasValue && i == maxPlane.Value) { yield break; } } } } }