Files
PIbd-22_Safiulova_K.N._Cata…/base/Catamaran/Catamaran/SetGeneric.cs
2023-12-24 04:33:08 +04:00

128 lines
4.6 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 Catamaran.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.Generics
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetGeneric<T>
where T : class
{
/// <summary>
/// Список объектов, которые храним
/// </summary>
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в списке
/// </summary>
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_maxCount = count;
_places = new List<T?>(_maxCount);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый катамаран</param>
/// <returns></returns>
public bool Insert(T catamaran, IEqualityComparer<T?>? equal = null)
{
return Insert(catamaran, 0, equal);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="catamaran">Добавляемая лодка</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T catamaran, int position, IEqualityComparer<T?>? equal = null)
{
if (position < 0 || position >= _maxCount)
throw new CatamaranNotFoundException(position);
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
if (equal != null && _places.Contains(catamaran, equal))
throw new ArgumentException("Данный объект уже есть в коллекции");
_places.Insert(0, catamaran);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
// TODO проверка позиции
// TODO удаление объекта из массива, присвоив элементу массива значение null
if (position >= Count || position < 0)
{
throw new CatamaranNotFoundException("Invalid operation");
}
if (_places[position] == null)
{
throw new CatamaranNotFoundException(position);
}
_places.RemoveAt(position);
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
{
get
{
// TODO проверка позиции
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
set
{
// TODO проверка позиции
// TODO проверка свободных мест в списке
// TODO вставка в список по позиции
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
return;
_places.Insert(position, value);
return;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetCatamarans(int? maxCatamarans = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxCatamarans.HasValue && i == maxCatamarans.Value)
{
yield break;
}
}
}
}
}