93 lines
2.2 KiB
C#
93 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace WinFormsApp1
|
|
{
|
|
internal class SetTraktorGeneric<T>
|
|
where T : class
|
|
{
|
|
private readonly List<T> _places;
|
|
public int Count => _places.Count;
|
|
private readonly int _maxCount;
|
|
private int TractorPlaces = 0;
|
|
|
|
public SetTraktorGeneric(int count)
|
|
{
|
|
_maxCount = count;
|
|
_places = new List<T>(); ;
|
|
}
|
|
|
|
public int Insert(T tractor)
|
|
{
|
|
return Insert(tractor, 0);
|
|
}
|
|
|
|
public int Insert(T tractor, int position)
|
|
{
|
|
if (position > _maxCount && position < 0)
|
|
{
|
|
return -1;
|
|
}
|
|
if (_places.Contains(tractor))
|
|
{
|
|
throw new ArgumentException($"Объект {tractor} уже есть в наборе");
|
|
}
|
|
if (Count == _maxCount)
|
|
{
|
|
throw new StorageOverflowException(_maxCount);
|
|
}
|
|
_places.Insert(position, tractor);
|
|
return position;
|
|
}
|
|
|
|
public T Remove(int position)
|
|
{
|
|
if (position < 0 || position >= _maxCount)
|
|
{
|
|
return null;
|
|
}
|
|
var result = _places[position];
|
|
_places.RemoveAt(position);
|
|
return result;
|
|
}
|
|
|
|
|
|
public T this[int position]
|
|
{
|
|
get
|
|
{
|
|
if (position >= 0 && position < _maxCount && position < Count)
|
|
{
|
|
return _places[position];
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
set
|
|
{
|
|
Insert(value, position);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<T> GetTraktor()
|
|
{
|
|
foreach (var traktor in _places)
|
|
{
|
|
if (traktor != null)
|
|
{
|
|
yield return traktor;
|
|
}
|
|
else
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|