2024-03-04 09:13:51 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ProjectElectricLocomotive.CollectionGenericObjects
|
|
|
|
|
{
|
|
|
|
|
public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Массив объектов, которые храним
|
|
|
|
|
/// </summary>
|
|
|
|
|
private T?[] _collection;
|
|
|
|
|
public int Count => _collection.Length;
|
|
|
|
|
public int SetMaxCount
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value > 0)
|
|
|
|
|
{
|
|
|
|
|
if (_collection.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _collection, value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_collection = new T?[value];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MassiveGenericObjects()
|
|
|
|
|
{
|
|
|
|
|
_collection = Array.Empty<T?>();
|
|
|
|
|
}
|
|
|
|
|
public T? Get(int position)
|
|
|
|
|
{
|
2024-03-14 14:26:53 +04:00
|
|
|
|
//TODO проверка позиции
|
|
|
|
|
if(position < 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return _collection[position];
|
2024-03-04 09:13:51 +04:00
|
|
|
|
}
|
2024-03-28 08:25:13 +04:00
|
|
|
|
public int Insert(T obj)
|
2024-03-04 09:13:51 +04:00
|
|
|
|
{
|
2024-03-28 08:25:13 +04:00
|
|
|
|
if(obj == null){ return -1; }
|
2024-03-14 14:26:53 +04:00
|
|
|
|
for(int i = 0; i < _collection.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[i] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[i] = obj;
|
|
|
|
|
|
2024-03-28 08:25:13 +04:00
|
|
|
|
return i;
|
2024-03-14 14:26:53 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-28 08:25:13 +04:00
|
|
|
|
return -1;
|
2024-03-04 09:13:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 08:25:13 +04:00
|
|
|
|
public int Insert(T obj, int position)
|
2024-03-04 09:13:51 +04:00
|
|
|
|
{
|
2024-03-14 14:26:53 +04:00
|
|
|
|
if(obj == null || position < 0)
|
|
|
|
|
{
|
2024-03-28 08:25:13 +04:00
|
|
|
|
return -1;
|
2024-03-14 14:26:53 +04:00
|
|
|
|
}
|
|
|
|
|
if (_collection[position] != null)
|
|
|
|
|
{
|
|
|
|
|
for(int i = position; i < _collection.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[i] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[i] = obj;
|
2024-03-28 08:25:13 +04:00
|
|
|
|
return position;
|
2024-03-14 14:26:53 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int i = position; i > 0; i--)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[i] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[i] = obj;
|
2024-03-28 08:25:13 +04:00
|
|
|
|
return position;
|
2024-03-14 14:26:53 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-03-04 09:13:51 +04:00
|
|
|
|
// TODO проверка позиции
|
|
|
|
|
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
|
|
|
|
|
// ищется свободное место после этой позиции и идет вставка туда
|
|
|
|
|
// если нет после, ищем до
|
|
|
|
|
// TODO вставка
|
2024-03-28 08:25:13 +04:00
|
|
|
|
return -1;
|
2024-03-04 09:13:51 +04:00
|
|
|
|
}
|
2024-03-28 08:25:13 +04:00
|
|
|
|
public T Remove(int position)
|
2024-03-04 09:13:51 +04:00
|
|
|
|
{
|
2024-03-14 14:26:53 +04:00
|
|
|
|
|
|
|
|
|
if(position < 0)
|
|
|
|
|
{
|
2024-03-28 08:25:13 +04:00
|
|
|
|
return null;
|
2024-03-14 14:26:53 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_collection[position] = null;
|
|
|
|
|
}
|
2024-03-04 09:13:51 +04:00
|
|
|
|
// TODO проверка позиции
|
|
|
|
|
// TODO удаление объекта из массива, присвоив элементу массива значение null
|
2024-03-28 08:25:13 +04:00
|
|
|
|
return Get(position);
|
2024-03-04 09:13:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|