2022-12-15 13:44:30 +03:00

106 lines
3.2 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 AirFighter
{
internal class SetCarsGeneric<T>
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
/// </summary>
private readonly List<T> _places;
/// <summary>
/// Количество объектов в списке
/// </summary>
public int Count => _places.Count;
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetCarsGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
}
public int Insert(T car)
{
if (_places.Contains(car)) return -1;
// TODO вставка в начало набора
if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount);
_places.Insert(0, car);
return 0;
}
public int Insert(T car, int position)
{
if (_places.Contains(car)) return -1;
if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount);
_places.Insert(position, car);
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Remove(int position)
{
if(position >= _maxCount || position >= _places.Count) throw new AircraftNotFoundException(position);
T res = _places[position];
_places.Remove(res);
if (res == null) throw new AircraftNotFoundException(position);
return res;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T this[int position]
{
get
{
// TODO проверка позиции
if(position < 0) return null;
if(position >= _places.Count) return null;
return _places[position];
}
set
{
// TODO проверка позиции
if (position < 0) return;
if (position >= _places.Count) return;
// TODO вставка в список по позиции
Insert(value, position);
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null) return;
_places.Sort(comparer);
}
public IEnumerable<T> GetCars()
{
foreach (var car in _places)
{
if (car != null)
{
yield return car;
}
else
{
yield break;
}
}
}
}
}