using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Hydroplane.Exceptions; namespace Hydroplane.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(_maxCount); } public bool Insert(T plane) { return Insert(plane, 0); } public bool Insert(T plane, int position) { if (position < 0 || position >= _maxCount) throw new StorageOverflowException("Impossible to insert"); if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); _places.Insert(0, plane); return true; } public bool Remove(int position) { if (position >= Count || position < 0) throw new PlaneNotFoundException("Invalid operation"); if (_places[position] == null) throw new PlaneNotFoundException(position); _places.RemoveAt(position); 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 GetPlanes(int? maxPlanes = null) { for (int i = 0; i < _places.Count; ++i) { yield return _places[i]; if (maxPlanes.HasValue && i == maxPlanes.Value) { yield break; } } } } }