86 lines
1.5 KiB
C#
86 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace WarmlyShip
|
|
{
|
|
internal class SetShipGeneric<T>
|
|
where T : class, IEquatable<T>
|
|
{
|
|
private readonly List<T> _places;
|
|
public int Count => _places.Count;
|
|
|
|
private readonly int _maxCount;
|
|
|
|
public SetShipGeneric(int count)
|
|
{
|
|
_maxCount = count;
|
|
_places = new List<T>();
|
|
}
|
|
|
|
public int Insert(T ship)
|
|
{
|
|
return Insert(ship, 0);
|
|
}
|
|
|
|
public int Insert(T ship, int position)
|
|
{
|
|
if (_places.Contains(ship))
|
|
{
|
|
return -1;
|
|
}
|
|
if (position < 0 || position > Count || Count == _maxCount) throw new StorageOverflowException(_maxCount);
|
|
_places.Insert(position, ship);
|
|
return position;
|
|
}
|
|
|
|
public T Remove(int position)
|
|
{
|
|
if (position >= _maxCount || position >= _places.Count) throw new ShipNotFoundException(position);
|
|
T DelElement = _places[position];
|
|
_places.Remove(DelElement);
|
|
return DelElement;
|
|
}
|
|
|
|
public T this[int position]
|
|
{
|
|
get
|
|
{
|
|
if (position < 0 || position > Count) return null;
|
|
return _places[position];
|
|
}
|
|
set
|
|
{
|
|
Insert(value, position);
|
|
}
|
|
}
|
|
|
|
|
|
public IEnumerable<T> GetShips()
|
|
{
|
|
foreach (var ship in _places)
|
|
{
|
|
if (ship != null)
|
|
{
|
|
yield return ship;
|
|
}
|
|
else
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SortSet(IComparer<T> comparer)
|
|
{
|
|
if (comparer == null)
|
|
{
|
|
return;
|
|
}
|
|
_places.Sort(comparer);
|
|
}
|
|
}
|
|
}
|