2023-10-10 22:08:33 +04:00
|
|
|
|
using System;
|
2023-12-05 20:02:05 +04:00
|
|
|
|
using RoadTrain.Exceptions;
|
2023-10-10 22:08:33 +04:00
|
|
|
|
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>
|
2023-10-24 18:58:46 +04:00
|
|
|
|
private readonly List<T?> _places;
|
2023-10-10 22:08:33 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Количество объектов в массиве
|
|
|
|
|
/// </summary>
|
2023-10-24 18:58:46 +04:00
|
|
|
|
public int Count => _places.Count;
|
2023-10-10 22:08:33 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="count"></param>
|
2023-10-24 18:58:46 +04:00
|
|
|
|
private readonly int _maxCount;
|
2023-10-10 22:08:33 +04:00
|
|
|
|
public SetGeneric(int count)
|
|
|
|
|
{
|
2023-10-24 18:58:46 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T?> (count);
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int Insert(T train)
|
|
|
|
|
{
|
2023-10-24 18:58:46 +04:00
|
|
|
|
return Insert(train, 0);
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор на конкретную позицию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="car">Добавляемый автомобиль</param>
|
|
|
|
|
/// <param name="position">Позиция</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int Insert(T train, int position)
|
|
|
|
|
{
|
2023-10-24 18:58:46 +04:00
|
|
|
|
if (position < 0 || position >= _maxCount)
|
2023-12-05 19:23:48 +04:00
|
|
|
|
throw new StorageOverflowException("Невалидная операция");
|
2023-10-24 18:58:46 +04:00
|
|
|
|
|
|
|
|
|
if (Count >= _maxCount)
|
2023-12-05 19:23:48 +04:00
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
2023-10-24 18:58:46 +04:00
|
|
|
|
_places.Insert(position, train);
|
2023-10-10 22:08:33 +04:00
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление объекта из набора с конкретной позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Remove(int position)
|
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
if (position >= Count || position < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new TrainNotFoundException("Невалидная операция");
|
|
|
|
|
}
|
|
|
|
|
if (_places[position] == null)
|
|
|
|
|
{
|
|
|
|
|
throw new TrainNotFoundException(position);
|
|
|
|
|
}
|
2023-10-10 22:08:33 +04:00
|
|
|
|
_places[position] = null;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение объекта из набора по позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-10-24 18:58:46 +04:00
|
|
|
|
public T? this[int position]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-12-05 19:23:48 +04:00
|
|
|
|
if ((position < 0) || (position >= Count)) return null;
|
2023-10-24 18:58:46 +04:00
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if ((position < 0) || (position > _maxCount)) return;
|
|
|
|
|
_places[position] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<T?> GetTrains(int? maxTrains = null)
|
2023-10-10 22:08:33 +04:00
|
|
|
|
{
|
2023-10-24 18:58:46 +04:00
|
|
|
|
for (int i = 0; i < _places.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _places[i];
|
|
|
|
|
if (maxTrains.HasValue && i == maxTrains.Value)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 18:58:46 +04:00
|
|
|
|
}
|
2023-10-10 22:08:33 +04:00
|
|
|
|
}
|