86 lines
2.1 KiB
C#
86 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ContainerShip.Generic
|
|
{
|
|
internal class SetGeneric<T>
|
|
where T : class
|
|
{
|
|
private readonly T?[] _places;
|
|
public int Count => _places.Length - 1;
|
|
public SetGeneric(int count)
|
|
{
|
|
_places = new T?[count];
|
|
}
|
|
public int Insert(T ship)
|
|
{
|
|
int pos = Count - 1;
|
|
if (_places[Count] != null)
|
|
{
|
|
|
|
for (int i = pos; i >= 0; --i)
|
|
{
|
|
if (_places[i] == null)
|
|
{
|
|
pos = i;
|
|
break;
|
|
}
|
|
}
|
|
for (int i = pos + 1; i <= Count; ++i)
|
|
{
|
|
_places[i - 1] = _places[i];
|
|
}
|
|
}
|
|
_places[Count] = ship;
|
|
return pos;
|
|
}
|
|
|
|
public bool Insert(T ship, int position)
|
|
{
|
|
if (position < 0 || position > Count)
|
|
{
|
|
return false;
|
|
}
|
|
if (_places[Count] != null)
|
|
{
|
|
int pos = Count;
|
|
for (int i = Count; i > 0; --i)
|
|
{
|
|
|
|
if (_places[i] == null)
|
|
{
|
|
pos = i;
|
|
break;
|
|
}
|
|
}
|
|
for (int i = Count; i >= pos; --i)
|
|
{
|
|
_places[i - 1] = _places[i];
|
|
}
|
|
}
|
|
_places[Count] = ship;
|
|
return true;
|
|
}
|
|
public bool Remove(int position)
|
|
{
|
|
if (position < 0 || position > Count)
|
|
{
|
|
return false;
|
|
}
|
|
_places[position] = null;
|
|
return true;
|
|
}
|
|
public T? Get(int position)
|
|
{
|
|
if (position < 0 || position > Count)
|
|
{
|
|
return null;
|
|
}
|
|
return _places[position];
|
|
}
|
|
}
|
|
}
|