Этап 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,14 +88,10 @@ namespace DoubleDeckerBus
public Bitmap ShowOnMap() public Bitmap ShowOnMap()
{ {
Shaking(); 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); return new(_pictureWidth, _pictureHeight);
} }
/// <summary> /// <summary>
@ -119,11 +115,11 @@ namespace DoubleDeckerBus
int j = _setBuses.Count - 1; int j = _setBuses.Count - 1;
for (int i = 0; i < _setBuses.Count; i++) for (int i = 0; i < _setBuses.Count; i++)
{ {
if (_setBuses.Get(i) == null) if (_setBuses[i] == null)
{ {
for (; j > i; j--) for (; j > i; j--)
{ {
var bus = _setBuses.Get(j); var bus = _setBuses[i];
if (bus != null) if (bus != null)
{ {
_setBuses.Insert(bus, i); _setBuses.Insert(bus, i);
@ -164,10 +160,10 @@ namespace DoubleDeckerBus
int xPosition = 10; int xPosition = 10;
int yPosition = _pictureHeight / _placeSizeHeight - 1; 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); bus.SetObject(xPosition, yPosition, _pictureWidth, _pictureHeight);
_setBuses.Get(i)?.DrawningObject(g); bus.DrawningObject(g);
xPosition += _placeSizeWidth; xPosition += _placeSizeWidth;
if (xPosition + _placeSizeWidth > _pictureWidth) if (xPosition + _placeSizeWidth > _pictureWidth)

View File

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