87 lines
1.8 KiB
C#
87 lines
1.8 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 bool Insert(T aircraft)
|
|
{
|
|
return Insert(aircraft, 0);
|
|
}
|
|
|
|
public bool Insert(T aircraft, int position)
|
|
{
|
|
int emptypos = -1;
|
|
|
|
|
|
if (position >= Count && position < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (_places[position] == null)
|
|
{
|
|
_places[position] = aircraft;
|
|
return true;
|
|
}
|
|
|
|
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 true;
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
public bool Remove(int position)
|
|
{
|
|
if (position < Count && position >= 0 && _places[position] != null)
|
|
{
|
|
_places[position] = null;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public T Get(int position)
|
|
{
|
|
if (position >= Count && position < 0)
|
|
{
|
|
return null;
|
|
}
|
|
return _places[position];
|
|
}
|
|
}
|
|
}
|