120 lines
3.7 KiB
C#
120 lines
3.7 KiB
C#
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 List<T?> _places;
|
||
/// <summary>
|
||
/// Количество объектов в массиве
|
||
/// </summary>
|
||
public int Count => _places.Capacity;
|
||
/// <summary>
|
||
/// Максимальное количество объектов в списке
|
||
/// </summary>
|
||
private readonly int _maxCount;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="count"></param>
|
||
public SetGeneric(int count)
|
||
{
|
||
_maxCount = count;
|
||
_places = new List<T?>(count);
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="liner">Добавляемый лайнер</param>
|
||
/// <returns></returns>
|
||
public int Insert(T liner)
|
||
{
|
||
if (_places.Count + 1 <= _maxCount)
|
||
{
|
||
_places.Insert(0, liner);
|
||
return 0;
|
||
}
|
||
return -1;
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор на конкретную позицию
|
||
/// </summary>
|
||
/// <param name="liner">Добавляемый лайнер</param>
|
||
/// <param name="position">Позиция</param>
|
||
/// <returns></returns>
|
||
public int Insert(T liner, int position)
|
||
{
|
||
if (_places.Count + 1 <= _maxCount && _places.Count >= position)
|
||
{
|
||
_places.Insert(position, liner);
|
||
return position;
|
||
}
|
||
return -1;
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора с конкретной позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public bool Remove(int position)
|
||
{
|
||
if(_places.Count > position && position >= 0)
|
||
{
|
||
_places.RemoveAt(position);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
/// <summary>
|
||
/// Получение объекта из набора по позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public T? this[int position]
|
||
{
|
||
get
|
||
{
|
||
if (_places.Count > position && position >= 0)
|
||
{
|
||
return _places[position];
|
||
}
|
||
return null;
|
||
}
|
||
set
|
||
{
|
||
if(_places.Count + 1 <= _maxCount && position >= 0 && _places.Count > position)
|
||
{
|
||
_places[position] = value;
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Проход по списку
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public IEnumerable<T?> GetLiners(int? maxLiners = null)
|
||
{
|
||
for (int i = 0; i < _places.Count; ++i)
|
||
{
|
||
yield return _places[i];
|
||
if (maxLiners.HasValue && i == maxLiners.Value)
|
||
{
|
||
yield break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|