using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WarmlyLocomotive.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 bool Insert(T warmlylocomotive) { if (_places.Count == _maxCount) { return false; } Insert(warmlylocomotive, 0); return true; } /// /// Добавление объекта в набор на конкретную позицию /// /// Добавляемый тепловоз /// Позиция /// public bool Insert(T warmlylocomotive, int position) { if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) return false; _places.Insert(position, warmlylocomotive); return true; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// public bool Remove(int position) { if (position < 0 || position >= Count) return false; _places.RemoveAt(position); return true; } /// /// Получение объекта из набора по позиции /// /// /// 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); return; } } /// /// Проход по списку /// /// public IEnumerable GetWarmlyLocomotives(int? maxWarmlyLocomotives = null) { for (int i = 0; i < _places.Count; ++i) { yield return _places[i]; if (maxWarmlyLocomotives.HasValue && i == maxWarmlyLocomotives.Value) { yield break; } } } } }