PIbd-23-Kondratev-N.D.-Gaso.../GasolineTanker/ProjectGasolineTanker/Generic/SetGeneric.cs
2024-10-03 00:42:04 +03:00

85 lines
2.0 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 List<T?> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetGeneric(int count)
{
_maxCount = count;
_places = new List<T?>(count);
}
public int Insert(T truck)
{
_places.Insert(0, truck);
return 0;
}
public bool Insert(T truck, int position)
{
if (position < 0 || position >= Count || Count >= _maxCount)
{
return false;
}
_places.Insert(position, truck);
return true;
}
public bool Remove(int position)
{
if (position < 0 || position >= Count)
{
return false;
}
_places.RemoveAt(position);
return true;
}
public T? this[int position]
{
get
{
if (position < 0 || position >= Count)
{
return null;
}
return _places[position];
}
set
{
if (position < 0 || position > Count || Count >= _maxCount)
{
return;
}
_places.Insert(position, value);
}
}
public IEnumerable<T?> GetTruck(int? maxTruck = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTruck.HasValue && i == maxTruck.Value)
{
yield break;
}
}
}
}
}