PIbd-22_Fedorenko_G.Y._Hydr.../Hydroplane/SetGeneric.cs

87 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hydroplane.Generics
{
internal class SetGeneric<T> where T : class
{
private readonly T?[] _places;
public int Count => _places.Length;
public SetGeneric(int count)
{
_places = new T?[count];
}
public int Insert(T plane)
{
int index = -1;
for (int i = 0; i < _places.Length; i++)
{
if (_places[i] == null)
{
index = i; break;
}
}
if (index < 0)
{
return -1;
}
for (int i = index; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = plane;
return 0;
}
public int Insert(T plane, int position)
{
if (position < 0 || position >= Count)
return -1;
if (_places[position] == null)
{
_places[position] = plane;
return position;
}
int index = -1;
for (int i = position; i < Count; i++)
{
if (_places[i] == null)
{
index = i; break;
}
}
if (index < 0)
return -1;
for (int i = index; index > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = plane;
return position;
}
public bool Remove(int position)
{
if (position < 0 || position >= _places.Count())
return false;
_places[position] = null;
return true;
}
public T? Get(int position)
{
if (position < 0 || position >= _places.Count())
return null;
return _places[position];
}
}
}