112 lines
3.9 KiB
C#
112 lines
3.9 KiB
C#
using Cruiser.Exceptions;
|
||
using System;
|
||
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>
|
||
/// Список объектов, которые храним
|
||
/// </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="cruiser">Добавляемый лайнер</param>
|
||
/// <returns></returns>
|
||
public bool Insert(T cruiser, IEqualityComparer<T?>? equal = null) //починил код, работал неправильно
|
||
{
|
||
if (_places.Count >= _maxCount)
|
||
{
|
||
throw new StorageOverflowException(_places.Count);
|
||
}
|
||
return Insert(cruiser, 0, equal);
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора с конкретной позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public bool Remove(int position)
|
||
{
|
||
if (position < 0 || position > _places.Count)
|
||
{
|
||
throw new CruiserNotFoundException(position);
|
||
}
|
||
_places[position] = null;
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор на конкретную позицию
|
||
/// </summary>
|
||
/// <param name="cruiser">Добавляемый автомобиль</param>
|
||
/// <param name="position">Позиция</param>
|
||
/// <returns></returns>
|
||
public bool Insert(T cruiser, int position, IEqualityComparer<T?>? equal = null) //починил код, работал неправильно
|
||
{
|
||
if (position < 0 || position > Count)
|
||
throw new CruiserNotFoundException(position);
|
||
if (Count >= _maxCount)
|
||
throw new StorageOverflowException(_maxCount);
|
||
if (equal != null && _places.Contains(cruiser, equal))
|
||
throw new ArgumentException("Круизер уже имеется");
|
||
_places.Insert(position, cruiser);
|
||
return true;
|
||
}
|
||
public T? this[int position]
|
||
{
|
||
get
|
||
{
|
||
if (position < 0 || position > _maxCount)
|
||
{ return null; }
|
||
return _places[position];
|
||
}
|
||
set
|
||
{
|
||
if (position < 0 || position > _maxCount)
|
||
return;
|
||
_places[position] = value;
|
||
}
|
||
}
|
||
public IEnumerable<T?> GetCruisers(int? maxCruisers = null)
|
||
{
|
||
for (int i = 0; i < _places.Count; ++i)
|
||
{
|
||
yield return _places[i];
|
||
if (maxCruisers.HasValue && i == maxCruisers.Value)
|
||
{
|
||
yield break;
|
||
}
|
||
}
|
||
}
|
||
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
||
}
|
||
}
|