using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hydroplane.Generics
{
///
/// Параметризованный набор объектов
///
///
internal class SetGeneric
where T : class
{
///
/// Массив объектов, которые храним
///
private readonly T?[] _places;
///
/// Количество объектов в массиве
///
public int Count => _places.Length;
///
/// Конструктор
///
///
public SetGeneric(int count)
{
_places = new T?[count];
}
///
/// Добавление объекта в набор
///
/// Добавляемый самолёт
///
public bool Insert(T plane)
{
try
{
for (int i = _places.Length - 1; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = plane;
return true;
}
catch
{
return false;
}
}
///
/// Добавление объекта в набор на конкретную позицию
///
/// Добавляемый автомобиль
/// Позиция
///
public bool Insert(T plane, int position)
{
if (position < 0 || position >= _places.Count() || plane == null)
{
return false;
}
if (_places[position] == null)
{
return false;
}
int positionNull = Array.FindIndex(_places, position, x => x == null);
if (positionNull == -1)
return false;
for (int i = positionNull; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = plane;
return true;
}
///
/// Удаление объекта из набора с конкретной позиции
///
///
///
public bool Remove(int position)
{
if (position < 0 || position >= _places.Count())
return false;
_places[position] = null;
return true;
}
///
/// Получение объекта из набора по позиции
///
///
///
public T? Get(int position)
{
if (position < 0 || position >= _places.Count())
return null;
return _places[position];
}
}
}