PIbd-22-Romanov-E.V.-Hoisti.../HoistingCrane/HoistingCrane/SetHoistingCraneGeneric.cs
2022-12-25 11:14:54 +04:00

84 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HoistingCrane
{
internal class SetHoistingCraneGeneric<T>
where T : class
{
public readonly T[] _places;
public int Count => _places.Length;
public SetHoistingCraneGeneric(int count)
{
_places = new T[count];
}
public int Insert(T hoistingCrane)
{
return Insert(hoistingCrane, 0);
}
public int Insert(T hoistingCrane, int position)
{
int emptypos = -1;
if (position >= Count && position < 0)
{
return -1;
}
if (_places[position] == null)
{
_places[position] = hoistingCrane;
return 1;
}
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] = hoistingCrane;
return 1;
}
return -1;
}
public T Remove(int position)
{
if (position < Count && position >= 0 && _places[position] != null)
{
_places[position] = null;
T removed = _places[position];
return removed;
}
return null;
}
public T Get(int position)
{
if (position >= Count && position < 0)
{
return null;
}
return _places[position];
}
}
}