From 859a9ffff60e41f42cc7a20cae0a35158319f8fb Mon Sep 17 00:00:00 2001 From: gettterot Date: Mon, 10 Jun 2024 05:44:42 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=D0=B8=20Insert=20d=20massive=20=D0=B8=20list,=20=D1=82=D0=B0?= =?UTF-8?q?=D0=BA=D0=B6=D0=B5=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B8=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=81=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ListGenericObjects.cs | 27 ++++++++---- .../MassiveGenericObjects.cs | 41 +++++++++++++------ .../ObjectAlreadyExistsException.cs | 22 ++++++++++ 3 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 ProjectLiner/ProjectLiner/Exceptions/ObjectAlreadyExistsException.cs diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs index d37225b..a9066aa 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs @@ -58,20 +58,31 @@ namespace ProjectLiner.CollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { - if (Count >= _maxCount) - throw new CollectionOverflowException(Count); + if (Count == _maxCount) throw new CollectionOverflowException(); + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectAlreadyExistsException(obj); + } + } _collection.Add(obj); return Count; } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { - if (Count == _maxCount) - throw new CollectionOverflowException(Count); - if (position < 0 || position > Count) - throw new PositionOutOfCollectionException(position); ; + if (Count == _maxCount) throw new CollectionOverflowException(); + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectAlreadyExistsException(obj); + } + } _collection.Insert(position, obj); return position; } diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs index 99f8a17..e8b40f5 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs @@ -61,8 +61,18 @@ namespace ProjectLiner.CollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + if (comparer != null) + { + foreach (T? i in _collection) + { + if (comparer.Equals(i, obj)) + { + throw new ObjectAlreadyExistsException(i); + } + } + } for (int i = 0; i < Count; i++) { if (_collection[i] == null) @@ -71,20 +81,29 @@ namespace ProjectLiner.CollectionGenericObjects return i; } } - throw new CollectionOverflowException(Count); + throw new CollectionOverflowException(); } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { - if (position >= Count || position < 0) - throw new PositionOutOfCollectionException(position); + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); + + if (comparer != null) + { + foreach (T? i in _collection) + { + if (comparer.Equals(i, obj)) + { + throw new ObjectAlreadyExistsException(i); + } + } + } if (_collection[position] == null) { _collection[position] = obj; return position; } - int temp = position + 1; while (temp < Count) { @@ -93,21 +112,19 @@ namespace ProjectLiner.CollectionGenericObjects _collection[temp] = obj; return temp; } - temp++; + ++temp; } - temp = position - 1; - while (temp > 0) + while (temp >= 0) { if (_collection[temp] == null) { _collection[temp] = obj; return temp; } - temp--; + --temp; } - - throw new CollectionOverflowException(Count); + throw new CollectionOverflowException(); } public T? Remove(int position) diff --git a/ProjectLiner/ProjectLiner/Exceptions/ObjectAlreadyExistsException.cs b/ProjectLiner/ProjectLiner/Exceptions/ObjectAlreadyExistsException.cs new file mode 100644 index 0000000..8cbb498 --- /dev/null +++ b/ProjectLiner/ProjectLiner/Exceptions/ObjectAlreadyExistsException.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLiner.Exceptions; + +/// +/// Класс, описывающий ошибку, что в коллекции уже есть такой элемент +/// +[Serializable] +public class ObjectAlreadyExistsException : ApplicationException +{ + public ObjectAlreadyExistsException(object i) : base("В коллекции уже есть такой элемент " + i) { } + public ObjectAlreadyExistsException() : base() { } + public ObjectAlreadyExistsException(string message) : base(message) { } + public ObjectAlreadyExistsException(string message, Exception exception) : base(message, exception) + { } + protected ObjectAlreadyExistsException(SerializationInfo info, StreamingContext context) : base(info, context) { } +}