70 lines
1.6 KiB
C#
Raw Permalink Normal View History

2023-04-14 14:36:46 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat
{
class SetBoatsGeneric<T> where T : class
{
2023-04-14 21:28:29 +04:00
private readonly T[] _places;
2023-04-14 14:36:46 +04:00
2023-04-14 21:28:29 +04:00
public int Count => _places.Length;
2023-04-14 14:36:46 +04:00
private int BusyPlaces = 0;
public SetBoatsGeneric(int count)
{
2023-04-14 21:28:29 +04:00
_places = new T[count]; ;
2023-04-14 14:36:46 +04:00
}
public int Insert(T boat)
{
2023-04-14 21:28:29 +04:00
return Insert(boat, 0);
2023-04-14 14:36:46 +04:00
}
public int Insert(T boat, int position)
{
2023-04-14 21:28:29 +04:00
if (position < 0 || position >= _places.Length)
2023-04-14 14:36:46 +04:00
{
return -1;
}
2023-04-14 21:28:29 +04:00
BusyPlaces++;
while (_places[position] != null)
2023-04-14 14:36:46 +04:00
{
2023-04-14 21:28:29 +04:00
for (int i = 0; i < BusyPlaces; i++)
2023-04-14 14:36:46 +04:00
{
2023-04-14 21:28:29 +04:00
if (_places[i] == null && _places[i - 1] != null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
2023-04-14 14:36:46 +04:00
}
}
2023-04-14 21:28:29 +04:00
_places[position] = boat;
return position;
2023-04-14 14:36:46 +04:00
}
2023-04-14 21:28:29 +04:00
public T Remove(int position)
2023-04-14 14:36:46 +04:00
{
2023-04-14 21:28:29 +04:00
if (position < 0 || position >= _places.Length)
2023-04-14 14:36:46 +04:00
{
return null;
}
2023-04-14 21:28:29 +04:00
BusyPlaces--;
T boat = _places[position];
_places[position] = null;
return boat;
2023-04-14 14:36:46 +04:00
}
2023-04-14 21:28:29 +04:00
public T Get(int position)
2023-04-14 14:36:46 +04:00
{
2023-04-14 21:28:29 +04:00
if (position < 0 || position >= _places.Length)
2023-04-14 14:36:46 +04:00
{
2023-04-14 21:28:29 +04:00
return null;
2023-04-14 14:36:46 +04:00
}
2023-04-14 21:28:29 +04:00
return _places[position];
2023-04-14 14:36:46 +04:00
}
}
}