diff --git a/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs b/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs index ddcf57b..1ded118 100644 --- a/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs +++ b/ProjectPlane/ProjectPlane/MapWithSetPlanesGeneric.cs @@ -89,14 +89,9 @@ namespace ProjectPlane public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setPlanes.Count; i++) - { - var plane = _setPlanes.Get(i); - if (plane != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, plane); - } - } + foreach (var plane in _setPlanes.GetPlanes()) + return _map.CreateMap(_pictureWidth, _pictureHeight, plane); + return new(_pictureWidth, _pictureHeight); } /// @@ -120,11 +115,11 @@ namespace ProjectPlane int j = _setPlanes.Count - 1; for (int i = 0; i < _setPlanes.Count; i++) { - if (_setPlanes.Get(i) == null) + if (_setPlanes[i] == null) { for (; j > i; j--) { - var plane = _setPlanes.Get(j); + var plane = _setPlanes[j]; if (plane != null) { _setPlanes.Insert(plane, i); @@ -169,10 +164,10 @@ namespace ProjectPlane int width = _pictureWidth / _placeSizeWidth - 1; int height = _pictureHeight / _placeSizeHeight - 1; - for (int i = 0; i < _setPlanes.Count; i++) + foreach (var plane in _setPlanes.GetPlanes()) { - _setPlanes.Get(i)?.SetObject((width) * _placeSizeWidth + 10, (height) * _placeSizeHeight + 15, _pictureWidth, _pictureHeight); - _setPlanes.Get(i)?.DrawingObject(g); + plane?.SetObject((width) * _placeSizeWidth + 10, (height) * _placeSizeHeight + 15, _pictureWidth, _pictureHeight); + plane?.DrawingObject(g); if (width <= 0) { diff --git a/ProjectPlane/ProjectPlane/SetPlanesGeneric.cs b/ProjectPlane/ProjectPlane/SetPlanesGeneric.cs index e84b88a..8396f73 100644 --- a/ProjectPlane/ProjectPlane/SetPlanesGeneric.cs +++ b/ProjectPlane/ProjectPlane/SetPlanesGeneric.cs @@ -9,20 +9,22 @@ namespace ProjectPlane internal class SetPlanesGeneric 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 SetPlanesGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -43,33 +45,8 @@ namespace ProjectPlane { bool isNull = false; int nullElem = 0; - if (position < 0 || position >= Count) - { - return -1; - } - if (_places[position] == null) - { - _places[position] = plane; - return position; - } - for (int i = position + 1; i < Count; i ++) - { - if (_places[i] == null) - { - isNull = true; - nullElem = i; - break; - } - } - if (!isNull) - { - return -1; - } - for (int i = nullElem; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = plane; + if (position < 0 || position >= _maxCount) return -1; + _places.Insert(position, plane); return position; } /// @@ -79,12 +56,12 @@ namespace ProjectPlane /// public T Remove(int position) { - if (position < 0 || position >= Count) + if (position < 0 || position >= _maxCount) { return null; - } + } T temp = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return temp; } /// @@ -92,13 +69,40 @@ namespace ProjectPlane /// /// /// - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= Count) + get { - return null; + if (position < 0 || position >= _maxCount) + return null; + + return _places[position]; + } + set + { + if (position < 0 || position >= _maxCount) + return; + + Insert(value, position); + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetPlanes() + { + foreach (var plane in _places) + { + if (plane != null) + { + yield return plane; + } + else + { + yield break; + } } - return _places[position]; } } }