diff --git a/Locomotive/Locomotive/FormLocomotive.cs b/Locomotive/Locomotive/FormLocomotive.cs index 7d782f5..2acdda2 100644 --- a/Locomotive/Locomotive/FormLocomotive.cs +++ b/Locomotive/Locomotive/FormLocomotive.cs @@ -3,7 +3,6 @@ namespace Locomotive public partial class FormLocomotive : Form { private DrawningLocomotive _locomotive; - public DrawningLocomotive SelectedLocomotive { get; private set; } public FormLocomotive() @@ -41,7 +40,6 @@ namespace Locomotive SetData(); Draw(); } - private void ButtonMove_Click(object sender, EventArgs e) { // diff --git a/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs b/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs index 71eaad3..8f1b26e 100644 --- a/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs +++ b/Locomotive/Locomotive/MapWithSetLocomotivesGeneric.cs @@ -55,13 +55,9 @@ namespace Locomotive 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); } @@ -80,11 +76,11 @@ namespace Locomotive 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); @@ -148,11 +144,11 @@ namespace Locomotive int curWidth = 0; int curHeight = 0; - for (int i = 0; i < _setLocomotives.Count; i++) + foreach (var locomotive in _setLocomotives.GetLocomotives()) { // установка позиции - _setLocomotives.Get(i)?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 15, _pictureWidth, _pictureHeight); - _setLocomotives.Get(i)?.DrawningObject(g); + locomotive?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 15, _pictureWidth, _pictureHeight); + locomotive?.DrawningObject(g); if (curWidth < width) curWidth++; else { diff --git a/Locomotive/Locomotive/SetLocomotivesGeneric.cs b/Locomotive/Locomotive/SetLocomotivesGeneric.cs index 5deb455..342fd46 100644 --- a/Locomotive/Locomotive/SetLocomotivesGeneric.cs +++ b/Locomotive/Locomotive/SetLocomotivesGeneric.cs @@ -9,14 +9,18 @@ namespace Locomotive internal class SetLocomotivesGeneric 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(); } /// Добавление объекта в набор public int Insert(T locomotive) @@ -26,53 +30,47 @@ namespace Locomotive /// Добавление объекта в набор на конкретную позицию public int Insert(T locomotive, int position) { - if (position >= _places.Length|| position < 0) return -1; + if (position >= _maxCount|| position < 0) return -1; + _places.Insert(position, locomotive); - if (_places[position] == null) - { - _places[position] = locomotive; - return position; - } - - int emptyEl = -1; - - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - emptyEl = i; - break; - } - } - - if (emptyEl == -1) - { - return -1; - } - - for (int i = emptyEl; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = locomotive; return position; } /// Удаление объекта из набора с конкретной позиции public T Remove(int position) { - if (position >= _places.Length || position < 0) return null; + if (position >= _maxCount || position < 0) return null; T result = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return result; } - /// Получение объекта из набора по позиции - public T Get(int position) + // Индексатор + public T this[int position] { - if (position >= _places.Length || position < 0) + get { - return null; + if (position >= _maxCount || position < 0) return null; + return _places[position]; + } + set + { + if (position >= _maxCount || position < 0) return; + Insert(value, position); + } + } + /// Проход по набору до первого пустого + public IEnumerable GetLocomotives() + { + foreach (var locomotive in _places) + { + if (locomotive != null) + { + yield return locomotive; + } + else + { + yield break; + } } - return _places[position]; } } }