Этап1. смена массива на список

This commit is contained in:
Daniel 2022-11-29 01:44:43 +04:00
parent 19b93bd2ee
commit 6806a3c9ca
2 changed files with 53 additions and 28 deletions

View File

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

View File

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