PIbd-23-Kondratev-N.D.-Gaso.../GasolineTanker/ProjectGasolineTanker/Generic/SetGeneric.cs

107 lines
2.2 KiB
C#
Raw Normal View History

2024-10-03 01:40:53 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectGasolineTanker.Generic
{
internal class SetGeneric<T>
where T : class
{
private readonly T?[] _places;
public int Count => _places.Length - 1;
public SetGeneric(int count)
{
_places = new T?[count];
}
public int Insert(T truck)
{
int pos = Count - 1;
if (_places[Count] != null)
{
for (int i = pos; i > 0; --i)
{
if (_places[i] == null)
{
pos = i;
break;
}
}
for (int i = pos + 1; i <= Count; ++i)
{
_places[i - 1] = _places[i];
}
}
_places[Count] = truck;
return pos;
}
public bool Insert(T truck, int position)
{
// TODO проверка позиции
if (position < 0 || position > Count)
{
return false;
}
if (_places[Count] != null)
{
int pos = Count;
for (int i = Count; i > 0; --i)
{
if (_places[i] == null)
{
pos = i;
break;
}
}
for (int i = Count; i >= pos; --i)
{
_places[i - 1] = _places[i];
}
}
_places[Count] = truck;
return true;
}
public bool Remove(int position)
{
if (position < 0 || position > Count)
{
return false;
}
_places[position] = null;
return true;
}
public T? Get(int position)
{
if (position < 0 || position > Count)
{
return null;
}
return _places[position];
}
}
}