2023-10-11 10:50:39 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Hydroplane.Generics
|
|
|
|
|
{
|
2023-10-25 02:19:13 +04:00
|
|
|
|
internal class SetGeneric<T> where T : class
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 10:59:54 +04:00
|
|
|
|
private readonly List<T?> _places;
|
|
|
|
|
public int Count => _places.Count;
|
2023-10-25 02:19:13 +04:00
|
|
|
|
|
2023-10-25 10:59:54 +04:00
|
|
|
|
private readonly int _maxCount;
|
2023-10-25 02:19:13 +04:00
|
|
|
|
|
2023-10-11 10:50:39 +04:00
|
|
|
|
public SetGeneric(int count)
|
|
|
|
|
{
|
2023-10-25 10:59:54 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T?>(_maxCount);
|
2023-10-11 10:50:39 +04:00
|
|
|
|
}
|
2023-10-25 02:19:13 +04:00
|
|
|
|
|
2023-10-25 10:59:54 +04:00
|
|
|
|
public bool Insert(T plane)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 09:11:32 +04:00
|
|
|
|
return Insert(plane, 0);
|
2023-10-11 10:50:39 +04:00
|
|
|
|
}
|
2023-10-25 10:59:54 +04:00
|
|
|
|
public bool Insert(T plane, int position)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 10:59:54 +04:00
|
|
|
|
if (position < 0 || position >= _maxCount)
|
|
|
|
|
return false;
|
2023-10-25 09:11:32 +04:00
|
|
|
|
|
2023-10-25 10:59:54 +04:00
|
|
|
|
if (Count >= _maxCount)
|
|
|
|
|
return false;
|
|
|
|
|
_places.Insert(0, plane);
|
|
|
|
|
return true;
|
2023-10-11 10:50:39 +04:00
|
|
|
|
}
|
2023-10-25 10:59:54 +04:00
|
|
|
|
|
2023-10-11 10:50:39 +04:00
|
|
|
|
public bool Remove(int position)
|
|
|
|
|
{
|
2023-10-25 10:59:54 +04:00
|
|
|
|
if (position < 0 || position > _maxCount)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
return false;
|
2023-10-25 10:59:54 +04:00
|
|
|
|
if (position >= Count)
|
|
|
|
|
return false;
|
|
|
|
|
_places.RemoveAt(position);
|
2023-10-11 10:50:39 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-10-25 10:59:54 +04:00
|
|
|
|
public T? this[int position]
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 10:59:54 +04:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position > _maxCount)
|
|
|
|
|
return null;
|
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position > _maxCount)
|
|
|
|
|
return;
|
|
|
|
|
_places[position] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<T?> GetPlanes(int? maxPlanes = null)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _places.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _places[i];
|
|
|
|
|
if (maxPlanes.HasValue && i == maxPlanes.Value)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-11 10:50:39 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|