2022-11-06 23:24:34 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Catamaran
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Параметризованный набор объектов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
internal class SetBoatsGeneric<T>
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Массив объектов, которые храним
|
|
|
|
|
/// </summary>
|
2022-11-06 23:39:21 +04:00
|
|
|
|
private readonly List<T> _places;
|
2022-11-06 23:24:34 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Количество объектов в массиве
|
|
|
|
|
/// </summary>
|
2022-11-06 23:39:21 +04:00
|
|
|
|
public int Count => _places.Count;
|
|
|
|
|
|
|
|
|
|
private readonly int _maxCount;
|
2022-11-06 23:24:34 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="count"></param>
|
|
|
|
|
public SetBoatsGeneric(int count)
|
|
|
|
|
{
|
2022-11-06 23:39:21 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T>();
|
2022-11-06 23:24:34 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="boat">Добавляемая лодка </param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int Insert(T boat)
|
|
|
|
|
{
|
2022-11-06 23:39:21 +04:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < _maxCount; i++)
|
2022-11-06 23:24:34 +04:00
|
|
|
|
{
|
2022-11-06 23:39:21 +04:00
|
|
|
|
if (i == Count)
|
2022-11-06 23:24:34 +04:00
|
|
|
|
{
|
2022-11-06 23:39:21 +04:00
|
|
|
|
_places.Insert(i, boat);
|
2022-11-06 23:24:34 +04:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 22:13:04 +04:00
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
|
|
|
|
|
2022-11-06 23:24:34 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление объекта в набор на конкретную позицию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="boat">Добавляемая лодка</param>
|
|
|
|
|
/// <param name="position">Позиция</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int Insert(T boat, int position)
|
|
|
|
|
{
|
|
|
|
|
// TODO проверка позиции
|
2022-12-03 22:13:04 +04:00
|
|
|
|
if (position < 0 || position >= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
|
|
|
|
|
|
}
|
2022-11-06 23:24:34 +04:00
|
|
|
|
// TODO проверка, что элемент массива по этой позиции пустой,если нет, то
|
|
|
|
|
// проверка, что после вставляемого элемента в массиве есть пустой элемент
|
|
|
|
|
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
|
2022-11-06 23:39:21 +04:00
|
|
|
|
if (position == Count)
|
2022-11-06 23:24:34 +04:00
|
|
|
|
{
|
2022-11-06 23:39:21 +04:00
|
|
|
|
_places.Insert(position, boat);
|
2022-11-06 23:24:34 +04:00
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-11-06 23:39:21 +04:00
|
|
|
|
for (int i = position + 1; i < _maxCount; i++)
|
2022-11-06 23:24:34 +04:00
|
|
|
|
{
|
2022-11-06 23:39:21 +04:00
|
|
|
|
if (i == Count)
|
2022-11-06 23:24:34 +04:00
|
|
|
|
{
|
|
|
|
|
for (int j = i - 1; j >= position; j--)
|
|
|
|
|
{
|
|
|
|
|
_places[j + 1] = _places[j];
|
|
|
|
|
}
|
2022-11-06 23:39:21 +04:00
|
|
|
|
_places.Insert(position, boat);
|
2022-11-06 23:24:34 +04:00
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 22:13:04 +04:00
|
|
|
|
throw new BoatNotFoundException(_maxCount);
|
2022-11-06 23:24:34 +04:00
|
|
|
|
}
|
|
|
|
|
// TODO вставка по позиции
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление объекта из набора с конкретной позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public T Remove(int position)
|
|
|
|
|
{
|
|
|
|
|
// TODO проверка позиции
|
2022-12-03 22:13:04 +04:00
|
|
|
|
if (position < 0 || position >= Count) throw new IndexOutOfRangeException();
|
2022-11-06 23:24:34 +04:00
|
|
|
|
// TODO удаление объекта из массива, присовив элементу массива значение null
|
2022-11-06 23:39:21 +04:00
|
|
|
|
var result = _places[position];
|
|
|
|
|
_places.RemoveAt(position);
|
|
|
|
|
return result;
|
2022-11-06 23:24:34 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение объекта из набора по позиции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-11-06 23:39:21 +04:00
|
|
|
|
public T this[int position]
|
2022-11-06 23:24:34 +04:00
|
|
|
|
{
|
|
|
|
|
// TODO проверка позиции
|
2022-11-06 23:39:21 +04:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (position >= 0 && position < _maxCount && position < Count)
|
|
|
|
|
{
|
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (position > 0 || position < Count)
|
|
|
|
|
{
|
|
|
|
|
Insert(value, position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Проход по набору до первого пустого
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IEnumerable<T> GetBoats()
|
|
|
|
|
{
|
|
|
|
|
foreach (var boat in _places)
|
|
|
|
|
{
|
|
|
|
|
if (boat != null)
|
|
|
|
|
{
|
|
|
|
|
yield return boat;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-06 23:24:34 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|