изменили Insert d massive и list, также добавили новый класс ошибки

This commit is contained in:
gettterot 2024-06-10 05:44:42 +04:00
parent 50755f3d53
commit 859a9ffff6
3 changed files with 70 additions and 20 deletions

View File

@ -58,20 +58,31 @@ namespace ProjectLiner.CollectionGenericObjects
return _collection[position];
}
public int Insert(T obj)
public int Insert(T obj, IEqualityComparer<T?>? 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<T?>? 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;
}

View File

@ -61,8 +61,18 @@ namespace ProjectLiner.CollectionGenericObjects
return _collection[position];
}
public int Insert(T obj)
public int Insert(T obj, IEqualityComparer<T?>? 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<T?>? 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)

View File

@ -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;
/// <summary>
/// Класс, описывающий ошибку, что в коллекции уже есть такой элемент
/// </summary>
[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) { }
}