87 lines
1.8 KiB
C#
Raw Normal View History

2022-10-02 21:50:02 +04:00
using System;
using System.Collections.Generic;
2022-10-03 03:04:38 +04:00
using System.Configuration;
2022-10-02 21:50:02 +04:00
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)
{
2022-10-03 03:04:38 +04:00
return Insert(aircraft, 0);
2022-10-02 21:50:02 +04:00
}
public bool Insert(T aircraft, int position)
{
2022-10-03 03:04:38 +04:00
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;
2022-10-02 21:50:02 +04:00
}
public bool Remove(int position)
{
2022-10-03 03:04:38 +04:00
if (position < Count && position >= 0 && _places[position] != null)
{
_places[position] = null;
return true;
}
return false;
2022-10-02 21:50:02 +04:00
}
public T Get(int position)
{
2022-10-03 03:04:38 +04:00
if (position >= Count && position < 0)
{
return null;
}
2022-10-02 21:50:02 +04:00
return _places[position];
}
}
}