62 lines
1.6 KiB
C#
62 lines
1.6 KiB
C#
namespace ProjectLainer.Generics
|
|
{
|
|
internal class SetGeneric<T>
|
|
where T : class
|
|
{
|
|
private readonly T?[] _places;
|
|
public int Count => _places.Length;
|
|
public SetGeneric(int count)
|
|
{
|
|
_places = new T?[count];
|
|
}
|
|
|
|
public int Insert(T lainer)
|
|
{
|
|
if (_places[Count - 1] != null)
|
|
return Count;
|
|
return Insert(lainer, 0);
|
|
}
|
|
public int Insert(T lainer, int position)
|
|
{
|
|
if (position < 0 || position >= Count)
|
|
{
|
|
return position;
|
|
}
|
|
if (_places[position] != null)
|
|
{
|
|
int ind = position;
|
|
while (ind < Count && _places[ind] != null)
|
|
ind++;
|
|
if (ind == Count)
|
|
return Count;
|
|
for (int i = ind - 1; i >= position; i--)
|
|
_places[i + 1] = _places[i];
|
|
}
|
|
_places[position] = lainer;
|
|
return position;
|
|
}
|
|
public bool Remove(int position)
|
|
{
|
|
if (position < 0 )
|
|
{
|
|
return false;
|
|
}
|
|
if(position >= Count)
|
|
{
|
|
return false;
|
|
}
|
|
_places[position] = null;
|
|
return true;
|
|
}
|
|
public T? Get(int position)
|
|
{
|
|
if(position < Count && position >= 0)
|
|
{
|
|
return _places[position];
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|