using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms.VisualStyles; using WarmlyLocomotive.Exceptions; using System; using System.Numerics; namespace WarmlyLocomotive.Generics { internal class SetGeneric where T : class { private readonly List _places; public int Count => _places.Count; //public int startPointer = 0; public int countMax = 0; public SetGeneric(int count) { _places = new List(count); countMax = count; } public bool Insert(T ship, IEqualityComparer? equal = null) { if (_places.Count == countMax) throw new StorageOverflowException(countMax); Insert(ship, 0, equal); return true; } public void Insert(T ship, int position, IEqualityComparer? equal = null) { if (_places.Count == countMax) throw new StorageOverflowException(countMax); if (!(position >= 0 && position <= Count)) throw new Exception($"Неверная позиция для вставки"); if (equal != null) { if (_places.Contains(ship, equal)) throw new ArgumentException(nameof(ship)); } _places.Insert(position, ship); } public void SortSet(IComparer comparer) => _places.Sort(comparer); public void Remove(int position) { if (!(position >= 0 && position < Count)) throw new WarmlyLocomotiveNotFoundException(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 < countMax)) return; _places.Insert(position, value); return; } } public IEnumerable GetWarmlyLocomotive(int? maxShip = null) { for (int i = 0; i < _places.Count; ++i) { yield return _places[i]; if (maxShip.HasValue && i == maxShip.Value) { yield break; } } } } }