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

This commit is contained in:
Марат Заргаров 2022-11-28 00:14:42 +04:00
parent fef1c1fb67
commit 45e642d983
2 changed files with 46 additions and 34 deletions

View File

@ -88,13 +88,9 @@ namespace DoubleDeckerBus
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setBuses.Count; i++)
foreach (var bus in _setBuses.GetBuses())
{
var bus = _setBuses.Get(i);
if (bus != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
}
return new(_pictureWidth, _pictureHeight);
}
@ -119,11 +115,11 @@ namespace DoubleDeckerBus
int j = _setBuses.Count - 1;
for (int i = 0; i < _setBuses.Count; i++)
{
if (_setBuses.Get(i) == null)
if (_setBuses[i] == null)
{
for (; j > i; j--)
{
var bus = _setBuses.Get(j);
var bus = _setBuses[i];
if (bus != null)
{
_setBuses.Insert(bus, i);
@ -164,10 +160,10 @@ namespace DoubleDeckerBus
int xPosition = 10;
int yPosition = _pictureHeight / _placeSizeHeight - 1;
for (int i = 0; i < _setBuses.Count; i++)
foreach(var bus in _setBuses.GetBuses())
{
_setBuses.Get(i)?.SetObject(xPosition, yPosition, _pictureWidth, _pictureHeight);
_setBuses.Get(i)?.DrawningObject(g);
bus.SetObject(xPosition, yPosition, _pictureWidth, _pictureHeight);
bus.DrawningObject(g);
xPosition += _placeSizeWidth;
if (xPosition + _placeSizeWidth > _pictureWidth)

View File

@ -10,13 +10,14 @@ namespace DoubleDeckerBus
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T[] _places;
private readonly List<T> _places;
/// <summary>
/// Количество объектов в массиве
/// Количество объектов в списке
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
private readonly int _maxCount;
private int BusyPlaces = 0;
/// <summary>
/// Конструктор
@ -24,7 +25,8 @@ namespace DoubleDeckerBus
/// <param name="count"></param>
public SetBusesGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
@ -43,21 +45,10 @@ namespace DoubleDeckerBus
/// <returns></returns>
public int Insert(T bus, int position)
{
if (position < 0 || position >= _places.Length || BusyPlaces == _places.Length) return -1;
if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount) return -1;
BusyPlaces++;
while (_places[position] != null)
{
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[position] = bus;
_places.Insert(position, bus);
return position;
}
/// <summary>
@ -67,9 +58,9 @@ namespace DoubleDeckerBus
/// <returns></returns>
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;
_places.RemoveAt(position);
return savedBus;
}
/// <summary>
@ -77,10 +68,35 @@ namespace DoubleDeckerBus
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
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) return null;
return _places[position];
}
set
{
Insert(value, position);
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetBuses()
{
foreach (var bus in _places)
{
if (bus != null)
{
yield return bus;
}
else
{
yield break;
}
}
}
}