исправление массива

This commit is contained in:
Anastasia Yazykova 2024-06-10 04:53:32 +04:00
parent 2d339f7d20
commit 38b39ba55d
3 changed files with 37 additions and 46 deletions

View File

@ -63,15 +63,10 @@ internal class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
public int Insert(T obj, IEqualityComparer<T?>? comparer = null) public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{ {
if (comparer != null) int index = Array.IndexOf(_collection, null);
if (_collection.Contains(obj, comparer))
{ {
for (int i = 0; i < Count; i++) throw new CollectionInsertException(obj);
{
if (comparer.Equals(_collection[i], obj))
{
throw new CollectionInsertException(obj);
}
}
} }
for (int i = 0; i < Count; i++) for (int i = 0; i < Count; i++)
{ {
@ -84,53 +79,45 @@ internal class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
throw new CollectionOverflowException(Count); throw new CollectionOverflowException(Count);
} }
public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null) public int Insert(T obj, int position, IEqualityComparer<T?>? 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); 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) if (_collection[position] == null)
{ {
_collection[position] = obj; _collection[position] = obj;
return position; 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; _collection[temp] = obj;
return index; 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; _collection[temp] = obj;
return index; return temp;
} }
--index; temp--;
} }
throw new CollectionOverflowException(Count); throw new CollectionOverflowException(Count);
} }
public T? Remove(int position) public T? Remove(int position)
{ {
if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position); if (position < 0 || position >= Count) throw new PositionOutOfCollectionException(position);
@ -150,10 +137,11 @@ internal class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
public void CollectionSort(IComparer<T?> comparer) public void CollectionSort(IComparer<T?> comparer)
{ {
List<T?> value = new List<T?>(_collection); if (_collection?.Length > 0)
value.Sort(comparer); {
value.CopyTo(_collection, 0); Array.Sort(_collection, comparer);
Array.Reverse(_collection);
}
} }
} }

View File

@ -211,8 +211,7 @@ public class StorageCollection<T>
/// </summary> /// </summary>
/// <param name="collectionType"></param> /// <param name="collectionType"></param>
/// <returns></returns> /// <returns></returns>
private static ICollectionGenericObjects<T>? private static ICollectionGenericObjects<T>?CreateCollection(CollectionType collectionType)
CreateCollection(CollectionType collectionType)
{ {
return collectionType switch return collectionType switch
{ {

View File

@ -10,14 +10,18 @@ public class DrawningBusCompareByColor: IComparer<DrawningBus?>
{ {
public int Compare(DrawningBus? x, DrawningBus? y) 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) if (y == null || y.EntityBus == null)
{ {
return -1; return 1;
} }
var bodycolorCompare = x.EntityBus.BodyColor.Name.CompareTo(y.EntityBus.BodyColor.Name); var bodycolorCompare = x.EntityBus.BodyColor.Name.CompareTo(y.EntityBus.BodyColor.Name);
if (bodycolorCompare != 0) if (bodycolorCompare != 0)