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

88 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Artilleries
{
internal class SetArtilleriesGeneric<T>
where T : class
{
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 (position < 0 || position >= Count || Count == _maxCount)
{
return -1;
}
_places.Insert(position, artillery);
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= Count)
{
return null;
}
var result = _places[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;
}
}
}
}
}