This commit is contained in:
Аркадий Радаев 2023-11-19 12:36:59 +04:00
parent aade6261c5
commit a9f980816e
10 changed files with 305 additions and 94 deletions

View File

@ -61,7 +61,7 @@ namespace Catamaran
} }
return _moveableObject?.GetStep; return _moveableObject?.GetStep;
} }
protected abstract void MoveToTarget(); protected abstract void MoveToTarget();
protected abstract bool IsTargetDestinaion(); protected abstract bool IsTargetDestinaion();

View File

@ -29,26 +29,26 @@ namespace Catamaran
_collection = new SetGeneric<T>(width * height); _collection = new SetGeneric<T>(width * height);
} }
public static int operator +(CatamaransGenericCollection<T, U> collect, T? obj) public static bool operator +(CatamaransGenericCollection<T, U> collect, T? obj)
{ {
if (obj == null) if (obj == null)
return -1; return false;
return collect?._collection.Insert(obj) ?? -1; return collect?._collection.Insert(obj) ?? false;
} }
public static bool operator -(CatamaransGenericCollection<T, U> collect, int pos) public static T? operator -(CatamaransGenericCollection<T, U> collect, int pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj != null) if (obj != null)
collect._collection.Remove(pos); collect._collection.Remove(pos);
return false; return obj;
} }
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
public Bitmap ShowCats() public Bitmap ShowCats()
{ {
Bitmap bmp = new(_pictureWidth, _pictureHeight); Bitmap bmp = new(_pictureWidth, _pictureHeight);
@ -75,15 +75,16 @@ namespace Catamaran
} }
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
for (int i = 0; i < _collection.Count; i++) int i = 0;
foreach (var catamaran in _collection.GetCatamarans())
{ {
DrawningCatamaran catamaran = _collection.Get(i); if (catamaran!= null)
if (catamaran != null)
{ {
int inRow = _pictureWidth / _placeSizeWidth; int inRow = _pictureWidth / _placeSizeWidth;
catamaran.SetPosition(i % inRow * _placeSizeWidth, ((_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight)+5); catamaran.SetPosition(i % inRow * _placeSizeWidth, ((_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight)+5);
catamaran.DrawTransport(g); catamaran.DrawTransport(g);
} }
i++;
} }
} }
} }

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class CatamaransGenericStorage
{
readonly Dictionary<string, CatamaransGenericCollection<DrawningCatamaran,
DrawningObjectCatamaran>> _catStorages;
public List<string> Keys => _catStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public CatamaransGenericStorage(int pictureWidth, int pictureHeight)
{
_catStorages = new Dictionary<string,
CatamaransGenericCollection<DrawningCatamaran,
DrawningObjectCatamaran>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
_catStorages.Add(name, new CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_catStorages.ContainsKey(name))
return;
_catStorages.Remove(name);
}
public CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran>? this[string ind]
{
get
{
if (_catStorages.ContainsKey(ind))
return _catStorages[ind];
return null;
}
}
}
}

View File

