Krasnikov D.D Lab work 4 #4

Closed
L1ontiev wants to merge 3 commits from LABA4 into LABA3
2 changed files with 53 additions and 28 deletions
Showing only changes of commit 6806a3c9ca - Show all commits

View File

@ -52,7 +52,7 @@ namespace WinFormsApp1
Shaking();
for (int i = 0; i < _setTraktors.Count; i++)
{
var bus = _setTraktors.Get(i);
var bus = _setTraktors[i];
if (bus != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
@ -75,11 +75,11 @@ namespace WinFormsApp1
int j = _setTraktors.Count - 1;
for (int i = 0; i < _setTraktors.Count; i++)
{
if (_setTraktors.Get(i) == null)
if (_setTraktors[i] == null)
{
for (; j > i; j--)
{
var bus = _setTraktors.Get(j);
var bus = _setTraktors[j];
if (bus != null)
{
_setTraktors.Insert(bus, i);
@ -121,10 +121,10 @@ namespace WinFormsApp1
for (int i = _setTraktors.Count; i >= 0; i--)
{
_setTraktors.Get(i)?.SetObject(
_setTraktors[i]?.SetObject(
_pictureWidth - _placeSizeWidth * curWidth - 60,
curHeight * _placeSizeHeight + 30, _pictureWidth, _pictureHeight);
_setTraktors.Get(i)?.DrawningObject(g);
_setTraktors[i]?.DrawningObject(g);
if (curWidth < widthEl)
curWidth++;

View File

@ -9,13 +9,15 @@ namespace WinFormsApp1
internal class SetTraktorGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
private readonly List<T> _places;
public int Count => _places.Count;
private readonly int _maxCount;
private int TractorPlaces = 0;
public SetTraktorGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>(); ;
}
public int Insert(T tractor)
@ -25,38 +27,61 @@ namespace WinFormsApp1
public int Insert(T tractor, int position)
{
if (position < 0 || position >= _places.Length || TractorPlaces == _places.Length)
if (position < 0 && position > _maxCount)
{
return -1;
}
TractorPlaces++;
while (_places[position] != null)
else
{
for (int i = _places.Length - 1; i > 0; --i)
{
if (_places[i] == null && _places[i - 1] != null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
_places.Insert(position, tractor);
return position;
}
_places[position] = tractor;
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= _places.Length) return null;
T savedTractor = _places[position];
_places[position] = null;
return savedTractor;
if (position < 0 || position >= _maxCount)
{
return null;
}
var result = _places[position];
_places.RemoveAt(position);
return result;
}
public T Get(int position)
public T this[int position]
{
if (position < 0 || position >= _places.Length) return null;
return _places[position];
get
{
if (position >= 0 && position < _maxCount && position < Count)
{
return _places[position];
}
else
{
return null;
}
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetBuses()
{
foreach (var traktor in _places)
{
if (traktor != null)
{
yield return traktor;
}
else
{
yield break;
}
}
}
}
}