diff --git a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs index f2ab9f9..d58ec9d 100644 --- a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs @@ -63,15 +63,10 @@ internal class MassiveGenericObjects : ICollectionGenericObjects public int Insert(T obj, IEqualityComparer? comparer = null) { - if (comparer != null) + int index = Array.IndexOf(_collection, null); + if (_collection.Contains(obj, comparer)) { - for (int i = 0; i < Count; i++) - { - if (comparer.Equals(_collection[i], obj)) - { - throw new CollectionInsertException(obj); - } - } + throw new CollectionInsertException(obj); } for (int i = 0; i < Count; i++) { @@ -84,53 +79,45 @@ internal class MassiveGenericObjects : ICollectionGenericObjects throw new CollectionOverflowException(Count); } - public int Insert(T obj, int position, IEqualityComparer? comparer = null) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { - if (position < 0 || position >= Count) + if (_collection.Contains(obj, comparer)) { + throw new CollectionInsertException(obj); + } + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(position); - } - if (comparer != null) - { - for (int i = 0; i < Count; i++) - { - if (comparer.Equals(_collection[i], obj)) - { - throw new CollectionInsertException(obj); - } - } - } - - if (position >= _collection.Length || position < 0) throw new PositionOutOfCollectionException(position); if (_collection[position] == null) { _collection[position] = obj; return position; } - int index = position + 1; - while (index < _collection.Length) + + int temp = position + 1; + while (temp < Count) { - if (_collection[index] == null) + if (_collection[temp] == null) { - _collection[index] = obj; - return index; + _collection[temp] = obj; + return temp; } - ++index; + temp++; } - index = position - 1; - while (index >= 0) + + temp = position - 1; + while (temp > 0) { - if (_collection[index] == null) + if (_collection[temp] == null) { - _collection[index] = obj; - return index; + _collection[temp] = obj; + return temp; } - --index; + temp--; } + throw new CollectionOverflowException(Count); } - public T? Remove(int position) { if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position); @@ -150,10 +137,11 @@ internal class MassiveGenericObjects : ICollectionGenericObjects public void CollectionSort(IComparer comparer) { - List value = new List(_collection); - value.Sort(comparer); - value.CopyTo(_collection, 0); + if (_collection?.Length > 0) + { + Array.Sort(_collection, comparer); + Array.Reverse(_collection); + } } - - } + diff --git a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs index cc1a24b..5fbb369 100644 --- a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/StorageCollection.cs @@ -211,8 +211,7 @@ public class StorageCollection /// /// /// - private static ICollectionGenericObjects? - CreateCollection(CollectionType collectionType) + private static ICollectionGenericObjects?CreateCollection(CollectionType collectionType) { return collectionType switch { diff --git a/TrolleybusProject/TrolleybusProject/Drawnings/DrawningBusCompareByColor.cs b/TrolleybusProject/TrolleybusProject/Drawnings/DrawningBusCompareByColor.cs index 81716a7..287782e 100644 --- a/TrolleybusProject/TrolleybusProject/Drawnings/DrawningBusCompareByColor.cs +++ b/TrolleybusProject/TrolleybusProject/Drawnings/DrawningBusCompareByColor.cs @@ -10,14 +10,18 @@ public class DrawningBusCompareByColor: IComparer { public int Compare(DrawningBus? x, DrawningBus? y) { - if (x == null || x.EntityBus == null) + if (x == null && y == null) { - return 1; + return 0; + } + if (x == null || x.EntityBus== null) + { + return -1; } if (y == null || y.EntityBus == null) { - return -1; + return 1; } var bodycolorCompare = x.EntityBus.BodyColor.Name.CompareTo(y.EntityBus.BodyColor.Name); if (bodycolorCompare != 0)