2024-04-14 12:18:57 +04:00
|
|
|
|
|
2024-04-20 10:04:26 +04:00
|
|
|
|
using ProjectAirplaneWithRadar.Exceptions;
|
|
|
|
|
|
2024-04-14 12:18:57 +04:00
|
|
|
|
namespace ProjectAirplaneWithRadar.CollectionGenericObjects
|
2024-03-23 18:09:22 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Параметризованный набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Параметр: ограничение - ссылочный тип</typeparam>
|
|
|
|
|
public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Массив объектов, которые храним
|
|
|
|
|
/// </summary>
|
|
|
|
|
private T?[] _collection;
|
|
|
|
|
|
|
|
|
|
public int Count => _collection.Length;
|
|
|
|
|
|
2024-04-14 12:25:50 +04:00
|
|
|
|
public int MaxCount
|
2024-03-23 18:09:22 +04:00
|
|
|
|
{
|
2024-04-14 12:27:49 +04:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _collection.Length;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 18:09:22 +04:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value > 0)
|
|
|
|
|
{
|
|
|
|
|
if (_collection.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _collection, value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_collection = new T?[value];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-14 12:27:49 +04:00
|
|
|
|
}
|
2024-03-23 18:09:22 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 12:18:57 +04:00
|
|
|
|
public CollectionType GetCollectionType => CollectionType.Massive;
|
|
|
|
|
|
2024-03-23 18:09:22 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MassiveGenericObjects()
|
|
|
|
|
{
|
|
|
|
|
_collection = Array.Empty<T?>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? Get(int position)
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position >= Count)
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new PositionOutOfCollectionException(position);
|
|
|
|
|
|
|
|
|
|
if (position >= _collection.Length && _collection[position] == null)
|
|
|
|
|
throw new ObjectNotFoundException(position);
|
|
|
|
|
|
2024-03-23 18:09:22 +04:00
|
|
|
|
return _collection[position];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T obj)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[i] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[i] = obj;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new CollectionOverflowException(Count);
|
2024-03-23 18:09:22 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T obj, int position)
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position >= Count)
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new PositionOutOfCollectionException(position);
|
2024-03-23 18:09:22 +04:00
|
|
|
|
|
|
|
|
|
if (_collection[position] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[position] = obj;
|
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int temp = position + 1;
|
|
|
|
|
while (temp < Count)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[temp] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[temp] = obj;
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
temp++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
temp = position - 1;
|
|
|
|
|
while (temp > 0)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[temp] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[temp] = obj;
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
temp--;
|
|
|
|
|
}
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new CollectionOverflowException(Count);
|
2024-03-23 18:09:22 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? Remove(int position)
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position >= Count)
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new PositionOutOfCollectionException(position);
|
2024-03-23 18:09:22 +04:00
|
|
|
|
|
|
|
|
|
if (_collection[position] == null)
|
2024-04-20 10:04:26 +04:00
|
|
|
|
throw new ObjectNotFoundException(position);
|
2024-03-23 18:09:22 +04:00
|
|
|
|
|
|
|
|
|
T? temp = _collection[position];
|
|
|
|
|
_collection[position] = null;
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
2024-04-14 12:18:57 +04:00
|
|
|
|
|
|
|
|
|
public IEnumerable<T?> GetItems()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _collection.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _collection[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-23 18:09:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|