113 lines
3.3 KiB
C#
113 lines
3.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AirBomber
|
||
{
|
||
/// <summary>
|
||
/// Параметризованный набор объектов
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
internal class SetAirplanesGeneric<T>
|
||
where T : class
|
||
{
|
||
/// <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 SetAirplanesGeneric(int count)
|
||
{
|
||
_maxcount = count;
|
||
_places = new List<T>();
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="airplane">Добавляемый самолет</param>
|
||
/// <returns></returns>
|
||
public bool Insert(T airplane)
|
||
{
|
||
return Insert(airplane, 0);
|
||
}
|
||
|
||
private bool isCorrectPosition(int position)
|
||
{
|
||
return 0 <= position && position < _maxcount;
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор на конкретную позицию
|
||
/// </summary>
|
||
/// <param name="airplane">Добавляемый самолет</param>
|
||
/// <param name="position">Позиция</param>
|
||
/// <returns></returns>
|
||
public bool Insert(T airplane, int position)
|
||
{
|
||
if (!isCorrectPosition(position))
|
||
{
|
||
return false;
|
||
}
|
||
_places.Insert(position, airplane);
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора с конкретной позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public bool Remove(int position)
|
||
{
|
||
if (!isCorrectPosition(position))
|
||
return false;
|
||
_places.RemoveAt(position);
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Получение объекта из набора по позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public T this[int position]
|
||
{
|
||
get
|
||
{
|
||
return isCorrectPosition(position) && position < Count ? _places[position] : null;
|
||
}
|
||
set
|
||
{
|
||
Insert(value, position);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Проход по набору до первого пустого
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public IEnumerable<T> GetAirplanes()
|
||
{
|
||
foreach (var airplane in _places)
|
||
{
|
||
if (airplane != null)
|
||
{
|
||
yield return airplane;
|
||
}
|
||
else
|
||
{
|
||
yield break;
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|