From 85d39d43aa91083fb451871e6683a779bc395950 Mon Sep 17 00:00:00 2001 From: tyxz0 Date: Thu, 25 Apr 2024 21:11:30 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D1=81=D0=BE=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ListGenericObjects.cs | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs new file mode 100644 index 0000000..8c4c3b5 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/CollectionGenericObjects/ListGenericObjects.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.CollectionGenericObjects; + +public class ListGenericObjects : ICollectionGenericObjects + where T : class +{ + private readonly List _collection; + + private int _maxCount; + public int Count => _collection.Count; + + public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + + public ListGenericObjects() + { + _collection = new(); + } + + public T? Get(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + return _collection[position]; + } + + public int Insert(T obj) + { + if (Count >= _maxCount) + { + return -1; + } + _collection.Add(obj); + return _collection.IndexOf(obj); + } + + public int Insert(T obj, int position) + { + if (Count >= _maxCount || position < 0 || position > _maxCount) + { + return -1; + } + + if (position > Count && position < _maxCount) + { + return Insert(obj); + } + + int copy_of_position = position - 1; + + while (position < Count) + { + if (_collection[position] == null) + { + _collection.Insert(position, obj); + return position; + } + position++; + } + + while (copy_of_position > 0) + { + if (_collection[copy_of_position] == null) + { + _collection.Insert(copy_of_position, obj); + return copy_of_position; + } + copy_of_position--; + } + + return -1; + } + + public T? Remove(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + + T? removed_object = Get(position); + _collection.RemoveAt(position); + return removed_object; + } +}