77 lines
2.2 KiB
C#
Raw Normal View History

2023-11-10 20:16:55 +04:00
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;
2023-11-10 20:16:55 +04:00
namespace WarmlyLocomotive.Generics
{
internal class SetGeneric<T>
where T : class
2023-11-10 20:16:55 +04:00
{
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)
2023-11-10 20:16:55 +04:00
{
if (_places.Count == countMax)
throw new StorageOverflowException(countMax);
Insert(ship, 0);
return true;
}
public bool Insert(T ship, int position)
{
if (_places.Count == countMax)
throw new StorageOverflowException(countMax);
if (!(position >= 0 && position <= Count && _places.Count < countMax))
return false;
_places.Insert(position, ship);
return true;
}
public bool Remove(int position)
{
if (!(position >= 0 && position < Count))
throw new WarmlyLocomotiveNotFoundException(position);
_places.RemoveAt(position);
return true;
}
public T? this[int position]
{
get
2023-11-10 20:16:55 +04:00
{
if (!(position >= 0 && position < Count))
return null;
return _places[position];
2023-11-10 20:16:55 +04:00
}
set
2023-11-10 20:16:55 +04:00
{
if (!(position >= 0 && position < Count && _places.Count < countMax))
return;
_places.Insert(position, value);
return;
2023-11-10 20:16:55 +04:00
}
}
public IEnumerable<T?> GetWarmlyLocomotive(int? maxShip = null)
{
for (int i = 0; i < _places.Count; ++i)
2023-11-10 20:16:55 +04:00
{
yield return _places[i];
if (maxShip.HasValue && i == maxShip.Value)
2023-11-10 20:16:55 +04:00
{
yield break;
2023-11-10 20:16:55 +04:00
}
}
}
}
}