83 lines
2.2 KiB
C#
Raw Normal View History

2023-12-21 21:21:02 +04:00
using ProjectLainer.Exceptions;
using System.Numerics;
2023-12-19 18:48:02 +04:00
namespace ProjectLainer.Generics
2023-12-08 21:11:13 +04:00
{
internal class SetGeneric<T>
where T : class
{
2023-12-19 18:48:02 +04:00
private readonly List<T?> _places;
public int Count => _places.Count;
private readonly int _maxCount;
2023-12-08 21:11:13 +04:00
public SetGeneric(int count)
{
2023-12-19 18:48:02 +04:00
_maxCount = count;
_places = new List<T?>(count);
2023-12-08 21:11:13 +04:00
}
2023-12-21 21:21:02 +04:00
public void Insert(T lainer)
2023-12-08 21:11:13 +04:00
{
2023-12-19 18:48:02 +04:00
if (_places.Count == _maxCount)
2023-12-08 21:11:13 +04:00
{
2023-12-21 21:21:02 +04:00
throw new StorageOverflowException(_maxCount);
2023-12-08 21:11:13 +04:00
}
2023-12-19 18:48:02 +04:00
Insert(lainer, 0);
2023-12-08 21:11:13 +04:00
}
2023-12-21 21:21:02 +04:00
public void Insert(T lainer, int position)
2023-12-08 21:11:13 +04:00
{
2023-12-21 21:21:02 +04:00
if (position >= 0 && position <= Count)
2023-12-08 21:11:13 +04:00
{
2023-12-21 21:21:02 +04:00
_places.Insert(position, lainer);
}
else
{
throw new Exception("Неверная позиция для вставки");
2023-12-08 21:11:13 +04:00
}
2023-12-19 18:48:02 +04:00
}
2023-12-21 21:21:02 +04:00
public void Remove(int position)
2023-12-19 18:48:02 +04:00
{
2023-12-21 21:21:02 +04:00
if (position < Count && position >= 0)
{
_places.RemoveAt(position);
}
else
2023-12-08 21:11:13 +04:00
{
2023-12-21 21:21:02 +04:00
throw new LainerNotFoundException(position);
2023-12-08 21:11:13 +04:00
}
}
2023-12-19 18:48:02 +04:00
public T? this[int position]
2023-12-08 21:11:13 +04:00
{
2023-12-19 18:48:02 +04:00
get
{
if(position < 0 || position >= Count)
{
return null;
}
2023-12-08 21:11:13 +04:00
return _places[position];
}
2023-12-19 18:48:02 +04:00
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;
}
}
2023-12-08 21:11:13 +04:00
}
}
}