PIbd-21_Zaharchenko_M.I._Do.../DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs
2022-11-24 21:20:17 +04:00

59 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class SetBusesGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
private int BusyPlaces = 0;
public SetBusesGeneric(int count)
{
_places = new T[count];
}
public int Insert(T bus)
{
return Insert(bus, 0);
}
public int Insert(T bus, int position)
{
if (position < 0 || position >= _places.Length || BusyPlaces == _places.Length) return -1;
BusyPlaces++;
while (_places[position] != null) {
for (int i = _places.Length - 1; i > 0; --i) {
if (_places[i] == null && _places[i - 1] != null) {
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
}
_places[position] = bus;
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= _places.Length) return null;
T savedBus = _places[position];
_places[position] = null;
return savedBus;
}
public T Get(int position)
{
if (position < 0 || position >= _places.Length) return null;
return _places[position];
}
}
}