93 lines
2.7 KiB
C#
93 lines
2.7 KiB
C#
using ProjectLainer.DrawningObjects;
|
|
using ProjectLainer.Exceptions;
|
|
using System.Numerics;
|
|
|
|
namespace ProjectLainer.Generics
|
|
{
|
|
internal class SetGeneric<T>
|
|
where T : class
|
|
{
|
|
private readonly List<T?> _places;
|
|
public int Count => _places.Count;
|
|
private readonly int _maxCount;
|
|
public SetGeneric(int count)
|
|
{
|
|
_maxCount = count;
|
|
_places = new List<T?>(count);
|
|
}
|
|
|
|
public void Insert(T lainer, IEqualityComparer<T?>? equal = null)
|
|
{
|
|
if (_places.Count == _maxCount)
|
|
{
|
|
throw new StorageOverflowException(_maxCount);
|
|
}
|
|
Insert(lainer, 0, equal);
|
|
}
|
|
|
|
public void Insert(T lainer, int position, IEqualityComparer<T?>? equal = null)
|
|
{
|
|
// объект есть уже в коллекции - выбросить исключение
|
|
if (!(position >= 0 && position <= Count))
|
|
{
|
|
throw new Exception("Неверная позиция для вставки");
|
|
}
|
|
if (equal != null)
|
|
{
|
|
foreach (T elem in _places)
|
|
{
|
|
if (equal.Equals(lainer, elem))
|
|
{
|
|
throw new Exception("такой объект уже есть");
|
|
}
|
|
}
|
|
}
|
|
_places.Insert(position, lainer);
|
|
}
|
|
|
|
public void Remove(int position)
|
|
{
|
|
if (position < Count && position >= 0)
|
|
{
|
|
_places.RemoveAt(position);
|
|
}
|
|
else
|
|
{
|
|
throw new LainerNotFoundException(position);
|
|
}
|
|
}
|
|
public T? this[int position]
|
|
{
|
|
get
|
|
{
|
|
if(position < 0 || position >= Count)
|
|
{
|
|
return null;
|
|
}
|
|
return _places[position];
|
|
}
|
|
set
|
|
{
|
|
if (position < 0 || position >= Count || Count >= _maxCount)
|
|
{
|
|
return;
|
|
}
|
|
_places.Insert(position, value);
|
|
}
|
|
}
|
|
public IEnumerable<T?> GetLainers(int? maxLainers = null)
|
|
{
|
|
for (int i = 0; i < _places.Count; ++i)
|
|
{
|
|
yield return _places[i];
|
|
if (maxLainers.HasValue && i == maxLainers.Value)
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
|
|
}
|
|
}
|
|
|