namespace ProjectAirplaneWithRadar.CollectionGenericObjects
{
///
/// Параметризованный набор объектов
///
/// Параметр: ограничение - ссылочный тип
public class MassiveGenericObjects : ICollectionGenericObjects
where T : class
{
///
/// Массив объектов, которые храним
///
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];
}
}
}
}
///
/// Конструктор
///
public MassiveGenericObjects()
{
_collection = Array.Empty();
}
public T? Get(int position)
{
if(position < 0 || position >= Count)
return null;
return _collection[position];
}
public bool Insert(T obj)
{
for(int i = 0; i < Count; i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return true;
}
}
return false;
}
public bool Insert(T obj, int position)
{
if (position < 0 || position >= Count)
return false;
if (_collection[position] == null)
{
_collection[position] = obj;
return true;
}
int temp = position + 1;
while (temp < Count)
{
if (_collection[temp] == null)
{
_collection[temp] = obj;
return true;
}
temp++;
}
temp = position - 1;
while (temp > 0)
{
if (_collection[temp] == null)
{
_collection[temp] = obj;
return true;
}
temp--;
}
return false;
}
public bool Remove(int position)
{
if (position < 0 || position >= Count)
return false;
_collection[position] = null;
return true;
}
}
}