From 6806a3c9ca49419a433a6671cda52fe351fffcbe Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 29 Nov 2022 01:44:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF1.=20=D1=81=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0=20?= =?UTF-8?q?=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 --- WinFormsApp1/MapWithSetTraktorGeneric.cs | 10 ++-- WinFormsApp1/SetTraktorGeneric.cs | 71 ++++++++++++++++-------- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/WinFormsApp1/MapWithSetTraktorGeneric.cs b/WinFormsApp1/MapWithSetTraktorGeneric.cs index 4fdf93d..328bfaf 100644 --- a/WinFormsApp1/MapWithSetTraktorGeneric.cs +++ b/WinFormsApp1/MapWithSetTraktorGeneric.cs @@ -52,7 +52,7 @@ namespace WinFormsApp1 Shaking(); for (int i = 0; i < _setTraktors.Count; i++) { - var bus = _setTraktors.Get(i); + var bus = _setTraktors[i]; if (bus != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, bus); @@ -75,11 +75,11 @@ namespace WinFormsApp1 int j = _setTraktors.Count - 1; for (int i = 0; i < _setTraktors.Count; i++) { - if (_setTraktors.Get(i) == null) + if (_setTraktors[i] == null) { for (; j > i; j--) { - var bus = _setTraktors.Get(j); + var bus = _setTraktors[j]; if (bus != null) { _setTraktors.Insert(bus, i); @@ -121,10 +121,10 @@ namespace WinFormsApp1 for (int i = _setTraktors.Count; i >= 0; i--) { - _setTraktors.Get(i)?.SetObject( + _setTraktors[i]?.SetObject( _pictureWidth - _placeSizeWidth * curWidth - 60, curHeight * _placeSizeHeight + 30, _pictureWidth, _pictureHeight); - _setTraktors.Get(i)?.DrawningObject(g); + _setTraktors[i]?.DrawningObject(g); if (curWidth < widthEl) curWidth++; diff --git a/WinFormsApp1/SetTraktorGeneric.cs b/WinFormsApp1/SetTraktorGeneric.cs index 7b5093e..657063a 100644 --- a/WinFormsApp1/SetTraktorGeneric.cs +++ b/WinFormsApp1/SetTraktorGeneric.cs @@ -9,13 +9,15 @@ namespace WinFormsApp1 internal class SetTraktorGeneric where T : class { - private readonly T[] _places; - public int Count => _places.Length; + private readonly List _places; + public int Count => _places.Count; + private readonly int _maxCount; private int TractorPlaces = 0; public SetTraktorGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); ; } public int Insert(T tractor) @@ -25,38 +27,61 @@ namespace WinFormsApp1 public int Insert(T tractor, int position) { - if (position < 0 || position >= _places.Length || TractorPlaces == _places.Length) + if (position < 0 && position > _maxCount) { return -1; } - TractorPlaces++; - while (_places[position] != null) + else { - 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.Insert(position, tractor); + return position; } - _places[position] = tractor; - return position; } public T Remove(int position) { - if (position < 0 || position >= _places.Length) return null; - T savedTractor = _places[position]; - _places[position] = null; - return savedTractor; + if (position < 0 || position >= _maxCount) + { + return null; + } + var result = _places[position]; + _places.RemoveAt(position); + return result; } - 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 && position < Count) + { + return _places[position]; + } + else + { + return null; + } + } + set + { + Insert(value, position); + } + } + + public IEnumerable GetBuses() + { + foreach (var traktor in _places) + { + if (traktor != null) + { + yield return traktor; + } + else + { + yield break; + } + } } } }