137 lines
3.4 KiB
C#
137 lines
3.4 KiB
C#
using ProjectSeaplane.Drawnings;
|
|
using ProjectSeaplane.Exceptions;
|
|
|
|
namespace ProjectSeaplane.CollectionGenericObjects;
|
|
|
|
/// <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
|
|
{
|
|
get
|
|
{
|
|
return _collection.Length;
|
|
}
|
|
|
|
set
|
|
{
|
|
if (value > 0)
|
|
{
|
|
if (_collection.Length > 0)
|
|
{
|
|
Array.Resize(ref _collection, value);
|
|
}
|
|
else
|
|
{
|
|
_collection = new T?[value];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public CollectionType GetCollectionType => CollectionType.Massive;
|
|
|
|
/// <summary>
|
|
/// Конструктор
|
|
/// </summary>
|
|
public MassiveGenericObjects()
|
|
{
|
|
_collection = Array.Empty<T?>();
|
|
}
|
|
|
|
public T? Get(int position)
|
|
{
|
|
try
|
|
{
|
|
if (_collection[position] == null) throw new ObjectNotFoundException(position);
|
|
return _collection[position];
|
|
}
|
|
catch (IndexOutOfRangeException)
|
|
{
|
|
throw new PositionOutOfCollectionException(position);
|
|
}
|
|
}
|
|
|
|
public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
|
|
{
|
|
return Insert(obj, 0, comparer);
|
|
}
|
|
|
|
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
|
|
{
|
|
// DO
|
|
if (position < 0 || position > _collection.Length - 1)
|
|
{
|
|
throw new PositionOutOfCollectionException();
|
|
}
|
|
if (comparer != null)
|
|
{
|
|
foreach (T? item in _collection)
|
|
{
|
|
if ((comparer as IEqualityComparer<DrawningPlane>).Equals(obj as DrawningPlane, item as DrawningPlane))
|
|
throw new ObjectNotUniqueException();
|
|
}
|
|
}
|
|
|
|
for (int i = position; i < Count; i++)
|
|
{
|
|
if (_collection[i] == null)
|
|
{
|
|
_collection[i] = obj;
|
|
return i;
|
|
}
|
|
}
|
|
for (int i = position - 1; i >= 0; --i)
|
|
{
|
|
if (_collection[i] == null)
|
|
{
|
|
_collection[i] = obj;
|
|
return i;
|
|
}
|
|
}
|
|
throw new CollectionOverflowException(Count);
|
|
}
|
|
|
|
public T? Remove(int position)
|
|
{
|
|
try
|
|
{
|
|
T? obj = _collection[position];
|
|
if (obj == null)
|
|
{
|
|
throw new ObjectNotFoundException(position);
|
|
}
|
|
_collection[position] = null;
|
|
return obj;
|
|
}
|
|
catch (IndexOutOfRangeException)
|
|
{
|
|
throw new PositionOutOfCollectionException(position);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<T?> GetItems()
|
|
{
|
|
for (int i = 0; i < _collection.Length; ++i)
|
|
{
|
|
yield return _collection[i];
|
|
}
|
|
}
|
|
|
|
public void CollectionSort(IComparer<T?> comparer)
|
|
{
|
|
Array.Sort(_collection, comparer);
|
|
Array.Reverse(_collection);
|
|
}
|
|
} |