PIbd-22_Shabunov_O.A._AirBo.../AirBomber/Generics/SetGeneric.cs

94 lines
2.3 KiB
C#
Raw Normal View History

2023-11-25 23:25:32 +04:00
using AirBomber.Exceptions;
namespace AirBomber.Generics
2023-11-19 17:11:10 +04:00
{
internal class SetGeneric<T>
where T : class
2023-11-19 17:11:10 +04:00
{
private readonly List<T?> _objects;
private readonly int _maxCount;
2023-11-19 17:11:10 +04:00
public int Count => _objects.Count;
2023-11-19 17:11:10 +04:00
public SetGeneric(int Count)
{
_maxCount = Count;
_objects = new List<T?>(_maxCount);
for (int i = 0; i < _maxCount; i++)
_objects.Add(null);
2023-11-19 17:11:10 +04:00
}
public int Insert(T Entity)
{
/** Вставка в начало набора */
for (int i = 0; i < _maxCount; i++)
2023-11-19 17:11:10 +04:00
if (_objects[i] is null)
{
_objects[i] = Entity;
return i;
}
2023-11-25 23:25:32 +04:00
throw new StorageOverflowException();
2023-11-19 17:11:10 +04:00
}
public int Insert(T Entity, int Position)
{
if (Position >= _maxCount)
2023-11-25 23:25:32 +04:00
throw new StorageOverflowException();
2023-11-19 17:11:10 +04:00
if (_objects[Position] is null)
{
_objects[Position] = Entity;
return Position;
}
2023-11-25 23:25:32 +04:00
throw new StorageOverflowException();
2023-11-19 17:11:10 +04:00
}
public bool Remove(int Position)
{
if (Position >= _maxCount)
2023-11-25 23:25:32 +04:00
throw new EntityNotFoundException();
if (_objects[Position] is null)
throw new EntityNotFoundException();
2023-11-19 17:11:10 +04:00
_objects[Position] = default(T);
return true;
}
public T? this[int Position]
{
get
{
if (Position >= _maxCount)
return null;
return _objects[Position];
}
set
{
if (Position >= _maxCount)
return;
if (_objects[Position] is not null)
return;
_objects[Position] = value;
}
}
public IEnumerable<T?> GetEntities(int? MaxEntities = null)
2023-11-19 17:11:10 +04:00
{
for (int i = 0; i < _objects.Count; ++i)
{
yield return _objects[i];
2023-11-19 17:11:10 +04:00
if (MaxEntities.HasValue && i == MaxEntities.Value)
yield break;
}
2023-11-19 17:11:10 +04:00
}
}
}