131 lines
3.9 KiB
C#
131 lines
3.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AircraftCarrier
|
||
{
|
||
/// <summary>
|
||
/// Параметризованный набор объектов
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
internal class SetWarshipsGeneric<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 SetWarshipsGeneric(int count)
|
||
{
|
||
_maxCount = count;
|
||
_places = new List<T>();
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="warship">Добавляемый военный корабль</param>
|
||
/// <returns></returns>
|
||
public int Insert(T warship)
|
||
{
|
||
return Insert(warship, 0);
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор на конкретную позицию
|
||
/// </summary>
|
||
/// <param name="warship">Добавляемый военный корабль</param>
|
||
/// <param name="position">Позиция</param>
|
||
/// <returns></returns>
|
||
public int Insert(T warship, int position)
|
||
{
|
||
if (_places.Contains(warship))
|
||
return -1;
|
||
|
||
if (Count == _maxCount)
|
||
throw new StorageOverflowException(_maxCount);
|
||
|
||
if (!isCorrectPosition(position)) return -1;
|
||
_places.Insert(position, warship);
|
||
return 1;
|
||
}
|
||
|
||
private bool isCorrectPosition(int position)
|
||
{
|
||
return 0 <= position && position < _maxCount;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление объекта из набора с конкретной позиции
|
||
/// </summary>
|
||
/// <param name="position"></param>
|
||
/// <returns></returns>
|
||
public T Remove(int position)
|
||
{
|
||
if (!isCorrectPosition(position))
|
||
return null;
|
||
var result = this[position];
|
||
if (result == null)
|
||
throw new WarshipNotFoundException(position);
|
||
_places.RemoveAt(position);
|
||
return result;
|
||
}
|
||
/// <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> GetWarships()
|
||
{
|
||
foreach(var warship in _places)
|
||
{
|
||
if(warship != null)
|
||
{
|
||
yield return warship;
|
||
}
|
||
else
|
||
{
|
||
yield break;
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Сортировка набора объектов
|
||
/// </summary>
|
||
/// <param name="comparer"></param>
|
||
public void SortSet(IComparer<T> comparer)
|
||
{
|
||
if (comparer == null)
|
||
{
|
||
return;
|
||
}
|
||
_places.Sort(comparer);
|
||
}
|
||
}
|
||
}
|