106 lines
3.2 KiB
C#
106 lines
3.2 KiB
C#
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;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|