73 lines
2.3 KiB
C#
73 lines
2.3 KiB
C#
|
|
|||
|
|
|||
|
namespace ProjectBulldozer.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 tract)
|
|||
|
{
|
|||
|
return Insert(tract, 0);
|
|||
|
}
|
|||
|
|
|||
|
public int Insert(T tract, int position)
|
|||
|
{
|
|||
|
int NoEmpty = 0, temp = 0;
|
|||
|
for (int i = position; i < Count; i++)
|
|||
|
{
|
|||
|
if (_places[i] != null) NoEmpty++;
|
|||
|
}
|
|||
|
if (NoEmpty == Count - position - 1) return -1;
|
|||
|
|
|||
|
if (position < Count && position >= 0)
|
|||
|
{
|
|||
|
for (int j = position; j < Count; j++)
|
|||
|
{
|
|||
|
if (_places[j] == null)
|
|||
|
{
|
|||
|
temp = j;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
// shift right
|
|||
|
for (int i = temp; i > position; i--)
|
|||
|
{
|
|||
|
_places[i] = _places[i - 1];
|
|||
|
}
|
|||
|
_places[position] = tract;
|
|||
|
return position;
|
|||
|
}
|
|||
|
return -1;
|
|||
|
// TODO проверка позиции
|
|||
|
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
|
|||
|
// проверка, что после вставляемого элемента в массиве есть пустой элемент //
|
|||
|
}
|
|||
|
|
|||
|
public T? Remove(int position)
|
|||
|
{
|
|||
|
// TODO проверка позиции
|
|||
|
// TODO удаление объекта из массива, присвоив элементу массива значение null
|
|||
|
if (position >= Count || position < 0)
|
|||
|
return null;
|
|||
|
|
|||
|
T tmp = _places[position];
|
|||
|
_places[position] = null;
|
|||
|
return tmp;
|
|||
|
}
|
|||
|
//Получение объекта из набора по позиции
|
|||
|
public T? Get(int position)
|
|||
|
{
|
|||
|
// TODO проверка позиции
|
|||
|
if (position < 0 || position >= Count) return null;
|
|||
|
return _places[position];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|