2023-12-29 22:03:17 +04:00

82 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;
using WarmlyLocomotive.Exceptions;
using System;
using System.Numerics;
namespace WarmlyLocomotive.Generics
{
internal class SetGeneric<T>
where T : class
{
private readonly List<T?> _places;
public int Count => _places.Count;
//public int startPointer = 0;
public int countMax = 0;
public SetGeneric(int count)
{
_places = new List<T?>(count);
countMax = count;
}
public bool Insert(T ship, IEqualityComparer<T>? equal = null)
{
if (_places.Count == countMax)
throw new StorageOverflowException(countMax);
Insert(ship, 0, equal);
return true;
}
public void Insert(T ship, int position, IEqualityComparer<T>? equal = null)
{
if (_places.Count == countMax)
throw new StorageOverflowException(countMax);
if (!(position >= 0 && position <= Count))
throw new Exception($"Неверная позиция для вставки");
if (equal != null)
{
if (_places.Contains(ship, equal))
throw new ArgumentException(nameof(ship));
}
_places.Insert(position, ship);
}
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public void Remove(int position)
{
if (!(position >= 0 && position < Count))
throw new WarmlyLocomotiveNotFoundException(position);
_places.RemoveAt(position);
}
public T? this[int position]
{
get
{
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < countMax))
return;
_places.Insert(position, value);
return;
}
}
public IEnumerable<T?> GetWarmlyLocomotive(int? maxShip = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxShip.HasValue && i == maxShip.Value)
{
yield break;
}
}
}
}
}