PIbd_23_Kislitsa_E.D_AirFig.../AirFighter/SetGeneric.cs

59 lines
1.5 KiB
C#
Raw Permalink Normal View History

2023-10-15 17:11:27 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirFighter.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 airplane)
{
return Insert(airplane, 0);
}
public int Insert(T airplane, int position)
{
if (!(position >= 0 && position < Count))
return -1;
if (_places[position] != null)
{
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return -1;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = airplane;
return position;
}
public bool Remove(int position)
{
if (!(position >= 0 && position < Count) || _places[position] == null)
return false;
_places[position] = null;
return true;
}
public T? Get(int position)
{
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
}
}