PIbd-23_Mochalov_D.V._Locom.../Locomotive/Locomotive/SetLocomotivesGeneric.cs

77 lines
2.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomotive
{
internal class SetLocomotivesGeneric <T>
where T : class
{
/// Список хранимых объектов
private readonly List<T> _places;
/// Количество объектов в массиве
public int Count => _places.Count;
// Ограничение на количество
private readonly int _maxCount;
/// Конструктор
public SetLocomotivesGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
}
/// Добавление объекта в набор
public int Insert(T locomotive)
{
return Insert(locomotive, 0);
}
/// Добавление объекта в набор на конкретную позицию
public int Insert(T locomotive, int position)
{
if (position >= _maxCount|| position < 0) return -1;
_places.Insert(position, locomotive);
return position;
}
/// Удаление объекта из набора с конкретной позиции
public T Remove(int position)
{
if (position >= _maxCount || position < 0) return null;
T result = _places[position];
_places.RemoveAt(position);
return result;
}
// Индексатор
public T this[int position]
{
get
{
if (position >= _maxCount || position < 0) return null;
return _places[position];
}
set
{
if (position >= _maxCount || position < 0) return;
Insert(value, position);
}
}
/// Проход по набору до первого пустого
public IEnumerable<T> GetLocomotives()
{
foreach (var locomotive in _places)
{
if (locomotive != null)
{
yield return locomotive;
}
else
{
yield break;
}
}
}
}
}