101 lines
3.3 KiB
C#
Raw Normal View History

2023-12-02 17:11:04 +04:00
using ProjectTank.Exceptions;
2023-10-31 12:26:25 +04:00
namespace ProjectTank.Generics
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetGeneric<T> where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
2023-10-31 12:39:26 +04:00
private readonly List<T?> _places;
2023-10-31 12:26:25 +04:00
/// <summary>
/// Количество объектов в массиве
/// </summary>
2023-10-31 12:39:26 +04:00
public int Count => _places.Count;
2023-10-31 12:26:25 +04:00
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
2023-10-31 12:39:26 +04:00
private readonly int _maxCount;
2023-10-31 12:26:25 +04:00
public SetGeneric(int count)
{
2023-10-31 12:39:26 +04:00
_maxCount = count;
_places = new List<T?>(count);
2023-10-31 12:26:25 +04:00
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
2023-12-02 17:11:04 +04:00
/// <param name="tank">Добавляемый танк</param>
2023-10-31 12:26:25 +04:00
/// <returns></returns>
2023-10-31 12:39:26 +04:00
public bool Insert(T tank)
2023-10-31 12:26:25 +04:00
{
2023-10-31 12:39:26 +04:00
if (_places.Count == _maxCount)
2023-12-02 17:11:04 +04:00
throw new StorageOverflowException(_maxCount);
Insert(tank, 0);
return true;
2023-10-31 12:26:25 +04:00
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
2023-12-02 17:11:04 +04:00
/// <param name="tank">Добавляемый танк</param>
2023-10-31 12:26:25 +04:00
/// <param name="position">Позиция</param>
/// <returns></returns>
2023-10-31 12:39:26 +04:00
public bool Insert(T tank, int position)
2023-10-31 12:26:25 +04:00
{
2023-12-02 17:11:04 +04:00
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!(position >= 0 && position <= Count))
2023-10-31 12:39:26 +04:00
return false;
_places.Insert(position, tank);
return true;
2023-10-31 12:26:25 +04:00
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
2023-10-31 12:39:26 +04:00
if (!(position >= 0 && position < Count))
2023-12-02 17:11:04 +04:00
throw new TankNotFoundException(position);
2023-10-31 12:39:26 +04:00
_places.RemoveAt(position);
2023-10-31 12:26:25 +04:00
return true;
}
2023-10-31 12:39:26 +04:00
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);
}
}
2023-10-31 12:26:25 +04:00
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
2023-10-31 12:39:26 +04:00
public IEnumerable<T?> GetTanks(int? maxTanks = null)
2023-10-31 12:26:25 +04:00
{
2023-10-31 12:39:26 +04:00
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTanks.HasValue && i == maxTanks.Value)
{
yield break;
}
}
2023-10-31 12:26:25 +04:00
}
}
}