PIbd22_Kuznetsov_A.V._Excav.../Excavator/SetGeneric.cs

86 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Excavator
{
internal class SetGeneric<T>
where T : class
{
private readonly T?[] _places;
public int Count => _places.Length - 1;
public SetGeneric(int count)
{
_places = new T?[count];
}
public int Insert(T Mash)
{
int pos = Count - 1;
if (_places[Count] != null)
{
for (int i = pos; i > 0; --i)
{
if (_places[i] == null)
{
pos = i;
break;
}
}
for (int i = pos + 1; i <= Count; ++i)
{
_places[i - 1] = _places[i];
}
}
_places[Count] = Mash;
return pos;
}
public bool Insert(T Mash, int position)
{
if (position < 0 || position > Count)
{
return false;
}
if (_places[Count] != null)
{
int pos = Count;
for (int i = Count; i > 0; --i)
{
if (_places[i] == null)
{
pos = i;
break;
}
}
for (int i = Count; i >= pos; --i)
{
_places[i - 1] = _places[i];
}
}
_places[Count] = Mash;
return true;
}
public bool Remove(int position)
{
if (position < 0 || position > Count)
{
return false;
}
_places[position] = null;
return true;
}
public T? Get(int position)
{
if (position < 0 || position > Count)
{
return null;
}
return _places[position];
}
}
}