2023-12-10 12:27:15 +04:00

95 lines
2.6 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 ProjectAirbus.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirbus.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 airbus)
{
return Insert(airbus, 0);
}
// Добавление объекта в набор на конкретную позицию
public int Insert(T airbus, int position)
{
if (Count >= _maxCount)
{
throw new StorageOverflowException(_maxCount);
}
if (position < 0 || position >= _maxCount)
{
throw new IndexOutOfRangeException("Индекс вне границ коллекции");
}
_places.Insert(position, airbus);
return 0;
}
// Удаление объекта из набора с конкретной позиции
public bool Remove(int position)
{
if (position < 0 || position >= Count)
{
throw new AirbusNotFoundException(position);
}
_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 || Count >= _maxCount)
{
return;
}
_places.Insert(position, value);
}
}
// Проход по списку
public IEnumerable<T?> GetAirbus(int? maxAirbus = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxAirbus.HasValue && i == maxAirbus.Value)
{
yield break;
}
}
}
}
}