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

76 lines
2.0 KiB
C#
Raw Normal View History

2023-12-19 18:48:02 +04:00
using System.Numerics;
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-19 18:48:02 +04:00
public bool 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-19 18:48:02 +04:00
return false;
2023-12-08 21:11:13 +04:00
}
2023-12-19 18:48:02 +04:00
Insert(lainer, 0);
return true;
2023-12-08 21:11:13 +04:00
}
2023-12-19 18:48:02 +04:00
public bool Insert(T lainer, int position)
2023-12-08 21:11:13 +04:00
{
2023-12-19 18:48:02 +04:00
if (position < 0 || position > Count || _places.Count >= _maxCount)
2023-12-08 21:11:13 +04:00
{
return false;
}
2023-12-19 18:48:02 +04:00
_places.Insert(position, lainer);
return true;
}
public bool Remove(int position)
{
if (position < 0 || position >= Count)
2023-12-08 21:11:13 +04:00
{
return false;
}
2023-12-19 18:48:02 +04:00
_places.RemoveAt(position);
2023-12-08 21:11:13 +04:00
return true;
}
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
}
}
}