PIbd-13_Ladyagin_P.D._Simple/AirplaneWithRadar/ProjectAirplaneWithRadar/CollectionGenericObjects/MassiveGenericObjects.cs

115 lines
2.9 KiB
C#
Raw Permalink Normal View History

2024-03-13 17:15:01 +04:00
namespace ProjectAirplaneWithRadar.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;
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)
{
if(position < 0 || position >= Count)
return null;
return _collection[position];
}
public int Insert(T obj)
2024-03-13 17:15:01 +04:00
{
for(int i = 0; i < Count; i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
2024-03-13 17:15:01 +04:00
}
}
return -1;
2024-03-13 17:15:01 +04:00
}
public int Insert(T obj, int position)
2024-03-13 17:15:01 +04:00
{
if (position < 0 || position >= Count)
return -1;
2024-03-13 17:15:01 +04:00
if (_collection[position] == null)
{
_collection[position] = obj;
return position;
2024-03-13 17:15:01 +04:00
}
int temp = position + 1;
while (temp < Count)
{
if (_collection[temp] == null)
{
_collection[temp] = obj;
return temp;
2024-03-13 17:15:01 +04:00
}
temp++;
}
temp = position - 1;
while (temp > 0)
{
if (_collection[temp] == null)
{
_collection[temp] = obj;
return temp;
2024-03-13 17:15:01 +04:00
}
temp--;
}
return -1;
2024-03-13 17:15:01 +04:00
}
public T? Remove(int position)
2024-03-13 17:15:01 +04:00
{
if (position < 0 || position >= Count)
return null;
2024-03-13 17:15:01 +04:00
if(_collection[position] == null)
{
return null;
}
2024-03-13 17:15:01 +04:00
T? temp = _collection[position];
_collection[position] = null;
return temp;
2024-03-13 17:15:01 +04:00
}
}
}