using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShip
{
///
/// Параметризованный набор объектов
///
///
internal class SetWarmlyShipGeneric
where T : class
{
///
/// Список объектов, которые храним
///
private readonly List _places;
///
/// Количество объектов в списке
///
public int Count => _places.Count;
private readonly int _maxCount;
///
/// Конструктор
///
///
public SetWarmlyShipGeneric(int count)
{
_maxCount = count;
_places = new List();
}
///
/// Добавление объекта в набор
///
/// Добавляемый корабль
///
public int Insert(T ship)
{
if (Count < _maxCount)
return Insert(ship, 0);
return -1;
}
///
/// Добавление объекта в набор на конкретную позицию
///
/// Добавляемый автомобиль
/// Позиция
///
public int Insert(T ship, int position)
{
if (Count >= _maxCount)
{
throw new StorageOverflowException(Count);
}
if (position < 0 || position > Count)
{
return -1;
}
_places.Insert(position, ship);
return position;
}
///
/// Удаление объекта из набора с конкретной позиции
///
///
///
public T Remove(int position)
{
if (position < 0 || position >= Count)
return null;
var result = _places[position];
if (result == null)
{
throw new WarmlyShipNotFoundException(position);
}
_places.RemoveAt(position);
return result;
}
///
/// Получение объекта из набора по позиции
///
///
///
public T this[int position]
{
get
{
if (position < 0 || position >= Count)
return null;
return _places[position];
}
set
{
if (position < 0 || position >= _maxCount)
return;
_places.Add(value);
}
}
///
/// Проход по набору до первого пустого
///
///
public IEnumerable GetShip()
{
foreach (var ship in _places)
{
if (ship != null)
{
yield return ship;
}
else
{
yield break;
}
}
}
}
}