using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Airbus_Base.Exceptions; namespace Airbus_Base.Generics { /// /// Параметризованный набор объектов /// /// internal class SetGeneric where T : class { /// /// Список объектов, которые храним /// private readonly List _places; /// /// Количество объектов в cписке /// public int Count => _places.Count; /// /// Максимальное количество объектов в cписке /// private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { _maxCount = count; _places = new List(count); } /// /// Сортировка набора объектов /// /// public void SortSet(IComparer comparer) => _places.Sort(comparer); /// /// Добавление объекта в набор /// /// Добавляемый самолёт /// public void Insert(T airplane, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) { throw new StorageOverflowException(_maxCount); } Insert(airplane, 0, equal); } /// /// Добавление объекта в набор на конкретную позицию /// /// Добавляемый самолёт /// Позиция /// public void Insert(T airplane, int position, IEqualityComparer? equal = null) { if (_places.Count == _maxCount) { throw new StorageOverflowException(_maxCount); } if (!(position >= 0 && position <= Count)) { throw new Exception("Неверная позиция для вставки"); } if (equal != null) { if (_places.Contains(airplane, equal)) { throw new ArgumentException(nameof(airplane)); } } _places.Insert(position, airplane); } /// /// Удаление объекта из набора с конкретной позиции /// /// /// public void Remove(int position) { if (!(position >= 0 && position < Count)) { throw new AirplaneNotFoundException(position); } _places.RemoveAt(position); } /// /// Получение объекта из набора по позиции /// /// /// public T? this[int position] { get { if (!(position >= 0 && position < Count)) { return null; } return _places[position]; } set { if (!(position >= 0 && position < Count && _places.Count < _maxCount)) { return; } _places.Insert(position, value); } } /// /// Проход по списку /// /// public IEnumerable GetTheAirplanes(int? maxTheAirplanes = null) { for (int i = 0; i < _places.Count; ++i) { yield return _places[i]; if (maxTheAirplanes.HasValue && i == maxTheAirplanes.Value) { yield break; } } } } }