123 lines
2.6 KiB
C#
123 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AirBomber.CollectionGenericObjects;
|
|
public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
|
|
where T : class
|
|
{
|
|
/// <summary>
|
|
/// Массив объектов, которые храним
|
|
/// </summary>
|
|
private T?[] _collection;
|
|
|
|
public int Count => _collection.Length;
|
|
|
|
public int MaxCount
|
|
{
|
|
get
|
|
{
|
|
return _collection.Length;
|
|
}
|
|
set
|
|
{
|
|
if (value > 0)
|
|
{
|
|
if (_collection.Length > 0)
|
|
{
|
|
Array.Resize(ref _collection, value);
|
|
}
|
|
else
|
|
{
|
|
_collection = new T?[value];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public CollectionType GetCollectionType => CollectionType.Massive;
|
|
|
|
/// <summary>
|
|
/// Конструктор
|
|
/// </summary>
|
|
public MassiveGenericObjects()
|
|
{
|
|
_collection = Array.Empty<T?>();
|
|
}
|
|
|
|
public T? Get(int position)
|
|
{
|
|
if (position >= 0 && position < Count)
|
|
{
|
|
return _collection[position];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public int Insert(T obj)
|
|
{
|
|
for (int i = 0; i < Count; i++)
|
|
{
|
|
if (_collection[i] == null)
|
|
{
|
|
_collection[i] = obj;
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public int Insert(T obj, int position)
|
|
{
|
|
if (position < 0 || position >= Count)
|
|
{
|
|
return -1;
|
|
}
|
|
if (_collection[position] == null)
|
|
{
|
|
_collection[position] = obj;
|
|
return position;
|
|
}
|
|
|
|
for (int i = position + 1; i < Count; i++)
|
|
{
|
|
if (_collection[i] == null)
|
|
{
|
|
_collection[i] = obj;
|
|
return i;
|
|
}
|
|
}
|
|
for (int i = position - 1; i >= 0; i--)
|
|
{
|
|
if (_collection[i] == null)
|
|
{
|
|
_collection[i] = obj;
|
|
return i;
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
public T Remove(int position)
|
|
{
|
|
if (position < 0 || position >= Count)
|
|
{
|
|
return null;
|
|
}
|
|
T obj = _collection[position];
|
|
_collection[position] = null;
|
|
return obj;
|
|
}
|
|
|
|
public IEnumerable<T?> GetItems()
|
|
{
|
|
for (int i = 0; i < _collection.Length; ++i)
|
|
{
|
|
yield return _collection[i];
|
|
}
|
|
}
|
|
} |