PIbd-14_Pruidze_I.K_Simple_.../ProjectCruiser/CollectionGenericObj/ArrayGenObj.cs

148 lines
3.8 KiB
C#
Raw Normal View History

2024-06-16 21:39:45 +04:00
using ProjectCruiser.DrawningSamples;
using ProjectCruiser.Exceptions;
2024-06-15 13:28:42 +04:00
namespace ProjectCruiser.CollectionGenericObj;
public class ArrayGenObj<T> : ICollectionGenObj<T>
where T : class
{
// Массив объектов, которые храним
private T?[] _collection;
2024-06-15 18:06:35 +04:00
// Максимально допустимое число объектов в массиве
private int _maxCount;
2024-06-16 19:46:37 +04:00
public int Count => _collection.Count(s => (s != null));
2024-06-15 18:06:35 +04:00
2024-06-15 13:28:42 +04:00
public int MaxCount
{
2024-06-15 18:06:35 +04:00
get { return _maxCount; }
2024-06-15 09:05:36 +04:00
set
{
if (value > 0)
{
2024-06-16 19:46:37 +04:00
_maxCount = value;
2024-06-15 18:06:35 +04:00
if (_collection.Length == 0) _collection = new T?[value];
else Array.Resize(ref _collection, value);
}
}
}
2024-06-15 13:28:42 +04:00
public CollectionType GetCollectionType => CollectionType.Array;
public ArrayGenObj()
{
_collection = Array.Empty<T?>();
}
// methods :
public T? GetItem(int index)
{
2024-06-16 19:46:37 +04:00
if (index > _maxCount)
throw new CollectionOverflowException(index);
if (index < 0)
throw new PositionOutOfCollectionException(index);
2024-06-16 19:46:37 +04:00
if (_collection[index] == null)
throw new ObjectNotFoundException(index);
2024-06-16 19:46:37 +04:00
return _collection[index];
}
2024-06-16 21:39:45 +04:00
public int Insert(T? item, IEqualityComparer<DrawningBase?>? cmpr = null)
{
2024-06-16 19:46:37 +04:00
if (item == null) throw
new NullReferenceException("> Inserting item is null");
2024-06-16 21:39:45 +04:00
else
{
if (cmpr != null && item == cmpr)
{
throw new Exception();
}
}
2024-06-15 18:06:35 +04:00
2024-06-16 19:46:37 +04:00
// выход за границы, курируется CollectionOverflowException
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
2024-06-16 19:46:37 +04:00
// any empty place -> fill immediately
for (int i = Count; i < _maxCount; i++)
{
if (_collection[i] == null)
{
2024-06-16 19:46:37 +04:00
_collection[i] = item;
return i;
}
}
2024-06-16 19:46:37 +04:00
return Count;
}
2024-06-16 21:39:45 +04:00
public int Insert(T? item, int index, IEqualityComparer<DrawningBase?>? cmpr = null)
{
2024-06-16 21:39:45 +04:00
2024-06-16 19:46:37 +04:00
if (index < 0 || index >= _maxCount) throw new PositionOutOfCollectionException(index);
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
2024-06-16 19:46:37 +04:00
if (item == null) throw
new NullReferenceException("> Inserting item (at position) is null");
2024-06-16 21:39:45 +04:00
else
{
if (cmpr != null && item == cmpr)
{
throw new Exception();
}
}
2024-06-16 19:46:37 +04:00
if (_collection[index] == null)
{
_collection[index] = item;
return index;
}
else
{
int min_diff = 100, firstNullIndex = 100;
2024-06-16 19:46:37 +04:00
for (int i = 0; i < Count; i++)
{
if (_collection[i] == null && min_diff > Math.Abs(index - i))
{
2024-06-16 19:46:37 +04:00
min_diff = Math.Abs(index - i);
firstNullIndex = i;
}
}
2024-06-16 19:46:37 +04:00
_collection[firstNullIndex] = item;
return firstNullIndex;
}
}
public T? Remove(int index)
{
2024-06-16 19:46:37 +04:00
if (index >= _maxCount || index < 0)
// on the other positions items don't exist
{
2024-06-16 19:46:37 +04:00
throw new PositionOutOfCollectionException(index);
}
2024-06-16 19:46:37 +04:00
T? item = _collection[index];
_collection[index] = null;
2024-06-16 19:46:37 +04:00
if (item == null) throw new ObjectNotFoundException(index);
2024-06-16 19:46:37 +04:00
return item;
}
2024-06-15 13:28:42 +04:00
public IEnumerable<T?> GetItems()
{
2024-06-15 18:06:35 +04:00
for (int i = 0; i < MaxCount; ++i)
2024-06-15 13:28:42 +04:00
{
yield return _collection[i];
}
}
2024-06-16 21:39:45 +04:00
2024-06-16 23:08:52 +04:00
void ICollectionGenObj<T>.CollectionSort(IComparer<T?> comparer)
2024-06-16 21:39:45 +04:00
{
Array.Sort(_collection, comparer);
}
}