PIbd-23_Polevoy_S.V._SelfPr.../SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs

103 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Artilleries
{
internal class SetArtilleriesGeneric<T>
2022-11-21 19:58:14 +04:00
where T : class, IEquatable<T>
{
private readonly List<T> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetArtilleriesGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
}
public int Insert(T artillery)
{
return Insert(artillery, 0);
}
public int Insert(T artillery, int position)
{
if (Count == _maxCount)
{
throw new StorageOverflowException(_maxCount);
}
if (position < 0 || position > Count)
{
return -1;
}
2022-11-21 19:58:14 +04:00
if (_places.Contains(artillery))
{
return -1;
}
_places.Insert(position, artillery);
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= Count)
{
return null;
}
var result = _places[position];
if (result == null)
{
throw new ArtilleryNotFoundException(position);
}
_places.RemoveAt(position);
return result;
}
public T this[int position]
{
get
{
if (position < 0 || position >= Count)
{
return null;
}
return _places[position];
}
set
{
if (position >= 0 && position < Count)
{
Insert(value, position);
}
}
}
public IEnumerable<T> GetArtilleries()
{
foreach (var artillery in _places)
{
if (artillery != null)
{
yield return artillery;
}
else
{
yield break;
}
}
}
}
}