2023-12-26 23:00:14 +04:00
|
|
|
|
using WarmlyShip.Exceptions;
|
|
|
|
|
using System;
|
2023-12-01 21:18:58 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace WarmlyShip.Generics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Параметризованный набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
internal class SetGeneric<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2023-12-02 00:46:08 +04:00
|
|
|
|
/// Список объектов, которые храним
|
2023-12-01 21:18:58 +04:00
|
|
|
|
/// </summary>
|
2023-12-02 00:46:08 +04:00
|
|
|
|
private readonly List<T?> _places;
|
2023-12-01 21:18:58 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-12-02 00:46:08 +04:00
|
|
|
|
/// Количество объектов в списке
|
2023-12-01 21:18:58 +04:00
|
|
|
|
/// </summary>
|
2023-12-02 00:46:08 +04:00
|
|
|
|
public int Count => _places.Count;
|
2023-12-01 21:18:58 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-12-02 00:46:08 +04:00
|
|
|
|
/// Максимальное количество объектов в cписке
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _maxCount;
|
|
|
|
|
|
|
|
|
|
//// <summary>
|
2023-12-01 21:18:58 +04:00
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="count"></param>
|
|
|
|
|
public SetGeneric(int count)
|
|
|
|
|
{
|
2023-12-02 00:46:08 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T?>(count);
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ship">Добавляемый корабль</param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-26 23:00:14 +04:00
|
|
|
|
public void Insert(T ship)
|
2023-12-01 21:18:58 +04:00
|
|
|
|
{
|
2023-12-02 00:46:08 +04:00
|
|
|
|
if (_places.Count == _maxCount)
|
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
2023-12-02 00:46:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Insert(ship, 0);
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор на конкретную позицию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ship">Добавляемый корабль</param>
|
|
|
|
|
/// <param name="position">Позиция</param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-26 23:00:14 +04:00
|
|
|
|
public void Insert(T ship, int position)
|
2023-12-01 21:18:58 +04:00
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
if (_places.Count == _maxCount)
|
|
|
|
|
{
|
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
|
|
|
|
}
|
|
|
|
|
if (!(position >= 0 && position <= Count))
|
2023-12-01 21:18:58 +04:00
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
throw new Exception("Неверная позиция для вставки");
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 00:46:08 +04:00
|
|
|
|
_places.Insert(position, ship);
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление объекта из набора с конкретной позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-26 23:00:14 +04:00
|
|
|
|
public void Remove(int position)
|
2023-12-01 21:18:58 +04:00
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
if (!(position >= 0 && position < Count))
|
2023-12-01 21:18:58 +04:00
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
throw new ShipNotFoundException(position);
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 00:46:08 +04:00
|
|
|
|
_places.RemoveAt(position);
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение объекта из набора по позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-02 00:46:08 +04:00
|
|
|
|
public T? this[int position]
|
2023-12-01 21:18:58 +04:00
|
|
|
|
{
|
2023-12-02 00:46:08 +04:00
|
|
|
|
get
|
2023-12-01 21:18:58 +04:00
|
|
|
|
{
|
2023-12-26 23:00:14 +04:00
|
|
|
|
if (!(position >= 0 && position < Count))
|
2023-12-02 00:46:08 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return _places[position];
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
2023-12-02 00:46:08 +04:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-01 21:18:58 +04:00
|
|
|
|
|
2023-12-02 00:46:08 +04:00
|
|
|
|
_places.Insert(position, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Проход по списку
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IEnumerable<T?> GetShips(int? maxShips = null)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _places.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _places[i];
|
|
|
|
|
if (maxShips.HasValue && i == maxShips.Value)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|