diff --git a/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs
index 1ad5045..5930d48 100644
--- a/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs
+++ b/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs
@@ -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)
diff --git a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs
index ae130b0..870b6db 100644
--- a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs
+++ b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs
@@ -10,13 +10,14 @@ namespace DoubleDeckerBus
where T : class
{
///
- /// Массив объектов, которые храним
+ /// Список объектов, которые храним
///
- private readonly T[] _places;
+ private readonly List _places;
///
- /// Количество объектов в массиве
+ /// Количество объектов в списке
///
- public int Count => _places.Length;
+ public int Count => _places.Count;
+ private readonly int _maxCount;
private int BusyPlaces = 0;
///
/// Конструктор
@@ -24,7 +25,8 @@ namespace DoubleDeckerBus
///
public SetBusesGeneric(int count)
{
- _places = new T[count];
+ _maxCount = count;
+ _places = new List();
}
///
/// Добавление объекта в набор
@@ -43,21 +45,10 @@ namespace DoubleDeckerBus
///
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;
}
///
@@ -67,9 +58,9 @@ namespace DoubleDeckerBus
///
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;
}
///
@@ -77,10 +68,35 @@ namespace DoubleDeckerBus
///
///
///
- 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);
+ }
+ }
+ ///
+ /// Проход по набору до первого пустого
+ ///
+ ///
+ public IEnumerable GetBuses()
+ {
+ foreach (var bus in _places)
+ {
+ if (bus != null)
+ {
+ yield return bus;
+ }
+ else
+ {
+ yield break;
+ }
+ }
}
}