изменили 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]; return _collection[position];
} }
public int Insert(T obj) public int Insert(T obj, IEqualityComparer<T?>? comparer = null)
{ {
if (Count >= _maxCount) if (Count == _maxCount) throw new CollectionOverflowException();
throw new CollectionOverflowException(Count); if (comparer != null)
{
if (_collection.Contains(obj, comparer))
{
throw new ObjectAlreadyExistsException(obj);
}
}
_collection.Add(obj); _collection.Add(obj);
return Count; return Count;
} }
public int Insert(T obj, int position) public int Insert(T obj, int position, IEqualityComparer<T?>? comparer = null)
{ {
if (Count == _maxCount) if (Count == _maxCount) throw new CollectionOverflowException();
throw new CollectionOverflowException(Count); if (position >= Count || position < 0) throw new PositionOutOfCollectionException();
if (position < 0 || position > Count) if (comparer != null)
throw new PositionOutOfCollectionException(position); ; {
if (_collection.Contains(obj, comparer))
{
throw new ObjectAlreadyExistsException(obj);
}
}
_collection.Insert(position, obj); _collection.Insert(position, obj);
return position; return position;
} }

View File

@ -61,8 +61,18 @@ namespace ProjectLiner.CollectionGenericObjects
return _collection[position]; 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++) for (int i = 0; i < Count; i++)
{ {
if (_collection[i] == null) if (_collection[i] == null)
@ -71,20 +81,29 @@ namespace ProjectLiner.CollectionGenericObjects
return i; 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) if (position >= Count || position < 0) throw new PositionOutOfCollectionException();
throw new PositionOutOfCollectionException(position);
if (comparer != null)
{
foreach (T? i in _collection)
{
if (comparer.Equals(i, obj))
{
throw new ObjectAlreadyExistsException(i);
}
}
}
if (_collection[position] == null) if (_collection[position] == null)
{ {
_collection[position] = obj; _collection[position] = obj;
return position; return position;
} }
int temp = position + 1; int temp = position + 1;
while (temp < Count) while (temp < Count)
{ {
@ -93,21 +112,19 @@ namespace ProjectLiner.CollectionGenericObjects
_collection[temp] = obj; _collection[temp] = obj;
return temp; return temp;
} }
temp++; ++temp;
} }
temp = position - 1; temp = position - 1;
while (temp > 0) while (temp >= 0)
{ {
if (_collection[temp] == null) if (_collection[temp] == null)
{ {
_collection[temp] = obj; _collection[temp] = obj;
return temp; return temp;
} }
temp--; --temp;
} }
throw new CollectionOverflowException();
throw new CollectionOverflowException(Count);
} }
public T? Remove(int position) 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) { }
}