lab4
This commit is contained in:
parent
37c4b1810a
commit
e92c303460
84
WarmlyShip/WarmlyShip/SetGeneric.cs
Normal file
84
WarmlyShip/WarmlyShip/SetGeneric.cs
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WarmlyShip.Generics
|
||||||
|
{
|
||||||
|
internal class SetGeneric<T>
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
private readonly List<T?> _places;
|
||||||
|
public int Count => _places.Count;
|
||||||
|
private readonly int _maxCount;
|
||||||
|
public SetGeneric(int count)
|
||||||
|
{
|
||||||
|
_maxCount = count;
|
||||||
|
_places = new List<T>(count);
|
||||||
|
}
|
||||||
|
public bool Insert(T warmlyship)
|
||||||
|
{
|
||||||
|
if (_places.Count == _maxCount)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Insert(warmlyship, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Insert(T warmlyship, int position)
|
||||||
|
{
|
||||||
|
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_places.Insert(position, warmlyship);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove(int position)
|
||||||
|
{
|
||||||
|
if (position < 0 || position >= Count)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_places.RemoveAt(position);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T? this[int position]
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (position < 0 || position >= Count)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return _places[position];
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_places.Insert(position, value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public IEnumerable<T?> GetShips(int? maxShips = null)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _places.Count; ++i)
|
||||||
|
{
|
||||||
|
yield return _places[i];
|
||||||
|
if (maxShips.HasValue && i == maxShips.Value)
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user