From 45e642d983e29fa9ce17263d4667d7d8da4cd403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=B0=D1=82=20=D0=97=D0=B0=D1=80=D0=B3?= =?UTF-8?q?=D0=B0=D1=80=D0=BE=D0=B2?= Date: Mon, 28 Nov 2022 00:14:42 +0400 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=201.=20=D0=A1=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DoubleDeckerBus/MapWithSetBusesGeneric.cs | 18 +++--- .../DoubleDeckerBus/SetBusesGeneric.cs | 62 ++++++++++++------- 2 files changed, 46 insertions(+), 34 deletions(-) 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; + } + } } }