массив -> список

This commit is contained in:
Мельников Игорь 2022-10-26 04:58:32 +04:00
parent 2b0d5459ce
commit a03fc8bb8a
2 changed files with 67 additions and 51 deletions

View File

@ -82,13 +82,9 @@
public Bitmap ShowOnMap()
{
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);
}
@ -113,11 +109,11 @@
int j = _setLocomotives.Count - 1;
for (int i = 0; i < _setLocomotives.Count; i++)
{
if (_setLocomotives.Get(i) == null)
if (_setLocomotives[i] == null)
{
for (; j > i; j--)
{
var locomotive = _setLocomotives.Get(j);
var locomotive = _setLocomotives[j];
if (locomotive != null)
{
_setLocomotives.Insert(locomotive, i);
@ -161,10 +157,11 @@
int LocomotivesInLine = _pictureWidth / _placeSizeWidth;
int CurrentVertPos = 20;
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.Get(i)?.DrawningObject(g);
_setLocomotives[CurrentLocomotiveNumber]?.SetObject(CurrentHorPos, CurrentVertPos, _pictureWidth, _pictureHeight);
_setLocomotives[CurrentLocomotiveNumber]?.DrawningObject(g);
if (CurrentHorPos < LocomotivesInLine)
{
CurrentHorPos += _placeSizeWidth;
@ -174,6 +171,7 @@
CurrentHorPos = 0;
CurrentVertPos += _placeSizeHeight;
}
CurrentLocomotiveNumber++;
}
}
}

View File

@ -4,20 +4,22 @@
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T[] _places;
private readonly List<T> _places;
/// <summary>
/// Количество объектов в массиве
/// Количество объектов в списке
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
private readonly int _maxCount;
public SetLocomotivesGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
@ -26,24 +28,22 @@
/// <returns></returns>
public int Insert(T locomotive)
{
T testLocomotive = _places[0];
for (int i = 0; i < Count; i++)
if (_places.Count == 0)
{
if (_places[i] == null)
_places.Add(locomotive);
return 0;
}
if (Count < _maxCount)
{
for (int i = Count; i >= 1; i--)
{
testLocomotive = _places[i];
_places.Insert(i, _places[i - 1]);
_places.RemoveAt(i - 1);
}
_places.Insert(0, locomotive);
return 0;
}
if (testLocomotive != null)
{
return -1;
}
for (int i = Count - 2; i >= 0; i--)
{
_places[i + 1] = _places[i];
}
_places[0] = locomotive;
return 0;
return -1;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -53,26 +53,16 @@
/// <returns></returns>
public int Insert(T locomotive, int position)
{
if (position < 0 || position >= Count)
if (position < 0 || position > Count + 1)
{
return position;
}
int ClosestNullElementIndex = position;
while (_places[ClosestNullElementIndex] != null)
if (_places[position] == null)
{
if (ClosestNullElementIndex == Count)
{
return position;
}
ClosestNullElementIndex++;
_places.Insert(position, locomotive);
return position;
}
while (ClosestNullElementIndex != position)
{
_places[ClosestNullElementIndex] = _places[ClosestNullElementIndex - 1];
ClosestNullElementIndex--;
}
_places[position] = locomotive;
return position;
return -1;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -81,11 +71,11 @@
/// <returns></returns>
public int Remove(int position)
{
if (_places[position] == null)
if (position > Count || _places[position] == null)
{
return -1;
}
_places[position] = null;
_places.RemoveAt(position);
return position;
}
/// <summary>
@ -93,13 +83,41 @@
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
if (_places[position] != null)
get
{
return _places[position];
if (_places[position] != null)
{
return _places[position];
}
else
{
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;
}
}
return null;
}
}
}