2023-11-19 17:11:10 +04:00

76 lines
1.8 KiB
C#
Raw 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.

namespace AirBomber.Generics
{
internal class SetGeneric<T>
{
private readonly T?[] _objects;
public int Count => _objects.Length;
public SetGeneric(int Count)
{
_objects = new T?[Count];
}
public int Insert(T Entity)
{
for (int i = 0; i < Count; i++)
if (_objects[i] is null)
{
_objects[i] = Entity;
return i;
}
return -1;
}
public int Insert(T Entity, int Position)
{
if (Position >= Count)
return -1;
if (_objects[Position] is null)
{
_objects[Position] = Entity;
return Position;
}
/** Сдвиг элементов вправо начиная с Position до ближайшего пустого места */
int EmptyPos = -1;
for (int i = Position + 1; i < Count; i++)
if (_objects[i] is null)
{
EmptyPos = i;
break;
}
if (EmptyPos == -1)
return -1;
/** Сдвиг */
for (int i = EmptyPos; i > Position; i--)
_objects[i] = _objects[i - 1];
_objects[Position] = Entity;
return Position;
}
public bool Remove(int Position)
{
if (Position >= Count)
return false;
_objects[Position] = default(T);
return true;
}
public T? Get(int Position)
{
if (Position >= Count)
return default(T);
return _objects[Position];
}
}
}