using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Artilleries { internal class SetArtilleriesGeneric where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; private readonly int _maxCount; public SetArtilleriesGeneric(int count) { _maxCount = count; _places = new List(); } public int Insert(T artillery) { return Insert(artillery, 0); } public int Insert(T artillery, int position) { if (Count == _maxCount) { throw new StorageOverflowException(_maxCount); } if (position < 0 || position > Count) { return -1; } if (_places.Contains(artillery)) { return -1; } _places.Insert(position, artillery); return position; } public T Remove(int position) { if (position < 0 || position >= Count) { return null; } var result = _places[position]; if (result == null) { throw new ArtilleryNotFoundException(position); } _places.RemoveAt(position); return result; } public T this[int position] { get { if (position < 0 || position >= Count) { return null; } return _places[position]; } set { if (position >= 0 && position < Count) { Insert(value, position); } } } public IEnumerable GetArtilleries() { foreach (var artillery in _places) { if (artillery != null) { yield return artillery; } else { yield break; } } } } }