113 lines
2.4 KiB
C#
113 lines
2.4 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, 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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SortSet(IComparer<T> comparer)
|
|
{
|
|
if (comparer == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_places.Sort(comparer);
|
|
}
|
|
}
|
|
}
|