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

This commit is contained in:
Дамир Нугаев 2022-10-30 20:35:08 +04:00
parent 9a45e3fab9
commit 01ad994227
2 changed files with 42 additions and 20 deletions

View File

@ -49,13 +49,11 @@ namespace Bus
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i< _setBus.Count; i++)
foreach (var bus in _setBus.GetBuses())
{
var bus = _setBus.Get(i);
if (bus != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
}
return new(_pictureWidth, _pictureHeight);
}
@ -74,11 +72,11 @@ namespace Bus
int j = _setBus.Count - 1;
for (int i = 0; i<_setBus.Count;i++)
{
if(_setBus.Get(i) == null)
if(_setBus[i] == null)
{
for(; j > i; j--)
{
var bus = _setBus.Get(j);
var bus = _setBus[j];
if (bus != null)
{
_setBus.Insert(bus, i);
@ -125,12 +123,12 @@ namespace Bus
int currentWidth = width - 1;
int currentHeight = 0;
for (int i = 0; i < _setBus.Count; i++)
foreach (var bus in _setBus.GetBuses())
{
_setBus.Get(i)?.SetObject(currentWidth * _placeSizeWidth,
bus?.SetObject(currentWidth * _placeSizeWidth,
currentHeight * _placeSizeHeight,
_pictureWidth, _pictureHeight);
_setBus.Get(i)?.DrawingObject(g);
bus?.DrawingObject(g);
if (currentWidth > 0)
currentWidth--;

View File

@ -9,13 +9,16 @@ namespace Bus
internal class SetDoubleDeckerBusGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
private readonly List<T> _places;
public int Count => _places.Count;
private int BusPlaces = 0;
private readonly int _maxCount;
public SetDoubleDeckerBusGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
public int Insert(T bus)
@ -25,14 +28,14 @@ namespace Bus
public int Insert(T bus, int position)
{
if (position < 0 || position >= _places.Length || BusPlaces == _places.Length)
if (position < 0 || position >= _maxCount || BusPlaces == _maxCount)
{
return -1;
}
BusPlaces++;
while (_places[position] != null)
{
for (int i = _places.Length - 1; i > 0; --i)
for (int i = _places.Count - 1; i > 0; --i)
{
if (_places[i] == null && _places[i - 1] != null)
{
@ -47,16 +50,37 @@ namespace Bus
public T Remove(int position)
{
if (position < 0 || position >= _places.Length) return null;
if (position < 0 || position >= _maxCount) return null;
T savedBus = _places[position];
_places[position] = null;
return savedBus;
}
public T Get(int position)
public T this[int position]
{
if (position < 0 || position >= _places.Length) return null;
return _places[position];
get
{
return _places[position];
}
set
{
// todo
}
}
public IEnumerable<T> GetBuses()
{
foreach (var bus in _places)
{
if (bus != null)
{
yield return bus;
}
else
{
yield break;
}
}
}
}
}