2023-12-29 22:22:51 +04:00
|
|
|
|
using DoubleDeckerbus.Exceptions;
|
|
|
|
|
using System;
|
2023-11-25 14:55:27 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2023-12-29 23:19:11 +04:00
|
|
|
|
|
2023-11-25 14:55:27 +04:00
|
|
|
|
namespace DoubleDeckerbus.Generic
|
|
|
|
|
{
|
|
|
|
|
internal class SetGeneric<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
private readonly List<T?> _places;
|
|
|
|
|
public int Count => _places.Count;
|
|
|
|
|
private readonly int _maxCount;
|
|
|
|
|
public SetGeneric(int count)
|
|
|
|
|
{
|
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T?>(count);
|
|
|
|
|
}
|
2023-12-29 23:19:11 +04:00
|
|
|
|
public int Insert(T bus, IEqualityComparer<T?>? equal = null)
|
2023-11-25 14:55:27 +04:00
|
|
|
|
{
|
2023-12-29 23:19:11 +04:00
|
|
|
|
return Insert(bus, 0, equal);
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
2023-12-29 23:19:11 +04:00
|
|
|
|
|
|
|
|
|
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
|
|
|
|
public int Insert(T bus, int position, IEqualityComparer<T?>? equal = null)
|
2023-11-25 14:55:27 +04:00
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
if (Count >= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
|
|
|
|
}
|
|
|
|
|
if (position < 0 || position >= _maxCount)
|
2023-11-25 14:55:27 +04:00
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
throw new IndexOutOfRangeException("Индекс вне границ коллекции");
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
2023-12-29 23:19:11 +04:00
|
|
|
|
if (equal != null && _places.Contains(bus, equal))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Данный объект уже есть в коллекции");
|
|
|
|
|
}
|
2023-11-25 14:55:27 +04:00
|
|
|
|
_places.Insert(position, bus);
|
2023-12-29 22:22:51 +04:00
|
|
|
|
return 0;
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
|
|
|
|
public bool Remove(int position)
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position >= Count)
|
|
|
|
|
{
|
2023-12-29 22:22:51 +04:00
|
|
|
|
throw new BusNotFoundException(position);
|
2023-11-25 14:55:27 +04:00
|
|
|
|
}
|
|
|
|
|
_places.RemoveAt(position);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public T? this[int position]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position >= Count)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position > Count || Count >= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_places.Insert(position, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public IEnumerable<T?> GetBus(int? maxBus = null)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _places.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _places[i];
|
|
|
|
|
if (maxBus.HasValue && i == maxBus.Value)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-29 22:22:51 +04:00
|
|
|
|
}
|