2023-12-01 21:10:50 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-12-29 20:25:33 +04:00
|
|
|
|
using Airbus_Base.Exceptions;
|
2023-12-01 21:10:50 +04:00
|
|
|
|
|
|
|
|
|
namespace Airbus_Base.Generics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Параметризованный набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
internal class SetGeneric<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2023-12-02 00:24:08 +04:00
|
|
|
|
/// Список объектов, которые храним
|
2023-12-01 21:10:50 +04:00
|
|
|
|
/// </summary>
|
2023-12-02 00:24:08 +04:00
|
|
|
|
private readonly List<T?> _places;
|
2023-12-01 21:10:50 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-12-02 00:24:08 +04:00
|
|
|
|
/// Количество объектов в cписке
|
2023-12-01 21:10:50 +04:00
|
|
|
|
/// </summary>
|
2023-12-02 00:24:08 +04:00
|
|
|
|
public int Count => _places.Count;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Максимальное количество объектов в cписке
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _maxCount;
|
2023-12-01 21:10:50 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="count"></param>
|
|
|
|
|
public SetGeneric(int count)
|
|
|
|
|
{
|
2023-12-02 00:24:08 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T?>(count);
|
2023-12-01 21:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-12-29 21:27:30 +04:00
|
|
|
|
/// Сортировка набора объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="comparer"></param>
|
|
|
|
|
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
|
|
|
|
/// <summary>
|
2023-12-01 21:10:50 +04:00
|
|
|
|
/// Добавление объекта в набор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="airplane">Добавляемый самолёт</param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-29 21:27:30 +04:00
|
|
|
|
public void Insert(T airplane, IEqualityComparer<T>? equal = null)
|
2023-12-01 21:10:50 +04:00
|
|
|
|
{
|
2023-12-02 00:24:08 +04:00
|
|
|
|
if (_places.Count == _maxCount)
|
|
|
|
|
{
|
2023-12-29 20:25:33 +04:00
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
2023-12-02 00:24:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 21:27:30 +04:00
|
|
|
|
Insert(airplane, 0, equal);
|
2023-12-01 21:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор на конкретную позицию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="airplane">Добавляемый самолёт</param>
|
|
|
|
|
/// <param name="position">Позиция</param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-29 21:27:30 +04:00
|
|
|
|
public void Insert(T airplane, int position, IEqualityComparer<T>? equal = null)
|
2023-12-01 21:10:50 +04:00
|
|
|
|
{
|
2023-12-29 20:25:33 +04:00
|
|
|
|
if (_places.Count == _maxCount)
|
2023-12-01 21:10:50 +04:00
|
|
|
|
{
|
2023-12-29 20:25:33 +04:00
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
|
|
|
|
}
|
|
|
|
|
if (!(position >= 0 && position <= Count))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Неверная позиция для вставки");
|
2023-12-01 21:10:50 +04:00
|
|
|
|
}
|
2023-12-29 21:27:30 +04:00
|
|
|
|
if (equal != null)
|
|
|
|
|
{
|
|
|
|
|
if (_places.Contains(airplane, equal))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(nameof(airplane));
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-02 00:24:08 +04:00
|
|
|
|
_places.Insert(position, airplane);
|
2023-12-01 21:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление объекта из набора с конкретной позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-29 20:25:33 +04:00
|
|
|
|
public void Remove(int position)
|
2023-12-01 21:10:50 +04:00
|
|
|
|
{
|
2023-12-29 20:25:33 +04:00
|
|
|
|
if (!(position >= 0 && position < Count))
|
2023-12-01 21:10:50 +04:00
|
|
|
|
{
|
2023-12-29 20:25:33 +04:00
|
|
|
|
throw new AirplaneNotFoundException(position);
|
2023-12-01 21:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 00:24:08 +04:00
|
|
|
|
_places.RemoveAt(position);
|
2023-12-01 21:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение объекта из набора по позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-02 00:24:08 +04:00
|
|
|
|
public T? this[int position]
|
2023-12-01 21:10:50 +04:00
|
|
|
|
{
|
2023-12-02 00:24:08 +04:00
|
|
|
|
get
|
2023-12-01 21:10:50 +04:00
|
|
|
|
{
|
2023-12-29 20:25:33 +04:00
|
|
|
|
if (!(position >= 0 && position < Count))
|
2023-12-02 00:24:08 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _places[position];
|
2023-12-01 21:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 00:24:08 +04:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_places.Insert(position, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Проход по списку
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IEnumerable<T?> GetTheAirplanes(int? maxTheAirplanes = null)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _places.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _places[i];
|
|
|
|
|
if (maxTheAirplanes.HasValue && i == maxTheAirplanes.Value)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-01 21:10:50 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|