2023-11-04 16:22:00 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace DumpTruck.Generics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Параметризованный набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
internal class SetGeneric<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Массив объектов, которые храним
|
|
|
|
|
/// </summary>
|
2023-11-04 16:38:42 +04:00
|
|
|
|
private readonly List<T?> _places;
|
2023-11-04 16:22:00 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Количество объектов в массиве
|
|
|
|
|
/// </summary>
|
2023-11-04 16:38:42 +04:00
|
|
|
|
public int Count => _places.Count;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Максимальное количество объектов в списке
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly int _maxCount;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="count"></param>
|
|
|
|
|
public SetGeneric(int count)
|
2023-11-04 16:22:00 +04:00
|
|
|
|
{
|
2023-11-04 16:38:42 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T?>(count);
|
2023-11-04 16:22:00 +04:00
|
|
|
|
}
|
2023-11-04 16:38:42 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="truck">Добавляемый грузовик</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int Insert(T truck)
|
2023-11-04 16:22:00 +04:00
|
|
|
|
{
|
|
|
|
|
return Insert(truck, 0);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор на конкретную позицию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="truck">Добавляемый грузовик</param>
|
|
|
|
|
/// <param name="position">Позиция</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int Insert(T truck, int position)
|
|
|
|
|
{
|
2023-11-04 16:38:42 +04:00
|
|
|
|
if (position < 0 || position > Count || Count >= _maxCount) return -1;
|
|
|
|
|
_places.Insert(position, truck);
|
2023-11-04 16:22:00 +04:00
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление объекта из набора с конкретной позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Remove(int position)
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position >= Count) return false;
|
2023-11-04 16:38:42 +04:00
|
|
|
|
_places.RemoveAt(position);
|
2023-11-04 16:22:00 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-11-04 16:38:42 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение объекта из набора по позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public T? this[int position]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position >= Count) return null;
|
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (position < 0 || position > Count || Count >= _maxCount) return;
|
|
|
|
|
_places.Insert(position, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Проход по списку
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IEnumerable<T?> GetTrucks(int? maxTrucks = null)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _places.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
yield return _places[i];
|
|
|
|
|
if (maxTrucks.HasValue && i == maxTrucks.Value)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-04 16:22:00 +04:00
|
|
|
|
}
|