2023-12-01 21:18:58 +04:00
|
|
|
|
using System;
|
|
|
|
|
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-02 00:46:08 +04:00
|
|
|
|
public bool Insert(T ship)
|
2023-12-01 21:18:58 +04:00
|
|
|
|
{
|
2023-12-02 00:46:08 +04:00
|
|
|
|
if (_places.Count == _maxCount)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Insert(ship, 0);
|
|
|
|
|
return true;
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор на конкретную позицию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ship">Добавляемый корабль</param>
|
|
|
|
|
/// <param name="position">Позиция</param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-02 00:46:08 +04:00
|
|
|
|
public bool Insert(T ship, int position)
|
2023-12-01 21:18:58 +04:00
|
|
|
|
{
|
2023-12-02 00:46:08 +04:00
|
|
|
|
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
|
2023-12-01 21:18:58 +04:00
|
|
|
|
{
|
2023-12-02 00:46:08 +04:00
|
|
|
|
return false;
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 00:46:08 +04:00
|
|
|
|
_places.Insert(position, ship);
|
|
|
|
|
return true;
|
2023-12-01 21:18:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление объекта из набора с конкретной позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Remove(int position)
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position >= Count)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 00:46:08 +04:00
|
|
|
|
_places.RemoveAt(position);
|
2023-12-01 21:18:58 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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-02 00:46:08 +04:00
|
|
|
|
if (position < 0 || position >= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|