2024-05-09 22:08:07 +04:00
|
|
|
|
using ProjectStormtrooper.Exceptions;
|
|
|
|
|
using System;
|
2024-04-01 20:27:46 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ProjectStormtrooper.CollectionGenericObjects;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Параметрический набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Параметр: ограничение - ссылочный тип</typeparam>
|
|
|
|
|
public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Массив объекта, которые храним
|
|
|
|
|
/// </summary>
|
2024-04-16 10:33:52 +04:00
|
|
|
|
private T?[] _collection;
|
2024-04-01 20:27:46 +04:00
|
|
|
|
public int Count => _collection.Length;
|
|
|
|
|
|
2024-04-30 11:26:54 +04:00
|
|
|
|
public int MaxCount
|
2024-04-16 10:33:52 +04:00
|
|
|
|
{
|
2024-04-30 11:26:54 +04:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _collection.Length;
|
|
|
|
|
}
|
2024-04-16 10:33:52 +04:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value > 0)
|
|
|
|
|
{
|
|
|
|
|
if (_collection.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _collection, value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_collection = new T?[value];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-30 11:26:54 +04:00
|
|
|
|
|
|
|
|
|
public CollectionType GetCollectionType => CollectionType.Massive;
|
|
|
|
|
|
2024-04-01 20:27:46 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MassiveGenericObjects()
|
|
|
|
|
{
|
|
|
|
|
_collection = Array.Empty<T?>();
|
|
|
|
|
}
|
|
|
|
|
public T? Get(int position)
|
|
|
|
|
{
|
|
|
|
|
// проверка позиции
|
2024-05-09 22:08:07 +04:00
|
|
|
|
if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position);
|
2024-05-13 21:19:14 +04:00
|
|
|
|
if (_collection[position] == null) throw new ObjectNotFoundException(position);
|
2024-04-01 20:27:46 +04:00
|
|
|
|
return _collection[position];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T obj)
|
|
|
|
|
{
|
|
|
|
|
// TODO вставка в свободное место набора
|
|
|
|
|
int index = 0;
|
|
|
|
|
while ( index < _collection.Length)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[index] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[index] = obj;
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
index++;
|
|
|
|
|
}
|
2024-05-09 22:08:07 +04:00
|
|
|
|
throw new CollectionOverflowException(Count);
|
2024-04-01 20:27:46 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T obj, int position)
|
|
|
|
|
{
|
|
|
|
|
// TODO проверка позиции
|
|
|
|
|
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
|
|
|
|
|
// ищется свободное место после этой позиции и идет вставка туда
|
|
|
|
|
// если нет после, ищем до
|
|
|
|
|
// TODO вставка
|
2024-05-09 22:08:07 +04:00
|
|
|
|
if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position);
|
2024-04-01 20:27:46 +04:00
|
|
|
|
if (_collection[position] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[position] = obj;
|
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
int index = position + 1;
|
|
|
|
|
while(index < _collection.Length)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[index] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[index] = obj;
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
index = position - 1;
|
|
|
|
|
while ( index >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[index] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[index] = obj;
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
index--;
|
|
|
|
|
}
|
2024-05-09 22:08:07 +04:00
|
|
|
|
throw new CollectionOverflowException(Count);
|
2024-04-01 20:27:46 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? Remove(int position)
|
|
|
|
|
{
|
|
|
|
|
// TODO проверка позиции
|
|
|
|
|
// TODO удаление объекта из массива, присвоив элементу массива значение null
|
2024-05-09 22:08:07 +04:00
|
|
|
|
if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position);
|
|
|
|
|
if (_collection[position] == null) throw new ObjectNotFoundException(position);
|
2024-04-01 20:27:46 +04:00
|
|
|
|
T temp = _collection[position];
|
|
|
|
|
_collection[position] = null;
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
2024-04-30 11:26:54 +04:00
|
|
|
|
|
|
|
|
|
public IEnumerable<T?> GetItems()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _collection.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _collection[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-01 20:27:46 +04:00
|
|
|
|
}
|