158 lines
4.0 KiB
C#
Raw Normal View History

using AntiAircraftGun.CollectionGenericObjects;
using AntiAircraftGun.Drawnings;
using AntiAircraftGun.Exceptions;
2024-03-17 12:28:35 +04:00
namespace AntiAircraftGun.CollectionGenereticObject;
/// <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;
public int MaxCount
2024-03-17 12:28:35 +04:00
{
get
{
return _collection.Length;
}
2024-03-17 12:28:35 +04:00
set
{
if (value > 0)
{
if (_collection.Length > 0)
{
Array.Resize(ref _collection, value);
}
else
{
_collection = new T?[value];
}
}
}
}
public CollectionType GetCollectionType => CollectionType.Massive;
2024-03-17 12:28:35 +04:00
/// <summary>
/// Конструктор
/// </summary>
public MassiveGenericObjects()
{
_collection = Array.Empty<T?>();
}
public T? Get(int position)
{
2024-05-05 11:40:18 +04:00
if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position);
if (_collection[position] == null) throw new ObjectNotFoundException(position);
return _collection[position];
2024-03-17 12:28:35 +04:00
}
2024-05-12 14:52:01 +04:00
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
2024-03-17 12:28:35 +04:00
{
2024-05-12 14:52:01 +04:00
if (comparer != null)
{
for (int i = 0; i < Count; i++)
{
if (comparer.Equals(_collection[i], obj))
{
throw new CollectionInsertException(obj);
}
}
}
2024-03-17 12:28:35 +04:00
for (int i = 0; i < Count; i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
2024-05-05 11:40:18 +04:00
throw new CollectionOverflowException(Count);
2024-03-17 12:28:35 +04:00
}
2024-05-12 14:52:01 +04:00
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
2024-03-17 12:28:35 +04:00
{
2024-05-12 14:52:01 +04:00
2024-05-05 12:00:21 +04:00
if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position);
2024-05-12 14:52:01 +04:00
if (comparer != null)
{
for (int i = 0; i < Count; i++)
{
if (comparer.Equals(_collection[i], obj))
{
throw new CollectionInsertException(obj);
}
}
}
2024-05-05 12:00:21 +04:00
if (_collection[position] == null)
2024-03-17 12:28:35 +04:00
{
2024-05-05 12:00:21 +04:00
_collection[position] = obj;
return position;
}
int index = position + 1;
while (index < _collection.Length)
{
if (_collection[index] == null)
2024-03-17 12:28:35 +04:00
{
2024-05-05 12:00:21 +04:00
_collection[index] = obj;
return index;
2024-05-05 11:40:18 +04:00
}
2024-05-05 12:00:21 +04:00
++index;
}
index = position - 1;
while (index >= 0)
{
if (_collection[index] == null)
2024-05-05 11:40:18 +04:00
{
2024-05-05 12:00:21 +04:00
_collection[index] = obj;
return index;
2024-03-17 12:28:35 +04:00
}
2024-05-05 12:00:21 +04:00
--index;
2024-03-17 12:28:35 +04:00
}
2024-05-05 12:00:21 +04:00
throw new CollectionOverflowException(Count);
2024-03-17 12:28:35 +04:00
}
public T? Remove(int position)
{
2024-05-05 11:40:18 +04:00
if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position);
if (_collection[position] == null) throw new ObjectNotFoundException(position);
T? temp = _collection[position];
2024-03-17 12:28:35 +04:00
_collection[position] = null;
2024-05-05 11:40:18 +04:00
return temp;
2024-03-17 12:28:35 +04:00
}
public IEnumerable<T?> GetItems()
{
for (int i = 0; i < _collection.Length; ++i)
{
yield return _collection[i];
}
}
2024-05-12 14:52:01 +04:00
public void CollectionSort(IComparer<T?> comparer)
{
List<T?> lst = [.._collection];
lst.Sort(comparer.Compare);
for (int i = 0; i < _collection.Length; ++i)
{
_collection[i] = lst[i];
}
}
2024-03-17 12:28:35 +04:00
}