Готовая 4 лабораторная

This commit is contained in:
Илья 2023-10-24 14:48:32 +04:00
parent ba9a4d66ce
commit a8b0acd1f8
6 changed files with 362 additions and 87 deletions

View File

@ -46,7 +46,7 @@
pictureBoxMonorail.Dock = DockStyle.Fill;
pictureBoxMonorail.Location = new Point(0, 0);
pictureBoxMonorail.Name = "pictureBoxMonorail";
pictureBoxMonorail.Size = new Size(884, 461);
pictureBoxMonorail.Size = new Size(884, 545);
pictureBoxMonorail.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxMonorail.TabIndex = 0;
pictureBoxMonorail.TabStop = false;
@ -54,7 +54,7 @@
// buttonCreateExtendedMonorail
//
buttonCreateExtendedMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateExtendedMonorail.Location = new Point(12, 403);
buttonCreateExtendedMonorail.Location = new Point(12, 487);
buttonCreateExtendedMonorail.Name = "buttonCreateExtendedMonorail";
buttonCreateExtendedMonorail.Size = new Size(140, 39);
buttonCreateExtendedMonorail.TabIndex = 1;
@ -67,7 +67,7 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(770, 419);
buttonLeft.Location = new Point(770, 503);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 2;
@ -79,7 +79,7 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(806, 419);
buttonDown.Location = new Point(806, 503);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 3;
@ -91,7 +91,7 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.arrowRight;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(842, 419);
buttonRight.Location = new Point(842, 503);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 4;
@ -103,7 +103,7 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(806, 383);
buttonUp.Location = new Point(806, 467);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 5;
@ -124,7 +124,7 @@
// buttonCreateMonorail
//
buttonCreateMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateMonorail.Location = new Point(168, 403);
buttonCreateMonorail.Location = new Point(168, 487);
buttonCreateMonorail.Name = "buttonCreateMonorail";
buttonCreateMonorail.Size = new Size(140, 39);
buttonCreateMonorail.TabIndex = 7;
@ -146,7 +146,7 @@
// buttonSelectMonorail
//
buttonSelectMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonSelectMonorail.Location = new Point(324, 403);
buttonSelectMonorail.Location = new Point(324, 487);
buttonSelectMonorail.Name = "buttonSelectMonorail";
buttonSelectMonorail.Size = new Size(140, 39);
buttonSelectMonorail.TabIndex = 9;
@ -158,7 +158,7 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
ClientSize = new Size(884, 545);
Controls.Add(buttonSelectMonorail);
Controls.Add(buttonStep);
Controls.Add(buttonCreateMonorail);

View File