@ -87,6 +87,7 @@ namespace Catamaran
}; };
} }
public void MoveTransport(DirectionType direction) public void MoveTransport(DirectionType direction)
{ {
if (EntityCatamaran == null) if (EntityCatamaran == null)

View File

@ -9,16 +9,15 @@ namespace Catamaran
public class DrawningObjectCatamaran : IMoveableObject public class DrawningObjectCatamaran : IMoveableObject
{ {
private readonly DrawningCatamaran? _DrawningCatamaran = null; private readonly DrawningCatamaran? _DrawningCatamaran = null;
public DrawningObjectCatamaran(DrawningCatamaran DrawningCatamaran) public DrawningObjectCatamaran(DrawningCatamaran drawningCatamaran)
{ {
_DrawningCatamaran = DrawningCatamaran; _DrawningCatamaran = drawningCatamaran;
} }
public ObjectParameters? GetObjectPosition public ObjectParameters? GetObjectPosition
{ {
get get
{ {
if (_DrawningCatamaran == null || _DrawningCatamaran.EntityCatamaran == if (_DrawningCatamaran == null || _DrawningCatamaran.EntityCatamaran == null)
null)
{ {
return null; return null;
} }

View File

@ -2,7 +2,7 @@
{ {
partial class FormCatamaran partial class FormCatamaran
{ {
private System.ComponentModel.IContainer components = null; private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
@ -18,7 +18,7 @@
private void InitializeComponent() private void InitializeComponent()
{ {
pictureBox=new PictureBox(); pictureBoxCatatamaran=new PictureBox();
buttonCreateBasic=new Button(); buttonCreateBasic=new Button();
buttonUp=new Button(); buttonUp=new Button();
buttonRight=new Button(); buttonRight=new Button();
@ -28,19 +28,19 @@
comboBox=new ComboBox(); comboBox=new ComboBox();
buttonStep=new Button(); buttonStep=new Button();
buttonSelect=new Button(); buttonSelect=new Button();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCatatamaran).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// pictureBox // pictureBoxCatatamaran
// //
pictureBox.Dock=DockStyle.Fill; pictureBoxCatatamaran.Dock=DockStyle.Fill;
pictureBox.Location=new Point(0, 0); pictureBoxCatatamaran.Location=new Point(0, 0);
pictureBox.Margin=new Padding(4, 5, 4, 5); pictureBoxCatatamaran.Margin=new Padding(4, 5, 4, 5);
pictureBox.Name="pictureBox"; pictureBoxCatatamaran.Name="pictureBoxCatatamaran";
pictureBox.Size=new Size(1143, 750); pictureBoxCatatamaran.Size=new Size(1143, 750);
pictureBox.TabIndex=5; pictureBoxCatatamaran.TabIndex=5;
pictureBox.TabStop=false; pictureBoxCatatamaran.TabStop=false;
pictureBox.Click+=ButtonMove_Click; pictureBoxCatatamaran.Click+=ButtonMove_Click;
// //
// buttonCreateBasic // buttonCreateBasic
// //
@ -160,11 +160,11 @@
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonCreateBasic); Controls.Add(buttonCreateBasic);
Controls.Add(pictureBox); Controls.Add(pictureBoxCatatamaran);
Margin=new Padding(4, 5, 4, 5); Margin=new Padding(4, 5, 4, 5);
Name="FormCatamaran"; Name="FormCatamaran";
Text="FormCatamaran"; Text="FormCatamaran";
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCatatamaran).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -172,7 +172,7 @@
private Button button1; private Button button1;
private Button UpBut; private Button UpBut;
private PictureBox pictureBox; private PictureBox pictureBoxCatatamaran;
private Button buttonCreateBasic; private Button buttonCreateBasic;
private Button buttonUp; private Button buttonUp;
private Button buttonRight; private Button buttonRight;

View File

@ -17,10 +17,10 @@ namespace Catamaran
{ {
return; return;
} }
Bitmap bmp = new(pictureBox.Width, pictureBox.Height); Bitmap bmp = new(pictureBoxCatatamaran.Width, pictureBoxCatatamaran.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_DrawningCatamaran.DrawTransport(gr); _DrawningCatamaran.DrawTransport(gr);
pictureBox.Image = bmp; pictureBoxCatatamaran.Image = bmp;
} }
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
@ -59,7 +59,7 @@ namespace Catamaran
{ {
bodyColor = dialog.Color; bodyColor = dialog.Color;
} }
_DrawningCatamaran = new DrawningCatamaran(random.Next(10, 30), random.Next(100, 300), bodyColor, additionalColor, pictureBox.Width, pictureBox.Height, true); _DrawningCatamaran = new DrawningCatamaran(random.Next(10, 30), random.Next(100, 300), bodyColor, additionalColor, pictureBoxCatatamaran.Width, pictureBoxCatatamaran.Height, true);
_DrawningCatamaran.SetPosition(random.Next(10, 100), random.Next(10, 100)); _DrawningCatamaran.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw(); Draw();
@ -82,9 +82,9 @@ namespace Catamaran
{ {
additionalColor = dialog.Color; additionalColor = dialog.Color;
} }
_DrawningCatamaran = new DrawningCatamaranPro(random.Next(100, 300), _DrawningCatamaran = new DrawningCatamaranPro(random.Next(100, 300),
random.Next(1000, 3000), bodyColor, otherColor, additionalColor, true, true, true, pictureBox.Width, pictureBox.Height, true); random.Next(1000, 3000), bodyColor, otherColor, additionalColor, true, true, true, pictureBoxCatatamaran.Width, pictureBoxCatatamaran.Height, true);
_DrawningCatamaran.SetPosition(random.Next(10, 100), random.Next(10, 100)); _DrawningCatamaran.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw(); Draw();
@ -111,8 +111,8 @@ namespace Catamaran
return; return;
} }
_abstractStrategy.SetData(new _abstractStrategy.SetData(new
DrawningObjectCatamaran(_DrawningCatamaran), pictureBox.Width, DrawningObjectCatamaran(_DrawningCatamaran), pictureBoxCatatamaran.Width,
pictureBox.Height); pictureBoxCatatamaran.Height);
comboBox.Enabled = false; comboBox.Enabled = false;
} }
if (_abstractStrategy == null) if (_abstractStrategy == null)

View File

@ -25,35 +25,42 @@
buttonDeleteCat=new Button(); buttonDeleteCat=new Button();
buttonAddCat=new Button(); buttonAddCat=new Button();
pictureBoxCollection=new PictureBox(); pictureBoxCollection=new PictureBox();
setsBox=new GroupBox();
AddSetButton=new Button();
setAddBox=new TextBox();
SetslistBox=new ListBox();
DeleteSetButton=new Button();
groupBoxCollection.SuspendLayout(); groupBoxCollection.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
setsBox.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// groupBoxCollection // groupBoxCollection
// //
groupBoxCollection.Controls.Add(setsBox);
groupBoxCollection.Controls.Add(maskedTextBox); groupBoxCollection.Controls.Add(maskedTextBox);
groupBoxCollection.Controls.Add(buttonUpdateCollection); groupBoxCollection.Controls.Add(buttonUpdateCollection);
groupBoxCollection.Controls.Add(buttonDeleteCat); groupBoxCollection.Controls.Add(buttonDeleteCat);
groupBoxCollection.Controls.Add(buttonAddCat); groupBoxCollection.Controls.Add(buttonAddCat);
groupBoxCollection.Location=new Point(889, 12); groupBoxCollection.Location=new Point(889, 12);
groupBoxCollection.Name="groupBoxCollection"; groupBoxCollection.Name="groupBoxCollection";
groupBoxCollection.Size=new Size(242, 637); groupBoxCollection.Size=new Size(242, 738);
groupBoxCollection.TabIndex=0; groupBoxCollection.TabIndex=0;
groupBoxCollection.TabStop=false; groupBoxCollection.TabStop=false;
groupBoxCollection.Text="Инструменты"; groupBoxCollection.Text="Инструменты";
// //
// maskedTextBox // maskedTextBox
// //
maskedTextBox.Location=new Point(21, 138); maskedTextBox.Location=new Point(21, 537);
maskedTextBox.Name="maskedTextBox"; maskedTextBox.Name="maskedTextBox";
maskedTextBox.Size=new Size(215, 31); maskedTextBox.Size=new Size(204, 31);
maskedTextBox.TabIndex=2; maskedTextBox.TabIndex=2;
// //
// buttonUpdateCollection // buttonUpdateCollection
// //
buttonUpdateCollection.Location=new Point(21, 286); buttonUpdateCollection.Location=new Point(21, 666);
buttonUpdateCollection.Name="buttonUpdateCollection"; buttonUpdateCollection.Name="buttonUpdateCollection";
buttonUpdateCollection.Size=new Size(215, 54); buttonUpdateCollection.Size=new Size(204, 54);
buttonUpdateCollection.TabIndex=2; buttonUpdateCollection.TabIndex=2;
buttonUpdateCollection.Text="Обновить коллекцию"; buttonUpdateCollection.Text="Обновить коллекцию";
buttonUpdateCollection.UseVisualStyleBackColor=true; buttonUpdateCollection.UseVisualStyleBackColor=true;
@ -61,23 +68,23 @@
// //
// buttonDeleteCat // buttonDeleteCat
// //
buttonDeleteCat.Location=new Point(21, 186); buttonDeleteCat.Location=new Point(21, 584);
buttonDeleteCat.Name="buttonDeleteCat"; buttonDeleteCat.Name="buttonDeleteCat";
buttonDeleteCat.Size=new Size(215, 54); buttonDeleteCat.Size=new Size(204, 54);
buttonDeleteCat.TabIndex=1; buttonDeleteCat.TabIndex=1;
buttonDeleteCat.Text="Удалить катамаран"; buttonDeleteCat.Text="Удалить катамаран";
buttonDeleteCat.UseVisualStyleBackColor=true; buttonDeleteCat.UseVisualStyleBackColor=true;
buttonDeleteCat.Click+=ButtonRemoveCat_Click; buttonDeleteCat.Click+=ButtonRemoveCatamaran_Click;
// //
// buttonAddCat // buttonAddCat
// //
buttonAddCat.Location=new Point(21, 39); buttonAddCat.Location=new Point(21, 465);
buttonAddCat.Name="buttonAddCat"; buttonAddCat.Name="buttonAddCat";
buttonAddCat.Size=new Size(215, 54); buttonAddCat.Size=new Size(204, 54);
buttonAddCat.TabIndex=0; buttonAddCat.TabIndex=0;
buttonAddCat.Text="Добавить катамаран"; buttonAddCat.Text="Добавить катамаран";
buttonAddCat.UseVisualStyleBackColor=true; buttonAddCat.UseVisualStyleBackColor=true;
buttonAddCat.Click+=ButtonAddCat_Click; buttonAddCat.Click+=ButtonAddCatamaran_Click;
// //
// pictureBoxCollection // pictureBoxCollection
// //
@ -87,6 +94,57 @@
pictureBoxCollection.TabIndex=1; pictureBoxCollection.TabIndex=1;
pictureBoxCollection.TabStop=false; pictureBoxCollection.TabStop=false;
// //
// setsBox
//
setsBox.Controls.Add(DeleteSetButton);
setsBox.Controls.Add(SetslistBox);
setsBox.Controls.Add(setAddBox);
setsBox.Controls.Add(AddSetButton);
setsBox.Location=new Point(6, 45);
setsBox.Name="setsBox";
setsBox.RightToLeft=RightToLeft.No;
setsBox.Size=new Size(219, 374);
setsBox.TabIndex=3;
setsBox.TabStop=false;
setsBox.Text="Наборы";
//
// AddSetButton
//
AddSetButton.Location=new Point(15, 71);
AddSetButton.Name="AddSetButton";
AddSetButton.Size=new Size(190, 54);
AddSetButton.TabIndex=0;
AddSetButton.Text="Добавить набор";
AddSetButton.UseVisualStyleBackColor=true;
AddSetButton.Click+=ButtonAddStorage_Click;
//
// setAddBox
//
setAddBox.Location=new Point(15, 34);
setAddBox.Name="setAddBox";
setAddBox.Size=new Size(190, 31);
setAddBox.TabIndex=1;
//
// SetslistBox
//
SetslistBox.FormattingEnabled=true;
SetslistBox.ItemHeight=25;
SetslistBox.Location=new Point(15, 131);
SetslistBox.Name="SetslistBox";
SetslistBox.Size=new Size(190, 129);
SetslistBox.TabIndex=2;
SetslistBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// DeleteSetButton
//
DeleteSetButton.Location=new Point(15, 279);
DeleteSetButton.Name="DeleteSetButton";
DeleteSetButton.Size=new Size(190, 64);
DeleteSetButton.TabIndex=3;
DeleteSetButton.Text="Удалить набор";
DeleteSetButton.UseVisualStyleBackColor=true;
DeleteSetButton.Click+=ButtonDeleteStorage_Click;
//
// FormCatamaranCollection // FormCatamaranCollection
// //
AutoScaleDimensions=new SizeF(10F, 25F); AutoScaleDimensions=new SizeF(10F, 25F);
@ -99,6 +157,8 @@
groupBoxCollection.ResumeLayout(false); groupBoxCollection.ResumeLayout(false);
groupBoxCollection.PerformLayout(); groupBoxCollection.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
setsBox.ResumeLayout(false);
setsBox.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
} }
@ -110,5 +170,10 @@
private Button buttonDeleteCat; private Button buttonDeleteCat;
private MaskedTextBox maskedTextBox; private MaskedTextBox maskedTextBox;
public PictureBox pictureBoxCollection; public PictureBox pictureBoxCollection;
private GroupBox setsBox;
private Button DeleteSetButton;
private ListBox SetslistBox;
private TextBox setAddBox;
private Button AddSetButton;
} }
} }

