2023-11-24 12:39:11 +04:00

112 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Liner.Generics
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
public 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="liner">Добавляемый лайнер</param>
/// <returns></returns>
public int Insert(T liner)
{
int nulli = 0;
while (_places[nulli] != null)
{
nulli++;
if(nulli == _places.Length)
{
return -1; // нет пустого места
}
}
//сдвиг
for(int i = nulli; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = liner;
return 0;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="liner">Добавляемый лайнер</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T liner, int position)
{
if (position < 0 || position > _places.Length)
{
return -1;
}
if (_places[position] != null)
{
int nulli = position;
while (_places[nulli] != null)
{
nulli++;
if (nulli == _places.Length)
{
return -1; // нет пустого места
}
}
}
_places[position] = liner;
return position;
}
/// <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="position"></param>
/// <returns></returns>
public T? Get(int position)
{
if (position < 0 || position > _places.Length)
{
return null;
}
return _places[position];
}
}
}