2023-12-18 15:13:53 +04:00
|
|
|
|
using Cruiser.Exceptions;
|
|
|
|
|
using System;
|
2023-10-21 14:10:32 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics.Eventing.Reader;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Cruiser.Generics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Параметризованный набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
internal class SetGeneric<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2023-11-24 11:41:50 +04:00
|
|
|
|
/// Список объектов, которые храним
|
2023-10-21 14:10:32 +04:00
|
|
|
|
/// </summary>
|
2023-11-24 11:41:50 +04:00
|
|
|
|
private readonly List<T?> _places;
|
2023-10-21 14:10:32 +04:00
|
|
|
|
/// <summary>
|
2023-11-24 11:41:50 +04:00
|
|
|
|
/// Количество объектов в списке
|
2023-10-21 14:10:32 +04:00
|
|
|
|
/// </summary>
|
2023-11-24 11:41:50 +04:00
|
|
|
|
public int Count => _places.Count;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Максимальное количество объектов в списке
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _maxCount;
|
2023-10-21 14:10:32 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="count"></param>
|
|
|
|
|
public SetGeneric(int count)
|
|
|
|
|
{
|
2023-11-24 11:41:50 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T?>(count);
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cruiser">Добавляемый лайнер</param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-18 16:26:08 +04:00
|
|
|
|
public bool Insert(T cruiser, IEqualityComparer<T?>? equal = null) //починил код, работал неправильно, переделал на инт
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
if (_places.Count >= _maxCount)
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
throw new StorageOverflowException(_places.Count);
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
2023-12-18 16:26:08 +04:00
|
|
|
|
return Insert(cruiser, 0, equal);
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление объекта из набора с конкретной позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Remove(int position)
|
|
|
|
|
{
|
2023-11-24 11:41:50 +04:00
|
|
|
|
if (position < 0 || position > _places.Count)
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-12-18 15:13:53 +04:00
|
|
|
|
throw new CruiserNotFoundException(position);
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
|
|
|
|
_places[position] = null;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор на конкретную позицию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cruiser">Добавляемый автомобиль</param>
|
|
|
|
|
/// <param name="position">Позиция</param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-18 16:26:08 +04:00
|
|
|
|
public bool Insert(T cruiser, int position, IEqualityComparer<T?>? equal = null) //починил код, работал неправильно, переделал на инт
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-12-18 16:26:08 +04:00
|
|
|
|
if (position < 0 || position > Count)
|
2023-12-18 15:13:53 +04:00
|
|
|
|
throw new CruiserNotFoundException(position);
|
2023-12-18 16:26:08 +04:00
|
|
|
|
if (Count >= _maxCount)
|
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
|
|
|
|
if (equal != null && _places.Contains(cruiser, equal))
|
|
|
|
|
throw new ArgumentException("Круизер уже имеется");
|
2023-12-18 15:13:53 +04:00
|
|
|
|
_places.Insert(position, cruiser);
|
|
|
|
|
return true;
|
2023-11-24 11:41:50 +04:00
|
|
|
|
}
|
|
|
|
|
public T? this[int position]
|
|
|
|
|
{
|
|
|
|
|
get
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-11-24 11:41:50 +04:00
|
|
|
|
if (position < 0 || position > _maxCount)
|
|
|
|
|
{ return null; }
|
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position > _maxCount)
|
|
|
|
|
return;
|
|
|
|
|
_places[position] = value;
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-24 11:41:50 +04:00
|
|
|
|
public IEnumerable<T?> GetCruisers(int? maxCruisers = null)
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-11-24 11:41:50 +04:00
|
|
|
|
for (int i = 0; i < _places.Count; ++i)
|
2023-10-21 14:10:32 +04:00
|
|
|
|
{
|
2023-11-24 11:41:50 +04:00
|
|
|
|
yield return _places[i];
|
|
|
|
|
if (maxCruisers.HasValue && i == maxCruisers.Value)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-18 16:26:08 +04:00
|
|
|
|
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
2023-10-21 14:10:32 +04:00
|
|
|
|
}
|
|
|
|
|
}
|