75 lines
2.4 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 ProjectLocomotive
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetLocomotivesGeneric<T>
where T : class
{
/// Список хранимых объектов
private readonly List<T> _places;
/// Количество объектов в списке
public int Count => _places.Count;
private readonly int _maxCount = 15;
/// Конструктор
public SetLocomotivesGeneric(int count)
{
_maxCount = count = 15;
_places = new List<T>();
}
/// Добавление объекта в набор
public int Insert(T locomotive)
{
return Insert(locomotive, 0);
}
/// Добавление объекта в набор на конкретную позицию
public int Insert(T locomotive, int position)
{
if (position < 0) return -1;
if (Count >= _maxCount) throw new StorageOverflowException(_maxCount);
_places.Insert(position, locomotive);
return position;
}
/// Удаление объекта из набора с конкретной позиции
public T Remove(int position)
{
if (_places[position] is null) throw new LocomotiveNotFoundException(position);
T result = _places[position];
_places[position] = null;
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;
}
}
}
}
}