76 lines
2.1 KiB
C#
76 lines
2.1 KiB
C#
using ElectricLocomotive.Exceptions;
|
|
|
|
namespace ElectricLocomotive;
|
|
|
|
public 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);
|
|
}
|
|
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
|
public bool Insert(T electricLocomotiv, IEqualityComparer<T>? equal = null)
|
|
{
|
|
if (_places.Count == _maxCount)
|
|
throw new StorageOverflowException(_maxCount);
|
|
Insert(electricLocomotiv, 0, equal);
|
|
return true;
|
|
}
|
|
|
|
public bool Insert(T electricLocomotiv, int position, IEqualityComparer<T>? equal = null)
|
|
{
|
|
if (_places.Count == _maxCount)
|
|
throw new StorageOverflowException(_maxCount);
|
|
|
|
if (!(position >= 0 && position <= Count))
|
|
return false;
|
|
if(equal != null)
|
|
{
|
|
if (_places.Contains(electricLocomotiv, equal))
|
|
throw new ArgumentException(nameof(electricLocomotiv));
|
|
}
|
|
_places.Insert(position, electricLocomotiv);
|
|
return true;
|
|
}
|
|
|
|
public bool Remove(int position)
|
|
{
|
|
if (!(position >= 0 && position < Count))
|
|
throw new LocoNotFoundException(position);
|
|
_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 && _places.Count < _maxCount))
|
|
return;
|
|
_places.Insert(position, value);
|
|
return;
|
|
}
|
|
}
|
|
public IEnumerable<T?> GetElectricLocos(int? maxElectricLocos = null)
|
|
{
|
|
for (int i = 0; i < _places.Count; ++i)
|
|
{
|
|
yield return _places[i];
|
|
if (maxElectricLocos.HasValue && i == maxElectricLocos.Value)
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
|
|
} |