2024-06-15 13:28:42 +04:00
|
|
|
|
|
2024-06-15 23:48:53 +04:00
|
|
|
|
using ProjectCruiser.Exceptions;
|
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
namespace ProjectCruiser.CollectionGenericObj;
|
2024-06-13 00:05:05 +04:00
|
|
|
|
|
|
|
|
|
public class ArrayGenObj<T> : ICollectionGenObj<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
// Массив объектов, которые храним
|
|
|
|
|
private T?[] _collection;
|
2024-06-15 18:06:35 +04:00
|
|
|
|
|
|
|
|
|
// Максимально допустимое число объектов в массиве
|
|
|
|
|
private int _maxCount;
|
|
|
|
|
public int Count => _collection.Count(s => s != null);
|
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
public int MaxCount
|
2024-06-13 00:05:05 +04:00
|
|
|
|
{
|
2024-06-15 18:06:35 +04:00
|
|
|
|
get { return _maxCount; }
|
2024-06-15 09:05:36 +04:00
|
|
|
|
set
|
2024-06-13 00:05:05 +04:00
|
|
|
|
{
|
|
|
|
|
if (value > 0)
|
|
|
|
|
{
|
2024-06-15 18:06:35 +04:00
|
|
|
|
if (_collection.Length == 0) _collection = new T?[value];
|
|
|
|
|
else Array.Resize(ref _collection, value);
|
|
|
|
|
|
|
|
|
|
_maxCount = value;
|
2024-06-13 00:05:05 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 13:28:42 +04:00
|
|
|
|
public CollectionType GetCollectionType => CollectionType.Array;
|
2024-06-13 00:05:05 +04:00
|
|
|
|
|
|
|
|
|
public ArrayGenObj()
|
|
|
|
|
{
|
|
|
|
|
_collection = Array.Empty<T?>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// methods :
|
|
|
|
|
public T? GetItem(int index)
|
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (index > Count)
|
|
|
|
|
throw new CollectionOverflowException(index);
|
|
|
|
|
if (index < 0)
|
|
|
|
|
throw new PositionOutOfCollectionException(index);
|
|
|
|
|
|
|
|
|
|
if (_collection[index] == null)
|
|
|
|
|
throw new ObjectNotFoundException(index);
|
|
|
|
|
|
|
|
|
|
return _collection[index];
|
|
|
|
|
}
|
|
|
|
|
catch (CollectionOverflowException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (PositionOutOfCollectionException ex)
|
2024-06-13 00:05:05 +04:00
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
Console.WriteLine(ex.Message);
|
2024-06-13 00:05:05 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T? item)
|
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
if (item == null) { return -1; }
|
2024-06-15 18:06:35 +04:00
|
|
|
|
|
2024-06-15 23:48:53 +04:00
|
|
|
|
try // выход за границы, курируется CollectionOverflowException
|
2024-06-13 00:05:05 +04:00
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
if (Count >= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
throw new CollectionOverflowException(Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// any empty place -> fill immediately
|
|
|
|
|
for (int i = 0; i < _collection.Length; i++)
|
2024-06-13 00:05:05 +04:00
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
if (_collection[i] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[i] = item;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
2024-06-13 00:05:05 +04:00
|
|
|
|
}
|
2024-06-15 23:48:53 +04:00
|
|
|
|
return Count;
|
|
|
|
|
}
|
|
|
|
|
catch (CollectionOverflowException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return -1;
|
2024-06-13 00:05:05 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T? item, int index)
|
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
try
|
2024-06-15 18:06:35 +04:00
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
if (index < 0 || index >= _maxCount) throw new PositionOutOfCollectionException(index);
|
|
|
|
|
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
|
|
|
|
|
|
|
|
|
|
if (item == null) throw new ObjectNotFoundException(index);
|
|
|
|
|
|
|
|
|
|
if (_collection[index] == null)
|
|
|
|
|
{
|
|
|
|
|
_collection[index] = item;
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int min_diff = 100, firstNullIndex = 100;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_collection[i] == null && min_diff > Math.Abs(index - i))
|
|
|
|
|
{
|
|
|
|
|
min_diff = Math.Abs(index - i);
|
|
|
|
|
firstNullIndex = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_collection[firstNullIndex] = item;
|
|
|
|
|
return firstNullIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (CollectionOverflowException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
2024-06-15 18:06:35 +04:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2024-06-15 23:48:53 +04:00
|
|
|
|
catch (ObjectNotFoundException ex)
|
2024-06-13 00:05:05 +04:00
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return -1;
|
2024-06-13 00:05:05 +04:00
|
|
|
|
}
|
2024-06-15 23:48:53 +04:00
|
|
|
|
catch (PositionOutOfCollectionException ex)
|
2024-06-15 18:06:35 +04:00
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return -1;
|
2024-06-13 00:05:05 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? Remove(int index)
|
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
try
|
2024-06-13 00:05:05 +04:00
|
|
|
|
{
|
2024-06-15 23:48:53 +04:00
|
|
|
|
if (index >= _maxCount || index < 0)
|
|
|
|
|
// on the other positions items don't exist
|
|
|
|
|
{
|
|
|
|
|
throw new CollectionOverflowException(index);
|
|
|
|
|
// [?] PositionOutOfCollectionException <<<
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T? item = _collection[index];
|
|
|
|
|
_collection[index] = null;
|
|
|
|
|
|
|
|
|
|
if (item == null) throw new ObjectNotFoundException(index);
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
catch (CollectionOverflowException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
2024-06-15 18:06:35 +04:00
|
|
|
|
return null;
|
2024-06-13 00:05:05 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
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-13 00:05:05 +04:00
|
|
|
|
}
|
|
|
|
|
|