PIbd-23_Starostin_I.K._Lain.../lainer/Lainer1/SetGeneric.cs

76 lines
2.0 KiB
C#

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 bool Insert(T lainer)
{
if (_places.Count == _maxCount)
{
return false;
}
Insert(lainer, 0);
return true;
}
public bool Insert(T lainer, int position)
{
if (position < 0 || position > Count || _places.Count >= _maxCount)
{
return false;
}
_places.Insert(position, lainer);
return true;
}
public bool Remove(int position)
{
if (position < 0 || position >= Count)
{
return false;
}
_places.RemoveAt(position);
return true;
}
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;
}
}
}
}
}