77 lines
2.0 KiB
C#
77 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ProjectElectricLocomotive.Generics
|
|
{
|
|
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 loco)
|
|
{
|
|
return Insert(loco, 0);
|
|
}
|
|
|
|
public int Insert(T loco, int position)
|
|
{
|
|
if (position < 0 || position >= _maxCount) return -1;
|
|
_places.Insert(position, loco);
|
|
return position;
|
|
}
|
|
|
|
public T? Remove(int position)
|
|
{
|
|
if (position >= Count || position < 0)
|
|
return null;
|
|
|
|
T? tmp = _places[position];
|
|
_places[position] = null;
|
|
return tmp;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Проход по списку
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IEnumerable<T?> GetLocomotives(int? maxLocos = null)
|
|
{
|
|
for (int i = 0; i < _places.Count; ++i)
|
|
{
|
|
yield return _places[i];
|
|
if (maxLocos.HasValue && i == maxLocos.Value)
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|