PIbd-23_Lisov_N.A._AirFight.../AirFighter/SetAircraftsGeneric.cs
2022-10-03 16:31:36 +04:00

88 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class SetAircraftsGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
public SetAircraftsGeneric(int count)
{
_places = new T[count];
}
public int Insert(T aircraft)
{
return Insert(aircraft, 0);
}
public int Insert(T aircraft, int position)
{
int emptypos = -1;
if (position >= Count && position < 0)
{
return -1;
}
if (_places[position] == null)
{
_places[position] = aircraft;
return position;
}
for (int i = position; i < Count; i++)
{
if (_places[i] == null)
{
emptypos = i;
break;
}
}
if (emptypos != -1)
{
for (int i = emptypos; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = aircraft;
return position;
}
return -1;
}
public T Remove(int position)
{
if (position < Count && position >= 0)
{
T result = _places[position];
_places[position] = null;
return result;
}
return null;
}
public T Get(int position)
{
if (position >= Count && position < 0)
{
return null;
}
return _places[position];
}
}
}