using AirBomber.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace ProjectBomber.Generics
{
///
/// Параметризованный набор объектов
///
///
internal class SetGeneric
where T : class
{
///
/// Массив объектов, которые храним
///
private readonly List _places;
///
/// Количество объектов в массиве
///
public int Count => _places.Count;
///
/// Максимальное количество объектов в списке
///
private readonly int _maxCount;
///
/// Конструктор
///
///
public SetGeneric(int count)
{
_maxCount = count;
_places = new List(count);
}
public void SortSet(IComparer comparer) => _places.Sort(comparer);
///
/// Добавление объекта в набор
///
/// Добавляемая установка
///
public bool Insert(T plane, IEqualityComparer? equal = null)
{
Insert(plane, 0, equal);
return true;
}
///
/// Добавление объекта в набор на конкретную позицию
///
/// Добавляемая установка
/// Позиция
///
public bool Insert(T plane, int position, IEqualityComparer? equal = null)
{
if (position < 0 || position >= _maxCount)
throw new BomberNotFoundException(position);
if (Count >= _maxCount)
throw new StorageOverflowException(position);
if (equal != null)
{
if (_places.Contains(plane, equal))
throw new ArgumentException(nameof(plane));
}
_places.Insert(position, plane);
return true;
}
///
/// Удаление объекта из набора с конкретной позиции
///
///
///
public bool Remove(int position)
{
/// Проверка позиции
if (position < 0 || position > _maxCount || position >= Count)
throw new BomberNotFoundException(position);
/// Удаление объекта из массива, присвоив элементу массива значение null
_places.RemoveAt(position);
return true;
}
///
/// Получение объекта из набора по позиции
///
///
///
public T? this[int position]
{
get
{
if (position < 0 || position > _maxCount)
return null;
if (_places.Count <= position)
return null;
return _places[position];
}
set
{
if (position < 0 || position > _maxCount)
return;
if (_places.Count <= position)
return;
_places[position] = value;
}
}
public IEnumerable GetPlane(int? maxPlane = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxPlane.HasValue && i == maxPlane.Value)
{
yield break;
}
}
}
}
}