From 2049d2222b108a217916fbdade5d20d7dcbf5b97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sun, 9 Oct 2022 22:59:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D0=B4=D0=B8=D1=8F:=20=D0=BC=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B8=D0=B2=20=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D1=91=D0=BD=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 --- .../SetArtilleriesGeneric.cs | 75 ++++++++++--------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs b/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs index 35c69f5..1ef147d 100644 --- a/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs +++ b/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs @@ -9,12 +9,14 @@ namespace Artilleries internal class SetArtilleriesGeneric 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; public SetArtilleriesGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T artillery) @@ -24,39 +26,13 @@ namespace Artilleries public int Insert(T artillery, int position) { - if (position < 0 || position >= Count) + if (position < 0 || position >= Count || Count == _maxCount) { return -1; } - if (_places[position] == null) - { - _places[position] = artillery; - return position; - } + _places.Insert(position, artillery); - int firstNull = -1; - - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - firstNull = i; - break; - } - } - - if (firstNull == -1) - { - return -1; - } - - for (int i = firstNull; i > position; i--) - { - _places[i] = _places[i - 1]; - } - - _places[position] = artillery; return position; } @@ -69,18 +45,43 @@ namespace Artilleries var result = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return result; } - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= Count) + get { - return null; - } + if (position < 0 || position >= Count) + { + return null; + } - return _places[position]; + return _places[position]; + } + set + { + if (position >= 0 && position < Count) + { + Insert(value, position); + } + } + } + + public IEnumerable GetArtilleries() + { + foreach (var artillery in _places) + { + if (artillery != null) + { + yield return artillery; + } + else + { + yield break; + } + } } } }