76 lines
1.8 KiB
C#
76 lines
1.8 KiB
C#
|
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];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|