diff --git a/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs b/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs
index e26b025..08f71f9 100644
--- a/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs
+++ b/Locomotives/Locomotives/MapWithSetLocomotivesGeneric.cs
@@ -82,13 +82,9 @@
public Bitmap ShowOnMap()
{
Shaking();
- for (int i = 0; i < _setLocomotives.Count; i++)
+ foreach (var locomotive in _setLocomotives.GetLocomotives())
{
- var locomotive = _setLocomotives.Get(i);
- if (locomotive != null)
- {
- return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
- }
+ return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
}
return new(_pictureWidth, _pictureHeight);
}
@@ -113,11 +109,11 @@
int j = _setLocomotives.Count - 1;
for (int i = 0; i < _setLocomotives.Count; i++)
{
- if (_setLocomotives.Get(i) == null)
+ if (_setLocomotives[i] == null)
{
for (; j > i; j--)
{
- var locomotive = _setLocomotives.Get(j);
+ var locomotive = _setLocomotives[j];
if (locomotive != null)
{
_setLocomotives.Insert(locomotive, i);
@@ -161,10 +157,11 @@
int LocomotivesInLine = _pictureWidth / _placeSizeWidth;
int CurrentVertPos = 20;
int CurrentHorPos = 0;
- for (int i = 0; i < _setLocomotives.Count; i++)
+ int CurrentLocomotiveNumber = 0;
+ foreach (var locomotive in _setLocomotives.GetLocomotives())
{
- _setLocomotives.Get(i)?.SetObject(CurrentHorPos, CurrentVertPos, _pictureWidth, _pictureHeight);
- _setLocomotives.Get(i)?.DrawningObject(g);
+ _setLocomotives[CurrentLocomotiveNumber]?.SetObject(CurrentHorPos, CurrentVertPos, _pictureWidth, _pictureHeight);
+ _setLocomotives[CurrentLocomotiveNumber]?.DrawningObject(g);
if (CurrentHorPos < LocomotivesInLine)
{
CurrentHorPos += _placeSizeWidth;
@@ -174,6 +171,7 @@
CurrentHorPos = 0;
CurrentVertPos += _placeSizeHeight;
}
+ CurrentLocomotiveNumber++;
}
}
}
diff --git a/Locomotives/Locomotives/SetLocomotivesGeneric.cs b/Locomotives/Locomotives/SetLocomotivesGeneric.cs
index 88cee91..96c7182 100644
--- a/Locomotives/Locomotives/SetLocomotivesGeneric.cs
+++ b/Locomotives/Locomotives/SetLocomotivesGeneric.cs
@@ -4,20 +4,22 @@
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;
public SetLocomotivesGeneric(int count)
{
- _places = new T[count];
+ _maxCount = count;
+ _places = new List();
}
///
/// Добавление объекта в набор
@@ -26,24 +28,22 @@
///
public int Insert(T locomotive)
{
- T testLocomotive = _places[0];
- for (int i = 0; i < Count; i++)
+ if (_places.Count == 0)
{
- if (_places[i] == null)
+ _places.Add(locomotive);
+ return 0;
+ }
+ if (Count < _maxCount)
+ {
+ for (int i = Count; i >= 1; i--)
{
- testLocomotive = _places[i];
+ _places.Insert(i, _places[i - 1]);
+ _places.RemoveAt(i - 1);
}
+ _places.Insert(0, locomotive);
+ return 0;
}
- if (testLocomotive != null)
- {
- return -1;
- }
- for (int i = Count - 2; i >= 0; i--)
- {
- _places[i + 1] = _places[i];
- }
- _places[0] = locomotive;
- return 0;
+ return -1;
}
///
/// Добавление объекта в набор на конкретную позицию
@@ -53,26 +53,16 @@
///
public int Insert(T locomotive, int position)
{
- if (position < 0 || position >= Count)
+ if (position < 0 || position > Count + 1)
{
return position;
}
- int ClosestNullElementIndex = position;
- while (_places[ClosestNullElementIndex] != null)
+ if (_places[position] == null)
{
- if (ClosestNullElementIndex == Count)
- {
- return position;
- }
- ClosestNullElementIndex++;
+ _places.Insert(position, locomotive);
+ return position;
}
- while (ClosestNullElementIndex != position)
- {
- _places[ClosestNullElementIndex] = _places[ClosestNullElementIndex - 1];
- ClosestNullElementIndex--;
- }
- _places[position] = locomotive;
- return position;
+ return -1;
}
///
/// Удаление объекта из набора с конкретной позиции
@@ -81,11 +71,11 @@
///
public int Remove(int position)
{
- if (_places[position] == null)
+ if (position > Count || _places[position] == null)
{
return -1;
}
- _places[position] = null;
+ _places.RemoveAt(position);
return position;
}
///
@@ -93,13 +83,41 @@
///
///
///
- public T Get(int position)
+ public T this[int position]
{
- if (_places[position] != null)
+ get
{
- return _places[position];
+ if (_places[position] != null)
+ {
+ return _places[position];
+ }
+ else
+ {
+ return null;
+ }
+ }
+ set
+ {
+ Insert(value, position);
+ }
+ }
+ ///
+ /// Проход по набору до первого пустого
+ ///
+ ///
+ public IEnumerable GetLocomotives()
+ {
+ foreach (var locomotive in _places)
+ {
+ if (locomotive != null)
+ {
+ yield return locomotive;
+ }
+ else
+ {
+ yield break;
+ }
}
- return null;
}
}
}