View File

@ -12,25 +12,87 @@ namespace Catamaran
{ {
public partial class FormCatamaranCollection : Form public partial class FormCatamaranCollection : Form
{ {
private readonly CatamaransGenericStorage _storage;
private readonly CatamaransGenericCollection<DrawningCatamaran, DrawningObjectCatamaran> _cats;
public FormCatamaranCollection() public FormCatamaranCollection()
{ {
InitializeComponent(); InitializeComponent();
_cats = new CatamaransGenericCollection<DrawningCatamaran, _storage = new CatamaransGenericStorage(pictureBoxCollection.Width,
DrawningObjectCatamaran>(pictureBoxCollection.Width, pictureBoxCollection.Height); pictureBoxCollection.Height);
} }
private void ButtonAddCat_Click(object sender, EventArgs e) private void ReloadObjects()
{ {
int index = SetslistBox.SelectedIndex;
SetslistBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
SetslistBox.Items.Add(_storage.Keys[i]);
}
if (SetslistBox.Items.Count > 0 && (index == -1 || index
>= SetslistBox.Items.Count))
{
SetslistBox.SelectedIndex = 0;
}
else if (SetslistBox.Items.Count > 0 && index > -1 &&
index < SetslistBox.Items.Count)
{
SetslistBox.SelectedIndex = index;
}
}
//
private void ButtonAddStorage_Click(object sender, EventArgs e)
{
string storname = setAddBox.Text;
if (string.IsNullOrEmpty(setAddBox.Text) || _storage.Keys.Contains(storname))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(setAddBox.Text);
ReloadObjects();
}
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[SetslistBox.SelectedItem?.ToString() ?? string.Empty]?.ShowCats();
}
private void ButtonDeleteStorage_Click(object sender, EventArgs e)
{
if (SetslistBox.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить набор { SetslistBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(SetslistBox.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
private void ButtonAddCatamaran_Click(object sender, EventArgs e)
{
if (SetslistBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[SetslistBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
FormCatamaran form = new(); FormCatamaran form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_cats + form.SelectedCatamaran != null) if (obj + form.SelectedCatamaran)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _cats.ShowCats(); pictureBoxCollection.Image = obj.ShowCats();
} }
else else
{ {
@ -38,29 +100,49 @@ namespace Catamaran
} }
} }
} }
private void ButtonRemoveCat_Click(object sender, EventArgs e) private void ButtonRemoveCatamaran_Click(object sender, EventArgs e)
{ {
if (SetslistBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[SetslistBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
int pos = Convert.ToInt32(maskedTextBox.Text); int pos = Convert.ToInt32(maskedTextBox.Text);
if (_cats - pos != null) if (obj - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _cats.ShowCats(); pictureBoxCollection.Image = obj.ShowCats();
} }
else else
{ {
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show("Не удалось удалить объект");
} }
} }
private void ButtonRefreshCollection_Click(object sender, EventArgs e) private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{ {
pictureBoxCollection.Image = _cats.ShowCats(); if (SetslistBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[SetslistBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowCats();
} }
} }
} }

View File

@ -8,39 +8,31 @@ namespace Catamaran
{ {
internal class SetGeneric<T> where T : class internal class SetGeneric<T> where T : class
{ {
private readonly List<T?> _places;
private readonly T?[] _places;
public int Count => _places.Count;
public int Count => _places.Length;
private readonly int _maxCount;
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>(count);
} }
public int Insert(T catamaran) public bool Insert(T catamaran)
{ {
if (_places[Count-1] != null) if (_places.Count == _maxCount)
return -1; return false;
return Insert(catamaran, 0); Insert(catamaran, 0);
return true;
} }
public int Insert(T catamaran, int position) public bool Insert(T catamaran, int position)
{ {
if (!(position >= 0 && position < Count)) if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
return -1; return false;
if (_places[position] != null) _places.Insert(position, catamaran);
{ return true;
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return -1;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = catamaran;
return position;
} }
public bool Remove(int position) public bool Remove(int position)
@ -50,12 +42,32 @@ namespace Catamaran
_places[position] = null; _places[position] = null;
return true; return true;
} }
public T? this[int position]
public T? Get(int position)
{ {
if (!(position >= 0 && position < Count)) get
return null; {
return _places[position]; 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?> GetCatamarans(int? maxMonorails = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxMonorails.HasValue && i == maxMonorails.Value)
{
yield break;
}
}
} }
} }
} }