2023-11-10 20:16:55 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-12-29 21:05:52 +04:00
|
|
|
|
using System.Windows.Forms.VisualStyles;
|
|
|
|
|
using WarmlyLocomotive.Exceptions;
|
|
|
|
|
using System;
|
2023-11-10 20:16:55 +04:00
|
|
|
|
|
2023-11-11 21:26:13 +04:00
|
|
|
|
namespace WarmlyLocomotive.Generics
|
2023-12-29 21:05:52 +04:00
|
|
|
|
|
2023-11-11 21:26:13 +04:00
|
|
|
|
{
|
|
|
|
|
internal class SetGeneric<T>
|
|
|
|
|
where T : class
|
2023-11-10 20:16:55 +04:00
|
|
|
|
{
|
2023-11-11 21:26:13 +04:00
|
|
|
|
private readonly List<T?> _places;
|
|
|
|
|
public int Count => _places.Count;
|
2023-12-29 21:05:52 +04:00
|
|
|
|
//public int startPointer = 0;
|
|
|
|
|
|
|
|
|
|
public int countMax = 0;
|
2023-11-11 21:26:13 +04:00
|
|
|
|
public SetGeneric(int count)
|
|
|
|
|
{
|
|
|
|
|
_places = new List<T?>(count);
|
2023-12-29 21:05:52 +04:00
|
|
|
|
countMax = count;
|
2023-11-11 21:26:13 +04:00
|
|
|
|
}
|
2023-12-29 21:05:52 +04:00
|
|
|
|
public bool Insert(T ship)
|
2023-11-10 20:16:55 +04:00
|
|
|
|
{
|
2023-12-29 21:05:52 +04:00
|
|
|
|
if (_places.Count == countMax)
|
|
|
|
|
throw new StorageOverflowException(countMax);
|
|
|
|
|
Insert(ship, 0);
|
2023-11-11 21:26:13 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-12-29 21:05:52 +04:00
|
|
|
|
public bool Insert(T ship, int position)
|
2023-11-11 21:26:13 +04:00
|
|
|
|
{
|
2023-12-29 21:05:52 +04:00
|
|
|
|
if (_places.Count == countMax)
|
|
|
|
|
throw new StorageOverflowException(countMax);
|
|
|
|
|
if (!(position >= 0 && position <= Count && _places.Count < countMax))
|
|
|
|
|
return false;
|
|
|
|
|
_places.Insert(position, ship);
|
2023-11-11 21:26:13 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public bool Remove(int position)
|
|
|
|
|
{
|
2023-12-29 21:05:52 +04:00
|
|
|
|
if (!(position >= 0 && position < Count))
|
|
|
|
|
throw new WarmlyLocomotiveNotFoundException(position);
|
2023-11-11 21:26:13 +04:00
|
|
|
|
_places.RemoveAt(position);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public T? this[int position]
|
|
|
|
|
{
|
|
|
|
|
get
|
2023-11-10 20:16:55 +04:00
|
|
|
|
{
|
2023-12-29 21:05:52 +04:00
|
|
|
|
if (!(position >= 0 && position < Count))
|
|
|
|
|
return null;
|
2023-11-11 21:26:13 +04:00
|
|
|
|
return _places[position];
|
2023-11-10 20:16:55 +04:00
|
|
|
|
}
|
2023-11-11 21:26:13 +04:00
|
|
|
|
set
|
2023-11-10 20:16:55 +04:00
|
|
|
|
{
|
2023-12-29 21:05:52 +04:00
|
|
|
|
if (!(position >= 0 && position < Count && _places.Count < countMax))
|
|
|
|
|
return;
|
2023-11-11 21:26:13 +04:00
|
|
|
|
_places.Insert(position, value);
|
|
|
|
|
return;
|
2023-11-10 20:16:55 +04:00
|
|
|
|
}
|
2023-11-11 21:26:13 +04:00
|
|
|
|
}
|
2023-12-29 21:05:52 +04:00
|
|
|
|
public IEnumerable<T?> GetWarmlyLocomotive(int? maxShip = null)
|
2023-11-11 21:26:13 +04:00
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _places.Count; ++i)
|
2023-11-10 20:16:55 +04:00
|
|
|
|
{
|
2023-11-11 21:26:13 +04:00
|
|
|
|
yield return _places[i];
|
2023-12-29 21:05:52 +04:00
|
|
|
|
if (maxShip.HasValue && i == maxShip.Value)
|
2023-11-10 20:16:55 +04:00
|
|
|
|
{
|
2023-11-11 21:26:13 +04:00
|
|
|
|
yield break;
|
2023-11-10 20:16:55 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-29 21:05:52 +04:00
|
|
|
|
}
|