PIBD-12_Morozov_D.V. LabWork№4 #13
@ -16,8 +16,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
/// <summary>
|
||||
/// Список объектов, которые храним
|
||||
/// </summary>
|
||||
//private readonly List<T?> _collection;
|
||||
private readonly Dictionary<int, T?> _collection;
|
||||
private readonly List<T?> _collection;
|
||||
|
||||
/// <summary>
|
||||
/// Максимально допустимое число объектов в списке
|
||||
@ -33,8 +32,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
/// </summary>
|
||||
public ListGenericObjects()
|
||||
{
|
||||
//_collection = new();
|
||||
_collection = new Dictionary<int, T?>();
|
||||
_collection = new();
|
||||
}
|
||||
|
||||
public T? Get(int position)
|
||||
@ -49,78 +47,32 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
}
|
||||
|
||||
}
|
||||
public bool Insert(T obj)
|
||||
public int Insert(T obj)
|
||||
{
|
||||
if (Count == _maxCount) { return false; }
|
||||
//_collection.Add(obj);
|
||||
//return true;
|
||||
|
||||
//допка
|
||||
int position = FindFirstNullPosition();
|
||||
if (position == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (Count == _maxCount) { return -1; }
|
||||
_collection.Add(obj);
|
||||
return Count;
|
||||
|
||||
_collection[position] = obj;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Insert(T obj, int position)
|
||||
public int Insert(T obj, int position)
|
||||
{
|
||||
//if (position < 0 || position >= Count || Count == _maxCount)
|
||||
//{
|
||||
// return false;
|
||||
//}
|
||||
//_collection.Insert(position, obj);
|
||||
|
||||
//return false;
|
||||
|
||||
//допка
|
||||
if (position < 0 || position >= _maxCount || Count == _maxCount || _collection.ContainsKey(position))
|
||||
if (position < 0 || position >= Count || Count == _maxCount)
|
||||
{
|
||||
return false;
|
||||
return -1;
|
||||
}
|
||||
_collection.Insert(position, obj);
|
||||
|
||||
return position;
|
||||
|
||||
_collection[position] = obj;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(int position)
|
||||
public T Remove(int position)
|
||||
{
|
||||
// if (position < 0 || position >= Count)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
// _collection.RemoveAt(position);
|
||||
// return true;
|
||||
|
||||
//допка
|
||||
if (!_collection.ContainsKey(position))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_collection.Remove(position);
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Находит первую пустую позицию в словаре
|
||||
/// </summary>
|
||||
/// <returns>Индекс первой пустой позиции или -1, если такой не найдено</returns>
|
||||
|
||||
|
||||
//допка
|
||||
private int FindFirstNullPosition()
|
||||
{
|
||||
for (int i = 0; i < _maxCount; i++)
|
||||
{
|
||||
if (!_collection.ContainsKey(i) || _collection[i] == null)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
if (position >= Count || position < 0) return null;
|
||||
T obj = _collection[position];
|
||||
_collection.RemoveAt(position);
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user