2022-09-30 16:33:17 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Artilleries
|
|
|
|
|
{
|
|
|
|
|
internal class SetArtilleriesGeneric<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
private readonly T[] _places;
|
|
|
|
|
public int Count => _places.Length;
|
|
|
|
|
|
|
|
|
|
public SetArtilleriesGeneric(int count)
|
|
|
|
|
{
|
|
|
|
|
_places = new T[count];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Insert(T artillery)
|
|
|
|
|
{
|
2022-09-30 17:24:13 +04:00
|
|
|
|
return Insert(artillery, 0);
|
2022-09-30 16:33:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Insert(T artillery, int position)
|
|
|
|
|
{
|
2022-09-30 17:24:13 +04:00
|
|
|
|
if (position < 0 || position >= Count)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-30 20:17:08 +04:00
|
|
|
|
if (_places[position] == null)
|
|
|
|
|
{
|
|
|
|
|
_places[position] = artillery;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-09-30 17:24:13 +04:00
|
|
|
|
|
2022-09-30 20:17:08 +04:00
|
|
|
|
int firstNull = -1;
|
|
|
|
|
|
|
|
|
|
for (int i = position + 1; i < Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_places[i] == null)
|
|
|
|
|
{
|
|
|
|
|
firstNull = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (firstNull == -1)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = firstNull; i > position; i--)
|
|
|
|
|
{
|
|
|
|
|
(_places[i], _places[i - 1]) = (_places[i - 1], _places[i]);
|
|
|
|
|
}
|
2022-09-30 17:24:13 +04:00
|
|
|
|
|
2022-09-30 16:33:17 +04:00
|
|
|
|
_places[position] = artillery;
|
2022-09-30 20:17:08 +04:00
|
|
|
|
return true;
|
2022-09-30 16:33:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Remove(int position)
|
|
|
|
|
{
|
2022-09-30 17:24:13 +04:00
|
|
|
|
if (position < 0 || position >= Count || _places[position] == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_places[position] = null;
|
|
|
|
|
return true;
|
2022-09-30 16:33:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T Get(int position)
|
|
|
|
|
{
|
2022-09-30 17:24:13 +04:00
|
|
|
|
if (position < 0 || position >= Count)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-30 16:33:17 +04:00
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|