115 lines
3.8 KiB
C#
115 lines
3.8 KiB
C#
using System;
|
||
using RoadTrain.Exceptions;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace RoadTrain.Generics
|
||
{
|
||
internal class SetGeneric<T>
|
||
where T : class
|
||
{
|
||
/// <summary>
|
||
/// Массив объектов, которые храним
|
||
/// </summary>
|
||
private readonly List<T?> _places;
|
||
/// <summary>
|
||
/// Количество объектов в массиве
|
||
/// </summary>
|
||
public int Count => _places.Count;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="count"></param>
|
||
private readonly int _maxCount;
|
||
public SetGeneric(int count)
|
||
{
|
||
_maxCount = count;
|
||
_places = new List<T?> (count);
|
||
}
|
||
/// <summary>
|
||
/// Сортировка набора объектов
|
||
/// </summary>
|
||
/// <param name="comparer"></param>
|
||
public void SortSet(IComparer<T?> comparer) =>
|
||
_places.Sort(comparer);
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int Insert(T train, IEqualityComparer<T?>? equal = null)
|
||
{
|
||
return Insert(train, 0, equal);
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор на конкретную позицию
|
||
/// </summary>
|
||
/// <param name="car">Добавляемый автомобиль</param>
|
||
/// <param name="position">Позиция</param>
|
||
/// <returns></returns>
|
||
public int Insert(T train, int position, IEqualityComparer<T?>? equal = null)
|
||
{
|
||
if (position < 0 || position >= _maxCount)
|
||
throw new StorageOverflowException("Невалидная операция");
|
||
|
||
if (Count >= _maxCount)
|
||
throw new StorageOverflowException(_maxCount);
|
||
|
||
if (equal != null && _places.Contains(train, equal))
|
||
throw new ArgumentException("Обьект уже есть в коллекции");
|
||
_places.Insert(position, train);
|
||
return position;
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора с конкретной позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public bool Remove(int position)
|
||
{
|
||
if (position >= Count || position < 0)
|
||
{
|
||
throw new TrainNotFoundException("Невалидная операция");
|
||
}
|
||
if (_places[position] == null)
|
||
{
|
||
throw new TrainNotFoundException(position);
|
||
}
|
||
_places[position] = null;
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Получение объекта из набора по позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public T? this[int position]
|
||
{
|
||
get
|
||
{
|
||
if ((position < 0) || (position >= Count)) return null;
|
||
return _places[position];
|
||
}
|
||
set
|
||
{
|
||
if ((position < 0) || (position > _maxCount)) return;
|
||
_places[position] = value;
|
||
}
|
||
}
|
||
|
||
public IEnumerable<T?> GetTrains(int? maxTrains = null)
|
||
{
|
||
for (int i = 0; i < _places.Count; ++i)
|
||
{
|
||
yield return _places[i];
|
||
if (maxTrains.HasValue && i == maxTrains.Value)
|
||
{
|
||
yield break;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|