2022-11-14 21:25:33 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace WinFormsApp1
|
|
|
|
|
{
|
|
|
|
|
internal class SetTraktorGeneric<T>
|
2022-12-23 01:49:51 +04:00
|
|
|
|
where T : class, IEquatable<T>
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
2022-11-29 01:44:43 +04:00
|
|
|
|
private readonly List<T> _places;
|
|
|
|
|
public int Count => _places.Count;
|
|
|
|
|
private readonly int _maxCount;
|
2022-11-14 21:25:33 +04:00
|
|
|
|
private int TractorPlaces = 0;
|
|
|
|
|
|
|
|
|
|
public SetTraktorGeneric(int count)
|
|
|
|
|
{
|
2022-11-29 01:44:43 +04:00
|
|
|
|
_maxCount = count;
|
|
|
|
|
_places = new List<T>(); ;
|
2022-11-14 21:25:33 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T tractor)
|
|
|
|
|
{
|
|
|
|
|
return Insert(tractor, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Insert(T tractor, int position)
|
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
if (position > _maxCount && position < 0)
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-12-23 00:48:39 +04:00
|
|
|
|
if (_places.Contains(tractor))
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
2022-12-23 00:48:39 +04:00
|
|
|
|
throw new ArgumentException($"Объект {tractor} уже есть в наборе");
|
2022-11-14 21:25:33 +04:00
|
|
|
|
}
|
2022-12-23 00:48:39 +04:00
|
|
|
|
if (Count == _maxCount)
|
|
|
|
|
{
|
|
|
|
|
throw new StorageOverflowException(_maxCount);
|
|
|
|
|
}
|
|
|
|
|
_places.Insert(position, tractor);
|
|
|
|
|
return position;
|
2022-11-14 21:25:33 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T Remove(int position)
|
|
|
|
|
{
|
2022-11-29 01:44:43 +04:00
|
|
|
|
if (position < 0 || position >= _maxCount)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var result = _places[position];
|
|
|
|
|
_places.RemoveAt(position);
|
|
|
|
|
return result;
|
2022-11-14 21:25:33 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 01:44:43 +04:00
|
|
|
|
|
|
|
|
|
public T this[int position]
|
2022-11-14 21:25:33 +04:00
|
|
|
|
{
|
2022-11-29 01:44:43 +04:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (position >= 0 && position < _maxCount && position < Count)
|
|
|
|
|
{
|
|
|
|
|
return _places[position];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
Insert(value, position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 01:53:02 +04:00
|
|
|
|
public IEnumerable<T> GetTraktor()
|
2022-11-29 01:44:43 +04:00
|
|
|
|
{
|
|
|
|
|
foreach (var traktor in _places)
|
|
|
|
|
{
|
|
|
|
|
if (traktor != null)
|
|
|
|
|
{
|
|
|
|
|
yield return traktor;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-14 21:25:33 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|