diff --git a/ProjectRoadTrain/ProjectRoadTrain/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectRoadTrain/ProjectRoadTrain/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..3cf72d6 --- /dev/null +++ b/ProjectRoadTrain/ProjectRoadTrain/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectRoadTrain.CollectionGenericObjects +{ + internal interface ICollectionGenericObjects + { + } +} diff --git a/ProjectRoadTrain/ProjectRoadTrain/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectRoadTrain/ProjectRoadTrain/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..a6559b1 --- /dev/null +++ b/ProjectRoadTrain/ProjectRoadTrain/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectRoadTrain.CollectionGenericObjects; + +public class MassiveGenericObjects : ICollectionGenericObjects + where T : class +{ + + private T?[] _collection; + + public int Count => _collection.Length; + + public int SetMaxCount + { + set + { + if (value > 0) + { + if (_collection.Length > 0) + { + Array.Resize(ref _collection, value); + } + else + { + _collection = new T?[value]; + } + } + } + } + + public MassiveGenericObjects() + { + _collection = Array.Empty(); + } + + public T? Get(int position) + { + if (position >= 0 && position < Count) + { + return _collection[position]; + } + + return null; + } + + public int Insert(T obj) + { + for (int i = 0; i < Count; i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + + return -1; + } + + public int Insert(T obj, int position) + { + + if (position < 0 || position >= Count) + { + return -1; + } + + if (_collection[position] != null) + { + bool pushed = false; + for (int index = position + 1; index < Count; index++) + { + if (_collection[index] == null) + { + position = index; + pushed = true; + break; + } + } + + if (!pushed) + { + for (int index = position - 1; index >= 0; index--) + { + if (_collection[index] == null) + { + position = index; + pushed = true; + break; + } + } + } + + if (!pushed) + { + return position; + } + } + + _collection[position] = obj; + return position; + } + + public T? Remove(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + + if (_collection[position] == null) return null; + + T? temp = _collection[position]; + _collection[position] = null; + return temp; + } +} \ No newline at end of file