ISE_22.Aparyan.Bulldozer.Base Lab04 #4
@ -2,62 +2,58 @@
|
||||
{
|
||||
internal class SetGeneric<T> where T : class
|
||||
{
|
||||
private readonly T[] _places;
|
||||
public int Count => _places.Length;
|
||||
|
||||
private readonly List<T?> _places;
|
||||
public int Count => _places.Count;
|
||||
/// Максимальное количество объектов в списке
|
||||
private readonly int _maxCount;
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
_maxCount = count;
|
||||
_places = new List<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;
|
||||
if (position < 0 || position >= _maxCount) return -1;
|
||||
_places.Insert(position, tract);
|
||||
return position;
|
||||
}
|
||||
|
||||
public T? Remove(int position)
|
||||
{
|
||||
if (position >= Count || position < 0)
|
||||
return null;
|
||||
|
||||
T tmp = _places[position];
|
||||
T? tmp = _places[position];
|
||||
_places[position] = null;
|
||||
return tmp;
|
||||
}
|
||||
//Получение объекта из набора по позиции
|
||||
public T? Get(int position)
|
||||
public T? this[int position]
|
||||
{
|
||||
// TODO проверка позиции
|
||||
if (position < 0 || position >= Count) return null;
|
||||
return _places[position];
|
||||
get
|
||||
{
|
||||
if (position < 0 || position >= Count) return null;
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (position < 0 || position >= Count || Count == _maxCount) return;
|
||||
_places.Insert(position, value);
|
||||
}
|
||||
}
|
||||
public IEnumerable<T?> GetTractors(int? maxTracts = null)
|
||||
{
|
||||
for (int i = 0; i < _places.Count; ++i)
|
||||
{
|
||||
yield return _places[i];
|
||||
if (maxTracts.HasValue && i == maxTracts.Value)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user