Начало

This commit is contained in:
Kirill 2023-12-27 08:38:38 +04:00
parent 55bb9dd042
commit a0a018477f
5 changed files with 140 additions and 3 deletions

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTractor.DrawningObjects;
using ProjectTractor.Entities;
using System.Diagnostics.CodeAnalysis;
namespace ProjectTractor.Generics
{
internal class DrawiningTractorEqutables : IEqualityComparer<DrawningTractor?>
{
public bool Equals(DrawningTractor? x, DrawningTractor? y)
{
if (x == null || x.EntityTractor == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityTractor == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return false;
}
if (x.EntityTractor.Speed != y.EntityTractor.Speed)
{
return false;
}
if (x.EntityTractor.Weight != y.EntityTractor.Weight)
{
return false;
}
if (x.EntityTractor.BodyColor != y.EntityTractor.BodyColor)
{
return false;
}
if (x is DrawningBulldoser && y is DrawningBulldoser)
{
// TODO доделать логику сравнения дополнительных параметров
}
return true;
}
public int GetHashCode([DisallowNull] DrawningTractor obj)
{
return obj.GetHashCode();
}
}
}

View File

@ -32,15 +32,21 @@ namespace ProjectTractor.Generics
_places = new List<T?>(count);
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T?> comparer) =>
_places.Sort(comparer);
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="tractor">Добавляемый трактор</param>
/// <returns></returns>
public bool Insert(T tractor)
public bool Insert(T tractor, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
Insert(tractor, 0);
Insert(tractor, 0, equal);
return true;
}
/// <summary>
@ -49,12 +55,17 @@ namespace ProjectTractor.Generics
/// <param name="tractor">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public void Insert(T tractor, int position)
public void Insert(T tractor, int position, IEqualityComparer<T>? equal = null)
{
if (_places.Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!(position >= 0 && position <= Count))
throw new Exception("Неверная позиция для вставки");
if (equal != null)
{
if (_places.Contains(tractor, equal))
throw new ArgumentException(nameof(tractor));
}
_places.Insert(position, tractor);
}
/// <summary>

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTractor.DrawningObjects;
namespace ProjectTractor.Generics
{
internal class TractorCompareByColor : IComparer<DrawningTractor?>
{
public int Compare(DrawningTractor? x, DrawningTractor? y)
{
if (x == null || x.EntityTractor == null)
throw new ArgumentNullException(nameof(x));
if (y == null || y.EntityTractor == null)
throw new ArgumentNullException(nameof(y));
if (x.EntityTractor.BodyColor.Name != y.EntityTractor.BodyColor.Name)
{
return x.EntityTractor.BodyColor.Name.CompareTo(y.EntityTractor.BodyColor.Name);
}
var speedCompare = x.EntityTractor.Speed.CompareTo(y.EntityTractor.Speed);
if (speedCompare != 0)
return speedCompare;
return x.EntityTractor.Weight.CompareTo(y.EntityTractor.Weight);
}
}
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTractor.DrawningObjects;
namespace ProjectTractor.Generics
{
internal class TractorCompareByType : IComparer<DrawningTractor?>
{
public int Compare(DrawningTractor? x, DrawningTractor? y)
{
if (x == null || x.EntityTractor == null)
{
throw new ArgumentNullException(nameof(x));
}
if (y == null || y.EntityTractor == null)
{
throw new ArgumentNullException(nameof(y));
}
if (x.GetType().Name != y.GetType().Name)
{
return x.GetType().Name.CompareTo(y.GetType().Name);
}
var speedCompare =
x.EntityTractor.Speed.CompareTo(y.EntityTractor.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return x.EntityTractor.Weight.CompareTo(y.EntityTractor.Weight);
}
}
}

View File

@ -55,6 +55,12 @@ namespace ProjectTractor.Generics
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T?> comparer) =>
_collection.SortSet(comparer);
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>