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

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() public Bitmap ShowOnMap()
{ {
Shaking(); Shaking();
for (int i = 0; i < _setLocomotives.Count; i++) foreach (var locomotive in _setLocomotives.GetLocomotives())
{ {
var locomotive = _setLocomotives.Get(i); return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
if (locomotive != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
}
} }
return new(_pictureWidth, _pictureHeight); return new(_pictureWidth, _pictureHeight);
} }
@ -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++;
} }
} }
} }

View File

@ -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,24 +28,22 @@
/// <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);
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;
{
return -1;
}
for (int i = Count - 2; i >= 0; i--)
{
_places[i + 1] = _places[i];
}
_places[0] = locomotive;
return 0;
} }
/// <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++;
} }
while (ClosestNullElementIndex != position) return -1;
{
_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]
{ {
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;
} }
} }
} }