Compare commits
No commits in common. "6699ba3e92f087d5611b50def6db0e51c0025a45" and "6adda3e8ecc80730eead40888eb3c78332468c52" have entirely different histories.
6699ba3e92
...
6adda3e8ec
@ -16,7 +16,8 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
/// <summary>
|
||||
/// Список объектов, которые храним
|
||||
/// </summary>
|
||||
private readonly List<T?> _collection;
|
||||
//private readonly List<T?> _collection;
|
||||
private readonly Dictionary<int, T?> _collection;
|
||||
|
||||
/// <summary>
|
||||
/// Максимально допустимое число объектов в списке
|
||||
@ -32,7 +33,8 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
/// </summary>
|
||||
public ListGenericObjects()
|
||||
{
|
||||
_collection = new();
|
||||
//_collection = new();
|
||||
_collection = new Dictionary<int, T?>();
|
||||
}
|
||||
|
||||
public T? Get(int position)
|
||||
@ -47,32 +49,78 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T>
|
||||
}
|
||||
|
||||
}
|
||||
public int Insert(T obj)
|
||||
public bool Insert(T obj)
|
||||
{
|
||||
if (Count == _maxCount) { return -1; }
|
||||
_collection.Add(obj);
|
||||
return Count;
|
||||
if (Count == _maxCount) { return false; }
|
||||
//_collection.Add(obj);
|
||||
//return true;
|
||||
|
||||
//допка
|
||||
int position = FindFirstNullPosition();
|
||||
if (position == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public int Insert(T obj, int position)
|
||||
_collection[position] = obj;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Insert(T obj, int position)
|
||||
{
|
||||
if (position < 0 || position >= Count || Count == _maxCount)
|
||||
//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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_collection[position] = obj;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool 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;
|
||||
}
|
||||
_collection.Insert(position, obj);
|
||||
|
||||
return position;
|
||||
|
||||
}
|
||||
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position >= Count || position < 0) return null;
|
||||
T obj = _collection[position];
|
||||
_collection.RemoveAt(position);
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user