From 1fce4712deca9e041b017a4ecc5aa72e4a205244 Mon Sep 17 00:00:00 2001 From: Arklightning Date: Fri, 28 Oct 2022 16:30:59 +0400 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BC=D0=B5=D0=BD=D0=B0=20=D0=BC?= =?UTF-8?q?=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0=20=D0=BD=D0=B0=20=D1=81?= =?UTF-8?q?=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 --- .../Trolleybus/MapWithSetTrolleybusGeneric.cs | 10 +-- Trolleybus/Trolleybus/SetTrolleybusGeneric.cs | 85 ++++++++++--------- Trolleybus/Trolleybus/Trolleybus.csproj | 2 +- 3 files changed, 51 insertions(+), 46 deletions(-) diff --git a/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs b/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs index 361e541..80c8143 100644 --- a/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs +++ b/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs @@ -91,7 +91,7 @@ namespace Trolleybus Shaking(); for (int i = 0; i < _setTrolleybus.Count; i++) { - var car = _setTrolleybus.Get(i); + var car = _setTrolleybus[i]; if (car != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, car); @@ -120,11 +120,11 @@ namespace Trolleybus int j = _setTrolleybus.Count - 1; for (int i = 0; i < _setTrolleybus.Count; i++) { - if (_setTrolleybus.Get(i) == null) + if (_setTrolleybus[j] == null) { for (; j > i; j--) { - var car = _setTrolleybus.Get(j); + var car = _setTrolleybus[j]; if (car != null) { _setTrolleybus.Insert(car, i); @@ -173,9 +173,9 @@ namespace Trolleybus yForLocomotive += _placeSizeHeight; countInRow = 0; } - if (_setTrolleybus.Get(i) != null) + if (_setTrolleybus[i] != null) { - T locomotive = _setTrolleybus.Get(i); + T locomotive = _setTrolleybus[i]; locomotive.SetObject(xForLocomotive, yForLocomotive, _pictureWidth, _pictureHeight); locomotive.DrawningObject(g); } diff --git a/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs b/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs index 49eb79c..a9d20be 100644 --- a/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs +++ b/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs @@ -11,13 +11,20 @@ namespace Trolleybus 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 SetTrolleybusGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); + } + private bool CorrectPos(int pos) + { + return 0 <= pos && pos < _maxCount; } // Добавление объекта в набор public int Insert(T trolleybus) @@ -29,53 +36,51 @@ namespace Trolleybus public int Insert(T trolleybus, int position) { // проверка позиции - if (position >= _places.Length || position < 0) + if (!CorrectPos(position)) + { return -1; - //проверка, что элемент массива по этой позиции пустой, если нет, то - if (_places[position] == null) - { - _places[position] = trolleybus; - return position; } - //проверка, что после вставляемого элемента в массиве есть пустой элемент - int findEmptyPos = -1; - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - findEmptyPos = i; - break; - } - } - if (findEmptyPos < 0) return -1; - - //сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - for (int i = findEmptyPos; i > position; i--) - { - _places[i] = _places[i - 1]; - } // вставка по позиции - _places[position] = trolleybus; + _places.Insert(position, trolleybus); return position; } // Удаление объекта из набора с конкретной позиции public T Remove(int position) - { - // проверка позиции - if (position >= _places.Length || position < 0) return null; - // удаление объекта из массива, присовив элементу массива значение null - T temp = _places[position]; - _places[position] = null; - return temp; - } + { + // проверка позиции + if (!CorrectPos(position)) + return null; + // удаление объекта из массива, присовив элементу массива значение null + T temp = _places[position]; + _places.RemoveAt(position); + return temp; + } // Получение объекта из набора по позиции - public T Get(int position) + public T this[int position] { - // проверка позиции - if (position >= _places.Length || position < 0) - return null; - return _places[position]; + get + { + return CorrectPos(position) && position < Count ? _places[position] : null; + } + set + { + Insert(value, position); + } + } + public IEnumerable GetTractors() + { + foreach (var tractor in _places) + { + if (tractor != null) + { + yield return tractor; + } + else + { + yield break; + } + } } } } \ No newline at end of file diff --git a/Trolleybus/Trolleybus/Trolleybus.csproj b/Trolleybus/Trolleybus/Trolleybus.csproj index ddd44f9..cc50b0e 100644 --- a/Trolleybus/Trolleybus/Trolleybus.csproj +++ b/Trolleybus/Trolleybus/Trolleybus.csproj @@ -73,7 +73,7 @@ FormMapWithSetTrolleybus.cs - +