From 0e449ace7c5304aebbb0724c0d789585b70fe439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Tue, 28 Nov 2023 00:38:30 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=D0=B8?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D1=81=20=D0=BD=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=80=D0=BE=D0=BC=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/SetGeneric.cs | 106 ++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 Sailboat/Sailboat/SetGeneric.cs diff --git a/Sailboat/Sailboat/SetGeneric.cs b/Sailboat/Sailboat/SetGeneric.cs new file mode 100644 index 0000000..b1db75c --- /dev/null +++ b/Sailboat/Sailboat/SetGeneric.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемая лодка + /// + public int Insert(T boat) + { + return Insert(boat, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемая лодка + /// Позиция + /// + public int Insert(T boat, int position) + { + int nullIndex = -1, i; + + if (position < 0 || position >= Count) + { + return -1; + } + + for (i = position; i < Count; i++) + { + if (_places[i] == null) + { + nullIndex = i; + break; + } + } + + if (nullIndex < 0) + { + return -1; + } + + for (i = nullIndex; i > position; i--) + { + _places[i] = _places[i - 1]; + } + + _places[position] = boat; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < 0 || position >= Count) + { + return false; + } + + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + return _places[position]; + } + } +} \ No newline at end of file