82 lines
1.9 KiB
C#
82 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Warship
|
|
{
|
|
internal class SetWarshipsGeneric<T>
|
|
where T : class
|
|
{
|
|
private readonly List<T> _places;
|
|
|
|
public int Count => _places.Count;
|
|
|
|
private readonly int _maxCount;
|
|
|
|
public SetWarshipsGeneric(int count)
|
|
{
|
|
_maxCount = count;
|
|
_places = new List<T>();
|
|
}
|
|
|
|
public int Insert(T warship)
|
|
{
|
|
if (_places.Count + 1 >= _maxCount)
|
|
return -1;
|
|
_places.Insert(0, warship);
|
|
return 0;
|
|
}
|
|
|
|
public int Insert(T warship, int position)
|
|
{
|
|
if (position >= _maxCount || position < 0)
|
|
return -1;
|
|
if (_places.Count + 1 >= _maxCount)
|
|
return -1;
|
|
_places.Insert(position, warship);
|
|
return position;
|
|
}
|
|
|
|
public T Remove(int position)
|
|
{
|
|
if (position >= _maxCount || position < 0)
|
|
return null;
|
|
|
|
T deleted =_places[position];
|
|
_places.RemoveAt(position);
|
|
return deleted;
|
|
}
|
|
|
|
public T this[int position]
|
|
{
|
|
get
|
|
{
|
|
if (position < 0 || position >= _maxCount)
|
|
return null;
|
|
return _places[position];
|
|
}
|
|
set
|
|
{
|
|
if (position < 0 || position >= _maxCount)
|
|
Insert(value, position);
|
|
}
|
|
}
|
|
public IEnumerable<T> GetWarships()
|
|
{
|
|
foreach(var warship in _places)
|
|
{
|
|
if (warship != null)
|
|
{
|
|
yield return warship;
|
|
}
|
|
else
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|