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