2024-10-03 01:40:53 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-10-03 01:45:35 +04:00
|
|
|
|
using ProjectGasolineTanker.Exceptions;
|
2024-10-03 01:40:53 +04:00
|
|
|
|
|
|
|
|
|
namespace ProjectGasolineTanker.Generic
|
|
|
|
|
{
|
|
|
|
|
internal class SetGeneric<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// список объектов
|
2024-10-03 01:42:04 +04:00
|
|
|
|
private readonly List<T?> _places;
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// кол-во объектов
|
2024-10-03 01:42:04 +04:00
|
|
|
|
public int Count => _places.Count;
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// максимальное количество
|
2024-10-03 01:42:04 +04:00
|
|
|
|
private readonly int _maxCount;
|
2024-10-03 01:40:53 +04:00
|
|
|
|
public SetGeneric(int count)
|
|
|
|
|
{
|
2024-10-03 01:42:04 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T?>(count);
|
2024-10-03 01:40:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// Добавление объекта в начало
|
2024-10-03 01:46:41 +04:00
|
|
|
|
public int Insert(T truck, IEqualityComparer<T?>? equal = null)
|
2024-10-03 01:45:35 +04:00
|
|
|
|
{
|
2024-10-03 01:46:41 +04:00
|
|
|
|
return Insert(truck, 0, equal);
|
2024-10-03 01:40:53 +04:00
|
|
|
|
}
|
2024-10-03 01:46:41 +04:00
|
|
|
|
|
|
|
|
|
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
|
|
|
|
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// Добавление объекта в набор на конкретную позицию
|
2024-10-03 01:46:41 +04:00
|
|
|
|
public int Insert(T truck, int position, IEqualityComparer<T?>? equal = null)
|
2024-10-03 01:40:53 +04:00
|
|
|
|
{
|
2024-10-03 01:45:35 +04:00
|
|
|
|
if (Count >= _maxCount)
|
2024-10-03 01:40:53 +04:00
|
|
|
|
{
|
2024-10-03 01:45:35 +04:00
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
|
|
|
|
}
|
|
|
|
|
if (position < 0 || position >= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
throw new IndexOutOfRangeException("Индекс вне границ коллекции");
|
2024-10-03 01:40:53 +04:00
|
|
|
|
}
|
2024-10-03 01:46:41 +04:00
|
|
|
|
if (equal != null && _places.Contains(truck, equal))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Данный объект уже есть в коллекции");
|
|
|
|
|
}
|
2024-10-03 01:42:04 +04:00
|
|
|
|
_places.Insert(position, truck);
|
2024-10-03 01:45:35 +04:00
|
|
|
|
return 0;
|
2024-10-03 01:40:53 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// Удаление объекта из набора с конкретной позиции
|
2024-10-03 01:40:53 +04:00
|
|
|
|
public bool Remove(int position)
|
|
|
|
|
{
|
2024-10-03 01:42:04 +04:00
|
|
|
|
if (position < 0 || position >= Count)
|
2024-10-03 01:40:53 +04:00
|
|
|
|
{
|
2024-10-03 01:45:35 +04:00
|
|
|
|
throw new TruckNotFoundException(position);
|
2024-10-03 01:40:53 +04:00
|
|
|
|
}
|
2024-10-03 01:42:04 +04:00
|
|
|
|
_places.RemoveAt(position);
|
2024-10-03 01:40:53 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// Получение объекта из набора по позиции
|
2024-10-03 01:42:04 +04:00
|
|
|
|
public T? this[int position]
|
2024-10-03 01:40:53 +04:00
|
|
|
|
{
|
2024-10-03 01:42:04 +04:00
|
|
|
|
get
|
|
|
|
|
{
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// TODO проверка позиции
|
2024-10-03 01:42:04 +04:00
|
|
|
|
if (position < 0 || position >= Count)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// TODO проверка позиции
|
2024-10-03 01:42:04 +04:00
|
|
|
|
if (position < 0 || position > Count || Count >= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-03 01:44:06 +04:00
|
|
|
|
|
|
|
|
|
// TODO вставка в список по позиции
|
2024-10-03 01:42:04 +04:00
|
|
|
|
_places.Insert(position, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-03 01:40:53 +04:00
|
|
|
|
|
2024-10-03 01:44:06 +04:00
|
|
|
|
// Проход по списку
|
2024-10-03 01:42:04 +04:00
|
|
|
|
public IEnumerable<T?> GetTruck(int? maxTruck = null)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _places.Count; ++i)
|
2024-10-03 01:40:53 +04:00
|
|
|
|
{
|
2024-10-03 01:42:04 +04:00
|
|
|
|
yield return _places[i];
|
|
|
|
|
if (maxTruck.HasValue && i == maxTruck.Value)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
2024-10-03 01:40:53 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|