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
|
|
|
|
{
|
|
|
|
|
private readonly T?[] _places;
|
2023-10-25 02:19:13 +04:00
|
|
|
|
|
2023-10-11 10:50:39 +04:00
|
|
|
|
public int Count => _places.Length;
|
2023-10-25 02:19:13 +04:00
|
|
|
|
|
2023-10-11 10:50:39 +04:00
|
|
|
|
public SetGeneric(int count)
|
|
|
|
|
{
|
|
|
|
|
_places = new T?[count];
|
|
|
|
|
}
|
2023-10-25 02:19:13 +04:00
|
|
|
|
|
|
|
|
|
public int Insert(T plane)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 02:19:13 +04:00
|
|
|
|
int index = -1;
|
|
|
|
|
for (int i = 0; i < _places.Length; i++)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 02:19:13 +04:00
|
|
|
|
if (_places[i] == null)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 02:19:13 +04:00
|
|
|
|
index = i; break;
|
2023-10-11 10:50:39 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-25 02:19:13 +04:00
|
|
|
|
if (index < 0)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 02:19:13 +04:00
|
|
|
|
return -1;
|
2023-10-11 10:50:39 +04:00
|
|
|
|
}
|
2023-10-25 02:19:13 +04:00
|
|
|
|
for (int i = index; i > 0; i--)
|
|
|
|
|
{
|
|
|
|
|
_places[i] = _places[i - 1];
|
|
|
|
|
}
|
|
|
|
|
_places[0] = plane;
|
|
|
|
|
return 0;
|
2023-10-11 10:50:39 +04:00
|
|
|
|
}
|
2023-10-25 02:19:13 +04:00
|
|
|
|
|
|
|
|
|
public int Insert(T plane, int position)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 02:19:13 +04:00
|
|
|
|
if (position < 0 || position >= Count)
|
|
|
|
|
return -1;
|
|
|
|
|
if (_places[position] == null)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 02:19:13 +04:00
|
|
|
|
_places[position] = plane;
|
|
|
|
|
return position;
|
2023-10-11 10:50:39 +04:00
|
|
|
|
}
|
2023-10-25 02:19:13 +04:00
|
|
|
|
int index = -1;
|
|
|
|
|
for (int i = position; i < Count; i++)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
2023-10-25 02:19:13 +04:00
|
|
|
|
if (_places[i] == null)
|
|
|
|
|
{
|
|
|
|
|
index = i; break;
|
|
|
|
|
}
|
2023-10-11 10:50:39 +04:00
|
|
|
|
}
|
2023-10-25 02:19:13 +04:00
|
|
|
|
if (index < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
for (int i = index; index > position; i--)
|
2023-10-11 10:50:39 +04:00
|
|
|
|
{
|
|
|
|
|
_places[i] = _places[i - 1];
|
|
|
|
|
}
|
|
|
|
|
_places[position] = plane;
|
2023-10-25 02:19:13 +04:00
|
|
|
|
return position;
|
2023-10-11 10:50:39 +04:00
|
|
|
|
}
|
2023-10-25 02:19:13 +04:00
|
|
|
|
|
2023-10-11 10:50:39 +04:00
|
|
|
|
public bool Remove(int position)
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position >= _places.Count())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
_places[position] = null;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-10-25 02:19:13 +04:00
|
|
|
|
|
2023-10-11 10:50:39 +04:00
|
|
|
|
public T? Get(int position)
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position >= _places.Count())
|
|
|
|
|
return null;
|
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|