2022-09-30 16:33:17 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
2022-10-09 22:59:43 +04:00
|
|
|
|
private readonly List<T> _places;
|
|
|
|
|
public int Count => _places.Count;
|
|
|
|
|
private readonly int _maxCount;
|
2022-09-30 16:33:17 +04:00
|
|
|
|
|
|
|
|
|
public SetArtilleriesGeneric(int count)
|
|
|
|
|
{
|
2022-10-09 22:59:43 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T>();
|
2022-09-30 16:33:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 16:01:05 +04:00
|
|
|
|
public int Insert(T artillery)
|
2022-09-30 16:33:17 +04:00
|
|
|
|
{
|
2022-09-30 17:24:13 +04:00
|
|
|
|
return Insert(artillery, 0);
|
2022-09-30 16:33:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 16:01:05 +04:00
|
|
|
|
public int Insert(T artillery, int position)
|
2022-09-30 16:33:17 +04:00
|
|
|
|
{
|
2022-10-10 00:00:51 +04:00
|
|
|
|
if (position < 0 || position > Count || Count == _maxCount)
|
2022-09-30 17:24:13 +04:00
|
|
|
|
{
|
2022-10-03 16:01:05 +04:00
|
|
|
|
return -1;
|
2022-09-30 17:24:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-09 22:59:43 +04:00
|
|
|
|
_places.Insert(position, artillery);
|
2022-09-30 20:17:08 +04:00
|
|
|
|
|
2022-10-03 16:01:05 +04:00
|
|
|
|
return position;
|
2022-09-30 16:33:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 16:01:05 +04:00
|
|
|
|
public T Remove(int position)
|
2022-09-30 16:33:17 +04:00
|
|
|
|
{
|
2022-10-03 16:01:05 +04:00
|
|
|
|
if (position < 0 || position >= Count)
|
2022-09-30 17:24:13 +04:00
|
|
|
|
{
|
2022-10-03 16:01:05 +04:00
|
|
|
|
return null;
|
2022-09-30 17:24:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 16:01:05 +04:00
|
|
|
|
var result = _places[position];
|
|
|
|
|
|
2022-10-09 22:59:43 +04:00
|
|
|
|
_places.RemoveAt(position);
|
2022-10-03 16:01:05 +04:00
|
|
|
|
return result;
|
2022-09-30 16:33:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-09 22:59:43 +04:00
|
|
|
|
public T this[int position]
|
2022-09-30 16:33:17 +04:00
|
|
|
|
{
|
2022-10-09 22:59:43 +04:00
|
|
|
|
get
|
2022-09-30 17:24:13 +04:00
|
|
|
|
{
|
2022-10-09 22:59:43 +04:00
|
|
|
|
if (position < 0 || position >= Count)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (position >= 0 && position < Count)
|
|
|
|
|
{
|
|
|
|
|
Insert(value, position);
|
|
|
|
|
}
|
2022-09-30 17:24:13 +04:00
|
|
|
|
}
|
2022-10-09 22:59:43 +04:00
|
|
|
|
}
|
2022-09-30 17:24:13 +04:00
|
|
|
|
|
2022-10-09 22:59:43 +04:00
|
|
|
|
public IEnumerable<T> GetArtilleries()
|
|
|
|
|
{
|
|
|
|
|
foreach (var artillery in _places)
|
|
|
|
|
{
|
|
|
|
|
if (artillery != null)
|
|
|
|
|
{
|
|
|
|
|
yield return artillery;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-30 16:33:17 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|