109 lines
3.2 KiB
C#
109 lines
3.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics.Eventing.Reader;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Cruiser.Generics
|
||
{
|
||
/// <summary>
|
||
/// Параметризованный набор объектов
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
internal class SetGeneric<T>
|
||
where T : class
|
||
{
|
||
/// <summary>
|
||
/// Массив объектов, которые храним
|
||
/// </summary>
|
||
private readonly T?[] _places;
|
||
/// <summary>
|
||
/// Количество объектов в массиве
|
||
/// </summary>
|
||
public int Count => _places.Length;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="count"></param>
|
||
public SetGeneric(int count)
|
||
{
|
||
_places = new T?[count];
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="cruiser">Добавляемый лайнер</param>
|
||
/// <returns></returns>
|
||
public int Insert(T cruiser)
|
||
{
|
||
int zero = 0;
|
||
for (; _places[zero] != null;)
|
||
{
|
||
zero++;
|
||
if (zero == _places.Length)
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
for (int i = zero; i > 0; i--)
|
||
{
|
||
_places[i] = _places[i - 1];
|
||
}
|
||
_places[0] = cruiser;
|
||
return 0;
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора с конкретной позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public bool Remove(int position)
|
||
{
|
||
if (position < 0 || position > _places.Length)
|
||
{
|
||
return false;
|
||
}
|
||
_places[position] = null;
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор на конкретную позицию
|
||
/// </summary>
|
||
/// <param name="cruiser">Добавляемый автомобиль</param>
|
||
/// <param name="position">Позиция</param>
|
||
/// <returns></returns>
|
||
public int Insert(T cruiser, int position)
|
||
{
|
||
if (position < 0 || position > _places.Length)
|
||
{
|
||
return 0;
|
||
}
|
||
if (_places.Length != null)
|
||
{
|
||
int zero = position;
|
||
while (_places[zero] != null)
|
||
{
|
||
zero++;
|
||
return -1;
|
||
}
|
||
}
|
||
_places[position] = cruiser;
|
||
return position;
|
||
}
|
||
/// <summary>
|
||
/// Получение объекта из набора по позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public T? Get(int position)
|
||
{
|
||
if (position < 0 || position > _places.Length)
|
||
{
|
||
return null;
|
||
}
|
||
return _places[position];
|
||
}
|
||
}
|
||
}
|