using ProjectMonorail.CollectionGenericObjects; using ProjectMonorail.Exceptions; namespace ProjectMonorail.CollectionGenericObject; /// /// Параметризованный набор объектов /// /// Параметр: ограничение - ссылочный тип public class MassiveGenericObjects : ICollectionGenericObjects where T : class { /// /// Массив объектов, которые храним /// private T?[] _collection; public int Count => _collection.Length; public int MaxCount { get { return _collection.Length; } set { if (value > 0) { if (_collection.Length > 0) { Array.Resize(ref _collection, value); } else { _collection = new T?[value]; } } } } public CollectionType GetCollectionType => CollectionType.Massive; /// /// Конструктор /// public MassiveGenericObjects() { _collection = Array.Empty(); } public T? Get(int position) { if (position < 0 || position >= Count) { throw new PositionOutOfCollectionException(); } if (_collection[position] == null) { throw new ObjectNotFoundException(); } return _collection[position]; } public int Insert(T obj) { for (int i = 0; i < Count; i++) { if (_collection[i] == null) { _collection[i] = obj; return i; } } throw new CollectionOverflowException(); } public int Insert(T obj, int position) { if (position < 0 || position >= Count) { throw new PositionOutOfCollectionException(); } for (int i = position; i < Count; i++) { if (_collection[i] == null) { _collection[i] = obj; return i; } } for (int i = position - 1; i >= 0; i--) { if (_collection[i] == null) { _collection[i] = obj; return i; } } throw new CollectionOverflowException(); } public T Remove(int position) { if (position < 0 || position >= Count) { throw new PositionOutOfCollectionException(); } if (_collection == null) { throw new ObjectNotFoundException(); } T tmp = _collection[position]; _collection[position] = null; return tmp; } public IEnumerable GetItems() { for (int i = 0; i < _collection.Length; i++) { yield return _collection[i]; } } }