99 lines
3.0 KiB
C#
99 lines
3.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Trolleybus
|
||
{
|
||
// Параметризованный набор объектов
|
||
internal class SetTrolleybusGeneric<T>
|
||
where T : class, IEquatable<T>
|
||
{
|
||
// Массив объектов, которые храним
|
||
private readonly List<T> _places;
|
||
// Количество объектов в массиве
|
||
public int Count => _places.Count;
|
||
|
||
private readonly int _maxCount;
|
||
// Конструктор
|
||
public SetTrolleybusGeneric(int count)
|
||
{
|
||
_maxCount = count;
|
||
_places = new List<T>();
|
||
}
|
||
private bool CorrectPos(int pos)
|
||
{
|
||
return 0 <= pos && pos < _maxCount;
|
||
}
|
||
// Добавление объекта в набор
|
||
public int Insert(T trolleybus)
|
||
{
|
||
if (_places.Count > _maxCount)
|
||
{
|
||
return -1;
|
||
}
|
||
// вставка в начало набора
|
||
return Insert(trolleybus, 0);
|
||
}
|
||
// Добавление объекта в набор на конкретную позицию
|
||
public int Insert(T trolleybus, int position)
|
||
{
|
||
// проверка позиции
|
||
if (!CorrectPos(position))
|
||
{
|
||
return -1;
|
||
}
|
||
|
||
// вставка по позиции
|
||
_places.Insert(position, trolleybus);
|
||
return position;
|
||
}
|
||
// Удаление объекта из набора с конкретной позиции
|
||
public T Remove(int position)
|
||
{
|
||
// проверка позиции
|
||
if (!CorrectPos(position))
|
||
return null;
|
||
// удаление объекта из массива, присовив элементу массива значение null
|
||
T temp = _places[position];
|
||
_places.RemoveAt(position);
|
||
return temp;
|
||
}
|
||
// Получение объекта из набора по позиции
|
||
public T this[int position]
|
||
{
|
||
get
|
||
{
|
||
return CorrectPos(position) && position < Count ? _places[position] : null;
|
||
}
|
||
set
|
||
{
|
||
Insert(value, position);
|
||
}
|
||
}
|
||
public IEnumerable<T> GetTrolleybus()
|
||
{
|
||
foreach (var trolleybus in _places)
|
||
{
|
||
if (trolleybus != null)
|
||
{
|
||
yield return trolleybus;
|
||
}
|
||
else
|
||
{
|
||
yield break;
|
||
}
|
||
}
|
||
}
|
||
|
||
public void SortSet(IComparer<T> comparer)
|
||
{
|
||
if (comparer == null)
|
||
{
|
||
return;
|
||
}
|
||
_places.Sort(comparer);
|
||
}
|
||
}
|
||
} |