2024-03-21 10:52:11 +04:00
|
|
|
|
using ProjectCruiser.Drawnings;
|
|
|
|
|
|
|
|
|
|
namespace ProjectCruiser.CollectionGenericObjects
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Параметризованный набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Параметр: ограничение - ссылочный тип</typeparam>
|
|
|
|
|
public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Массив объектов, которые храним
|
|
|
|
|
/// </summary>
|
|
|
|
|
private T?[] _collection;
|
|
|
|
|
public int Count => _collection.Length;
|
2024-05-13 12:59:51 +04:00
|
|
|
|
public int MaxCount
|
2024-04-04 11:05:33 +04:00
|
|
|
|
{
|
2024-05-13 12:59:51 +04:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _collection.Length;
|
|
|
|
|
}
|
2024-04-04 11:05:33 +04:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value > 0)
|
|
|
|
|
{
|
|
|
|
|
if (_collection.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _collection, value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_collection = new T?[value];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-21 10:52:11 +04:00
|
|
|
|
|
2024-05-13 12:59:51 +04:00
|
|
|
|
public CollectionType GetCollectionType => CollectionType.Massive;
|
|
|
|
|
|
2024-03-21 10:52:11 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MassiveGenericObjects()
|
|
|
|
|
{
|
|
|
|
|
_collection = Array.Empty<T?>();
|
|
|
|
|
}
|
|
|
|
|
public T? Get(int position)
|
|
|
|
|
{
|
|
|
|
|
// TODO проверка позиции
|
|
|
|
|
if (position >= _collection.Length || position < 0)
|
2024-04-04 11:05:33 +04:00
|
|
|
|
{ return null; }
|
2024-03-21 10:52:11 +04:00
|
|
|
|
return _collection[position];
|
|
|
|
|
}
|
2024-04-04 11:05:33 +04:00
|
|
|
|
|
2024-03-21 10:52:11 +04:00
|
|
|
|
public int Insert(T obj)
|
|
|
|
|
{
|
|
|
|
|
// TODO вставка в свободное место набора
|
|
|
|
|
int index = 0;
|
|
|
|
|
while (index < _collection.Length)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[index] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[index] = obj;
|
|
|
|
|
return index;
|
|
|
|
|
}
|
2024-04-04 11:05:33 +04:00
|
|
|
|
|
2024-03-21 10:52:11 +04:00
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-04-04 11:05:33 +04:00
|
|
|
|
|
2024-03-21 10:52:11 +04:00
|
|
|
|
public int Insert(T obj, int position)
|
|
|
|
|
{
|
|
|
|
|
// TODO проверка позиции
|
|
|
|
|
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
|
2024-04-04 11:05:33 +04:00
|
|
|
|
// ищется свободное место после этой позиции и идет вставка туда
|
|
|
|
|
// если нет после, ищем до
|
2024-03-21 10:52:11 +04:00
|
|
|
|
// TODO вставка
|
|
|
|
|
if (position >= _collection.Length || position < 0)
|
|
|
|
|
{ return -1; }
|
|
|
|
|
|
|
|
|
|
if (_collection[position] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[position] = obj;
|
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
int index;
|
|
|
|
|
|
|
|
|
|
for (index = position + 1; index < _collection.Length; ++index)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[index] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[position] = obj;
|
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (index = position - 1; index >= 0; --index)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[index] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[position] = obj;
|
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-04-04 11:05:33 +04:00
|
|
|
|
|
2024-03-21 10:52:11 +04:00
|
|
|
|
public T Remove(int position)
|
|
|
|
|
{
|
|
|
|
|
// TODO проверка позиции
|
|
|
|
|
// TODO удаление объекта из массива, присвоив элементу массива значение null
|
|
|
|
|
if (position >= _collection.Length || position < 0)
|
|
|
|
|
{ return null; }
|
2024-04-04 11:05:33 +04:00
|
|
|
|
T obj = _collection[position];
|
2024-03-21 10:52:11 +04:00
|
|
|
|
_collection[position] = null;
|
2024-04-04 11:05:33 +04:00
|
|
|
|
return obj;
|
2024-03-21 10:52:11 +04:00
|
|
|
|
}
|
2024-05-13 12:59:51 +04:00
|
|
|
|
|
|
|
|
|
public IEnumerable<T?> GetItems()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _collection.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _collection[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-21 10:52:11 +04:00
|
|
|
|
}
|
|
|
|
|
}
|