@ -29,13 +29,20 @@
private void InitializeComponent()
{
panelTools = new Panel();
panelSets = new Panel();
textBoxNameSet = new TextBox();
buttonAddSet = new Button();
ButtonDelSet = new Button();
listBoxSets = new ListBox();
labelSetsName = new Label();
buttonRefreshCollection = new Button();
labelName = new Label();
labelToolsName = new Label();
maskedTextBoxNumber = new MaskedTextBox();
buttonRemoveMonorail = new Button();
buttonAddMonorail = new Button();
pictureBoxCollection = new PictureBox();
panelTools.SuspendLayout();
panelSets.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
@ -43,57 +50,131 @@
//
panelTools.Anchor = AnchorStyles.Top | AnchorStyles.Right;
panelTools.BorderStyle = BorderStyle.FixedSingle;
panelTools.Controls.Add(panelSets);
panelTools.Controls.Add(buttonRefreshCollection);
panelTools.Controls.Add(labelName);
panelTools.Controls.Add(labelToolsName);
panelTools.Controls.Add(maskedTextBoxNumber);
panelTools.Controls.Add(buttonRemoveMonorail);
panelTools.Controls.Add(buttonAddMonorail);
panelTools.Location = new Point(788, 10);
panelTools.Name = "panelTools";
panelTools.Size = new Size(186, 430);
panelTools.Size = new Size(187, 580);
panelTools.TabIndex = 0;
//
// panelSets
//
panelSets.Anchor = AnchorStyles.Top | AnchorStyles.Right;
panelSets.BorderStyle = BorderStyle.FixedSingle;
panelSets.Controls.Add(textBoxNameSet);
panelSets.Controls.Add(buttonAddSet);
panelSets.Controls.Add(ButtonDelSet);
panelSets.Controls.Add(listBoxSets);
panelSets.Controls.Add(labelSetsName);
panelSets.Location = new Point(11, 45);
panelSets.Name = "panelSets";
panelSets.Size = new Size(163, 292);
panelSets.TabIndex = 4;
//
// textBoxNameSet
//
textBoxNameSet.Location = new Point(7, 40);
textBoxNameSet.Name = "textBoxNameSet";
textBoxNameSet.Size = new Size(145, 23);
textBoxNameSet.TabIndex = 8;
//
// buttonAddSet
//
buttonAddSet.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonAddSet.BackColor = SystemColors.Window;
buttonAddSet.FlatAppearance.BorderColor = Color.Black;
buttonAddSet.FlatStyle = FlatStyle.Flat;
buttonAddSet.Location = new Point(7, 80);
buttonAddSet.Name = "buttonAddSet";
buttonAddSet.Size = new Size(145, 34);
buttonAddSet.TabIndex = 7;
buttonAddSet.Text = "Add set";
buttonAddSet.UseVisualStyleBackColor = false;
buttonAddSet.Click += buttonAddSet_Click;
//
// ButtonDelSet
//
ButtonDelSet.Anchor = AnchorStyles.Top | AnchorStyles.Right;
ButtonDelSet.BackColor = SystemColors.Window;
ButtonDelSet.FlatAppearance.BorderColor = Color.Black;
ButtonDelSet.FlatStyle = FlatStyle.Flat;
ButtonDelSet.Location = new Point(7, 244);
ButtonDelSet.Name = "ButtonDelSet";
ButtonDelSet.Size = new Size(145, 34);
ButtonDelSet.TabIndex = 5;
ButtonDelSet.Text = "Remove set";
ButtonDelSet.UseVisualStyleBackColor = false;
ButtonDelSet.Click += ButtonDelSet_Click;
//
// listBoxSets
//
listBoxSets.Anchor = AnchorStyles.Top | AnchorStyles.Right;
listBoxSets.FormattingEnabled = true;
listBoxSets.ItemHeight = 15;
listBoxSets.Location = new Point(7, 133);
listBoxSets.Name = "listBoxSets";
listBoxSets.Size = new Size(145, 94);
listBoxSets.TabIndex = 6;
listBoxSets.SelectedIndexChanged += listBoxSets_SelectedIndexChanged;
//
// labelSetsName
//
labelSetsName.Anchor = AnchorStyles.Top | AnchorStyles.Right;
labelSetsName.AutoSize = true;
labelSetsName.Font = new Font("Segoe UI Semibold", 12F, FontStyle.Bold, GraphicsUnit.Point);
labelSetsName.Location = new Point(7, 6);
labelSetsName.Name = "labelSetsName";
labelSetsName.Size = new Size(41, 21);
labelSetsName.TabIndex = 5;
labelSetsName.Text = "Sets";
//
// buttonRefreshCollection
//
buttonRefreshCollection.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonRefreshCollection.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRefreshCollection.BackColor = SystemColors.Window;
buttonRefreshCollection.FlatAppearance.BorderColor = Color.Black;
buttonRefreshCollection.FlatStyle = FlatStyle.Flat;
buttonRefreshCollection.Location = new Point(18, 281);
buttonRefreshCollection.Location = new Point(5, 534);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(155, 34);
buttonRefreshCollection.Size = new Size(175, 34);
buttonRefreshCollection.TabIndex = 3;
buttonRefreshCollection.Text = "Refresh collection";
buttonRefreshCollection.UseVisualStyleBackColor = false;
buttonRefreshCollection.Click += buttonRefreshCollection_Click;
//
// labelName
// labelToolsName
//
labelName.AutoSize = true;
labelName.Font = new Font("Segoe UI Semibold", 12F, FontStyle.Bold, GraphicsUnit.Point);
labelName.Location = new Point(17, -2);
labelName.Name = "labelName";
labelName.Size = new Size(48, 21);
labelName.TabIndex = 0;
labelName.Text = "Tools";
labelToolsName.Anchor = AnchorStyles.Top | AnchorStyles.Right;
labelToolsName.AutoSize = true;
labelToolsName.Font = new Font("Segoe UI Semibold", 12F, FontStyle.Bold, GraphicsUnit.Point);
labelToolsName.Location = new Point(18, 9);
labelToolsName.Name = "labelToolsName";
labelToolsName.Size = new Size(48, 21);
labelToolsName.TabIndex = 0;
labelToolsName.Text = "Tools";
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
maskedTextBoxNumber.BorderStyle = BorderStyle.FixedSingle;
maskedTextBoxNumber.Location = new Point(17, 144);
maskedTextBoxNumber.Location = new Point(4, 411);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(155, 23);
maskedTextBoxNumber.Size = new Size(175, 23);
maskedTextBoxNumber.TabIndex = 2;
//
// buttonRemoveMonorail
//
buttonRemoveMonorail.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonRemoveMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRemoveMonorail.BackColor = SystemColors.Window;
buttonRemoveMonorail.FlatAppearance.BorderColor = Color.Black;
buttonRemoveMonorail.FlatStyle = FlatStyle.Flat;
buttonRemoveMonorail.Location = new Point(18, 173);
buttonRemoveMonorail.Location = new Point(4, 451);
buttonRemoveMonorail.Name = "buttonRemoveMonorail";
buttonRemoveMonorail.Size = new Size(155, 34);
buttonRemoveMonorail.Size = new Size(175, 34);
buttonRemoveMonorail.TabIndex = 1;
buttonRemoveMonorail.Text = "Remove monorail";
buttonRemoveMonorail.UseVisualStyleBackColor = false;
@ -101,13 +182,13 @@
//
// buttonAddMonorail
//
buttonAddMonorail.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonAddMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonAddMonorail.BackColor = SystemColors.Window;
buttonAddMonorail.FlatAppearance.BorderColor = Color.Black;
buttonAddMonorail.FlatStyle = FlatStyle.Flat;
buttonAddMonorail.Location = new Point(18, 33);
buttonAddMonorail.Location = new Point(4, 357);
buttonAddMonorail.Name = "buttonAddMonorail";
buttonAddMonorail.Size = new Size(155, 34);
buttonAddMonorail.Size = new Size(175, 34);
buttonAddMonorail.TabIndex = 0;
buttonAddMonorail.Text = "Add monorail";
buttonAddMonorail.UseVisualStyleBackColor = false;
@ -115,9 +196,9 @@
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(5, 10);
pictureBoxCollection.Location = new Point(12, 10);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(777, 430);
pictureBoxCollection.Size = new Size(774, 580);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
@ -125,13 +206,15 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(974, 439);
ClientSize = new Size(974, 589);
Controls.Add(pictureBoxCollection);
Controls.Add(panelTools);
Name = "FormMonorailCollection";
Text = "Monorail collection";
panelTools.ResumeLayout(false);
panelTools.PerformLayout();
panelSets.ResumeLayout(false);
panelSets.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
@ -139,11 +222,17 @@
#endregion
private Panel panelTools;
private Label labelName;
private Label labelToolsName;
private Button buttonAddMonorail;
private Button buttonRemoveMonorail;
private MaskedTextBox maskedTextBoxNumber;
private Button buttonRefreshCollection;
private PictureBox pictureBoxCollection;
private Panel panelSets;
private Label labelSetsName;
private ListBox listBoxSets;
private Button ButtonDelSet;
private Button buttonAddSet;
private TextBox textBoxNameSet;
}
}

