массив -> список
This commit is contained in:
parent
2b0d5459ce
commit
a03fc8bb8a
@ -82,14 +82,10 @@
|
|||||||
public Bitmap ShowOnMap()
|
public Bitmap ShowOnMap()
|
||||||
{
|
{
|
||||||
Shaking();
|
Shaking();
|
||||||
for (int i = 0; i < _setLocomotives.Count; i++)
|
foreach (var locomotive in _setLocomotives.GetLocomotives())
|
||||||
{
|
|
||||||
var locomotive = _setLocomotives.Get(i);
|
|
||||||
if (locomotive != null)
|
|
||||||
{
|
{
|
||||||
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
|
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return new(_pictureWidth, _pictureHeight);
|
return new(_pictureWidth, _pictureHeight);
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -113,11 +109,11 @@
|
|||||||
int j = _setLocomotives.Count - 1;
|
int j = _setLocomotives.Count - 1;
|
||||||
for (int i = 0; i < _setLocomotives.Count; i++)
|
for (int i = 0; i < _setLocomotives.Count; i++)
|
||||||
{
|
{
|
||||||
if (_setLocomotives.Get(i) == null)
|
if (_setLocomotives[i] == null)
|
||||||
{
|
{
|
||||||
for (; j > i; j--)
|
for (; j > i; j--)
|
||||||
{
|
{
|
||||||
var locomotive = _setLocomotives.Get(j);
|
var locomotive = _setLocomotives[j];
|
||||||
if (locomotive != null)
|
if (locomotive != null)
|
||||||
{
|
{
|
||||||
_setLocomotives.Insert(locomotive, i);
|
_setLocomotives.Insert(locomotive, i);
|
||||||
@ -161,10 +157,11 @@
|
|||||||
int LocomotivesInLine = _pictureWidth / _placeSizeWidth;
|
int LocomotivesInLine = _pictureWidth / _placeSizeWidth;
|
||||||
int CurrentVertPos = 20;
|
int CurrentVertPos = 20;
|
||||||
int CurrentHorPos = 0;
|
int CurrentHorPos = 0;
|
||||||
for (int i = 0; i < _setLocomotives.Count; i++)
|
int CurrentLocomotiveNumber = 0;
|
||||||
|
foreach (var locomotive in _setLocomotives.GetLocomotives())
|
||||||
{
|
{
|
||||||
_setLocomotives.Get(i)?.SetObject(CurrentHorPos, CurrentVertPos, _pictureWidth, _pictureHeight);
|
_setLocomotives[CurrentLocomotiveNumber]?.SetObject(CurrentHorPos, CurrentVertPos, _pictureWidth, _pictureHeight);
|
||||||
_setLocomotives.Get(i)?.DrawningObject(g);
|
_setLocomotives[CurrentLocomotiveNumber]?.DrawningObject(g);
|
||||||
if (CurrentHorPos < LocomotivesInLine)
|
if (CurrentHorPos < LocomotivesInLine)
|
||||||
{
|
{
|
||||||
CurrentHorPos += _placeSizeWidth;
|
CurrentHorPos += _placeSizeWidth;
|
||||||
@ -174,6 +171,7 @@
|
|||||||
CurrentHorPos = 0;
|
CurrentHorPos = 0;
|
||||||
CurrentVertPos += _placeSizeHeight;
|
CurrentVertPos += _placeSizeHeight;
|
||||||
}
|
}
|
||||||
|
CurrentLocomotiveNumber++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,20 +4,22 @@
|
|||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Массив объектов, которые храним
|
/// Список объектов, которые храним
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly T[] _places;
|
private readonly List<T> _places;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Количество объектов в массиве
|
/// Количество объектов в списке
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Count => _places.Length;
|
public int Count => _places.Count;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="count"></param>
|
/// <param name="count"></param>
|
||||||
|
private readonly int _maxCount;
|
||||||
public SetLocomotivesGeneric(int count)
|
public SetLocomotivesGeneric(int count)
|
||||||
{
|
{
|
||||||
_places = new T[count];
|
_maxCount = count;
|
||||||
|
_places = new List<T>();
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление объекта в набор
|
/// Добавление объекта в набор
|
||||||
@ -26,25 +28,23 @@
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public int Insert(T locomotive)
|
public int Insert(T locomotive)
|
||||||
{
|
{
|
||||||
T testLocomotive = _places[0];
|
if (_places.Count == 0)
|
||||||
for (int i = 0; i < Count; i++)
|
|
||||||
{
|
{
|
||||||
if (_places[i] == null)
|
_places.Add(locomotive);
|
||||||
{
|
|
||||||
testLocomotive = _places[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (testLocomotive != null)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
for (int i = Count - 2; i >= 0; i--)
|
|
||||||
{
|
|
||||||
_places[i + 1] = _places[i];
|
|
||||||
}
|
|
||||||
_places[0] = locomotive;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
if (Count < _maxCount)
|
||||||
|
{
|
||||||
|
for (int i = Count; i >= 1; i--)
|
||||||
|
{
|
||||||
|
_places.Insert(i, _places[i - 1]);
|
||||||
|
_places.RemoveAt(i - 1);
|
||||||
|
}
|
||||||
|
_places.Insert(0, locomotive);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление объекта в набор на конкретную позицию
|
/// Добавление объекта в набор на конкретную позицию
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -53,26 +53,16 @@
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public int Insert(T locomotive, int position)
|
public int Insert(T locomotive, int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= Count)
|
if (position < 0 || position > Count + 1)
|
||||||
{
|
{
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
int ClosestNullElementIndex = position;
|
if (_places[position] == null)
|
||||||
while (_places[ClosestNullElementIndex] != null)
|
|
||||||
{
|
|
||||||
if (ClosestNullElementIndex == Count)
|
|
||||||
{
|
{
|
||||||
|
_places.Insert(position, locomotive);
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
ClosestNullElementIndex++;
|
return -1;
|
||||||
}
|
|
||||||
while (ClosestNullElementIndex != position)
|
|
||||||
{
|
|
||||||
_places[ClosestNullElementIndex] = _places[ClosestNullElementIndex - 1];
|
|
||||||
ClosestNullElementIndex--;
|
|
||||||
}
|
|
||||||
_places[position] = locomotive;
|
|
||||||
return position;
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Удаление объекта из набора с конкретной позиции
|
/// Удаление объекта из набора с конкретной позиции
|
||||||
@ -81,11 +71,11 @@
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public int Remove(int position)
|
public int Remove(int position)
|
||||||
{
|
{
|
||||||
if (_places[position] == null)
|
if (position > Count || _places[position] == null)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
_places[position] = null;
|
_places.RemoveAt(position);
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -93,13 +83,41 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="position"></param>
|
/// <param name="position"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public T Get(int position)
|
public T this[int position]
|
||||||
|
{
|
||||||
|
get
|
||||||
{
|
{
|
||||||
if (_places[position] != null)
|
if (_places[position] != null)
|
||||||
{
|
{
|
||||||
return _places[position];
|
return _places[position];
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
Insert(value, position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Проход по набору до первого пустого
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public IEnumerable<T> GetLocomotives()
|
||||||
|
{
|
||||||
|
foreach (var locomotive in _places)
|
||||||
|
{
|
||||||
|
if (locomotive != null)
|
||||||
|
{
|
||||||
|
yield return locomotive;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user