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>
|
2023-11-19 17:56:27 +04:00
|
|
|
|
where T : class
|
2023-11-19 17:11:10 +04:00
|
|
|
|
{
|
2023-11-19 17:56:27 +04:00
|
|
|
|
private readonly List<T?> _objects;
|
|
|
|
|
private readonly int _maxCount;
|
2023-11-19 17:11:10 +04:00
|
|
|
|
|
2023-11-19 17:56:27 +04:00
|
|
|
|
public int Count => _objects.Count;
|
2023-11-19 17:11:10 +04:00
|
|
|
|
|
|
|
|
|
public SetGeneric(int Count)
|
|
|
|
|
{
|
2023-11-19 17:56:27 +04:00
|
|
|
|
_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)
|
|
|
|
|
{
|
2023-11-19 17:56:27 +04:00
|
|
|
|
/** Вставка в начало набора */
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-11-19 17:56:27 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-11-19 17:56:27 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-19 17:56:27 +04:00
|
|
|
|
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
|
|
|
|
{
|
2023-11-19 17:56:27 +04:00
|
|
|
|
for (int i = 0; i < _objects.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _objects[i];
|
2023-11-19 17:11:10 +04:00
|
|
|
|
|
2023-11-19 17:56:27 +04:00
|
|
|
|
if (MaxEntities.HasValue && i == MaxEntities.Value)
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
2023-11-19 17:11:10 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|