View File

@ -1,6 +1,4 @@
using ProjectMonorail.Generics;
using ProjectMonorail.MovementStrategy;
using ProjectMonorail.DrawingObjects;
namespace ProjectMonorail
{
@ -12,7 +10,7 @@ namespace ProjectMonorail
/// <summary>
/// Набор объектов
/// </summary>
private readonly MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail> _monorails;
private readonly MonorailsGenericStorage _storage;
/// <summary>
/// Конструктор
@ -20,7 +18,73 @@ namespace ProjectMonorail
public FormMonorailCollection()
{
InitializeComponent();
_monorails = new MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new MonorailsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение listBoxSets
/// </summary>
private void ReloadObjects()
{
int index = listBoxSets.SelectedIndex;
listBoxSets.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxSets.Items.Add(_storage.Keys[i]);
}
if (listBoxSets.Items.Count > 0 && (index == -1 || index >= listBoxSets.Items.Count))
{
listBoxSets.SelectedIndex = 0;
}
else if (listBoxSets.Items.Count > 0 && index > -1 && index < listBoxSets.Items.Count)
{
listBoxSets.SelectedIndex = index;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddSet_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxNameSet.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxNameSet.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxSets_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxSets.SelectedItem?.ToString() ?? string.Empty]?.ShowMonorails();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelSet_Click(object sender, EventArgs e)
{
if (listBoxSets.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить набор {listBoxSets.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxSets.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
/// <summary>
@ -30,13 +94,24 @@ namespace ProjectMonorail
/// <param name="e"></param>
private void buttonAddMonorail_Click(object sender, EventArgs e)
{
if (listBoxSets.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxSets.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormMonorail form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_monorails + form.SelectedMonorail != -1)
if (obj + form.SelectedMonorail != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _monorails.ShowMonorails();
pictureBoxCollection.Image = obj.ShowMonorails();
}
else
{
@ -52,6 +127,17 @@ namespace ProjectMonorail
/// <param name="e"></param>
private void buttonRemoveMonorail_Click(object sender, EventArgs e)
{
if (listBoxSets.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxSets.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
@ -59,10 +145,10 @@ namespace ProjectMonorail
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_monorails - pos)
if (obj - pos)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _monorails.ShowMonorails();
pictureBoxCollection.Image = obj.ShowMonorails();
}
else
{
@ -77,7 +163,18 @@ namespace ProjectMonorail
/// <param name="e"></param>
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _monorails.ShowMonorails();
if (listBoxSets.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxSets.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowMonorails();
}
}
}

View File

@ -74,7 +74,7 @@ namespace ProjectMonorail.Generics
/// <returns></returns>
public static bool operator -(MonorailsGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
@ -89,7 +89,7 @@ namespace ProjectMonorail.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
@ -130,11 +130,10 @@ namespace ProjectMonorail.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
T? obj;
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int diff = 1, currWidth = 0;
for (int i = 0; i < _collection.Count; i++)
int diff = 1, currWidth = 0, i = 0;
foreach (var monorail in _collection.GetMonorails())
{
currWidth++;
if (currWidth > width)
@ -142,13 +141,13 @@ namespace ProjectMonorail.Generics
diff++;
currWidth = 1;
}
obj = _collection.Get(i);
if (obj != null)
if (monorail != null)
{
obj.SetPosition(i % width * _placeSizeWidth + _placeSizeWidth / 40,
(height - diff) * _placeSizeHeight + _placeSizeHeight / 15);
obj.DrawTransport(g);
monorail.SetPosition(i % width * _placeSizeWidth + _placeSizeWidth / 40,
(height - diff) * _placeSizeHeight + _placeSizeHeight / 15);
monorail.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,78 @@
using ProjectMonorail.DrawingObjects;
using ProjectMonorail.MovementStrategy;
namespace ProjectMonorail.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class MonorailsGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>> _monorailsStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _monorailsStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public MonorailsGenericStorage(int pictureWidth, int pictureHeight)
{
_monorailsStorages = new Dictionary<string, MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (!_monorailsStorages.ContainsKey(name))
_monorailsStorages.Add(name, new MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>(_pictureWidth, _pictureHeight));
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (_monorailsStorages.ContainsKey(name))
_monorailsStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MonorailsGenericCollection<DrawingMonorail, DrawingObjectMonorail>? this[string ind]
{
get
{
if (_monorailsStorages.ContainsKey(ind))
return _monorailsStorages[ind];
return null;
}
}
}
}

View File

@ -1,6 +1,4 @@
using System.Numerics;
namespace ProjectMonorail.Generics
namespace ProjectMonorail.Generics
{
/// <summary>
/// Параметризованный набор объектов
@ -9,14 +7,19 @@ namespace ProjectMonorail.Generics
internal class SetGeneric<T> 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>
private readonly int _maxCount;
/// <summary>
/// Конструктор
@ -24,7 +27,8 @@ namespace ProjectMonorail.Generics
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
@ -45,28 +49,10 @@ namespace ProjectMonorail.Generics
/// <returns></returns>
public int Insert(T monorail, int position)
{
int nullIndex = -1, i;
if (position < 0 || position >= Count)
if (position < 0 || position > Count || Count >= _maxCount)
return -1;
for (i = position; i < Count; i++)
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
}
if (nullIndex < 0)
return -1;
for (i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = monorail;
_places.Insert(position, monorail);
return position;
}
@ -79,7 +65,7 @@ namespace ProjectMonorail.Generics
{
if (position < 0 || position >= Count)
return false;
_places[position] = null;
_places.RemoveAt(position);
return true;
}
@ -88,11 +74,37 @@ namespace ProjectMonorail.Generics
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position >= Count)
return null;
return _places[position];
get
{
if (position < 0 || position >= Count)
return null;
return _places[position];
}
set
{
if (position < 0 || position > Count || Count >= _maxCount)
return;
_places.Insert(position, value);
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetMonorails(int? maxCars = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxCars.HasValue && i == maxCars.Value)
{
yield break;
}
}
}
}
}