using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProjectElectricLocomotive.CollectionGenericObjects { 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 (Count > 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) { //TODO проверка позиции if(position < 0) { return null; } return _collection[position]; } public int Insert(T obj) { if(obj == null){ return -1; } for(int i = 0; i < _collection.Length; i++) { if (_collection[i] == null) { _collection[i] = obj; return i; } } return -1; } public int Insert(T obj, int position) { if(obj == null || position < 0) { return -1; } if (_collection[position] != null) { for(int i = position; i < _collection.Length; i++) { if (_collection[i] == null) { _collection[i] = obj; return position; } } for(int i = position; i > 0; i--) { if (_collection[i] == null) { _collection[i] = obj; return position; } } } // TODO проверка позиции // TODO проверка, что элемент массива по этой позиции пустой, если нет, то // ищется свободное место после этой позиции и идет вставка туда // если нет после, ищем до // TODO вставка return -1; } public T Remove(int position) { if(position < 0) { return null; } else { _collection[position] = null; } // TODO проверка позиции // TODO удаление объекта из массива, присвоив элементу массива значение null return Get(position); } public IEnumerable GetItems() { for (int i = 0; i < _collection.Length; ++i) { yield return _collection[i]; } } } }