116 lines
3.5 KiB
C#
116 lines
3.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Sailboat.Generics
|
||
{
|
||
internal class SetGeneric<T> where T : class
|
||
{
|
||
/// <summary>
|
||
/// Список объектов, которые храним
|
||
/// </summary>
|
||
private readonly List<T?> _places;
|
||
/// <summary>
|
||
/// Количество объектов в массиве
|
||
/// </summary>
|
||
public int Count => _places.Count;
|
||
/// <summary>
|
||
/// Максимальное количество объектов в списке
|
||
/// </summary>
|
||
private readonly int _maxCount;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="count"></param>
|
||
public SetGeneric(int count)
|
||
{
|
||
_maxCount = count;
|
||
_places = new List<T?>(count);
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="boat">Добавляемая лодка</param>
|
||
/// <returns></returns>
|
||
public bool Insert(T boat)
|
||
{
|
||
if (_places.Count == _maxCount)
|
||
{
|
||
return false;
|
||
}
|
||
Insert(boat, 0);
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор на конкретную позицию
|
||
/// </summary>
|
||
/// <param name="boat">Добавляемая лодка</param>
|
||
/// <param name="position">Позиция</param>
|
||
/// <returns></returns>
|
||
public bool Insert(T boat, int position)
|
||
{
|
||
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
|
||
{
|
||
return false;
|
||
}
|
||
_places.Insert(position, boat);
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора с конкретной позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public bool Remove(int position)
|
||
{
|
||
if (position < 0 || position >= Count)
|
||
{
|
||
return false;
|
||
}
|
||
_places.RemoveAt(position);
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Получение объекта из набора по позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public T? this[int position]
|
||
{
|
||
get
|
||
{
|
||
if (position < 0 || position >= Count)
|
||
{
|
||
return null;
|
||
}
|
||
return _places[position];
|
||
}
|
||
set
|
||
{
|
||
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
|
||
{
|
||
return;
|
||
}
|
||
_places.Insert(position, value);
|
||
return;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Проход по списку
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public IEnumerable<T?> GetBoats(int? maxBoats = null)
|
||
{
|
||
for (int i = 0; i < _places.Count; ++i)
|
||
{
|
||
yield return _places[i];
|
||
if (maxBoats.HasValue && i == maxBoats.Value)
|
||
{
|
||
yield break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} |