107 lines
2.2 KiB
C#
107 lines
2.2 KiB
C#
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];
|
|
}
|
|
}
|
|
}
|