Merge branch 'main'
Merging branch main (from rider) to master
This commit is contained in:
commit
3a97899126
10 changed files with 5381 additions and 0 deletions
15
FlexArray/FlexArray.csproj
Normal file
15
FlexArray/FlexArray.csproj
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HTLLeonding.Utility.LeoAnalyzers" Version="1.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
110
FlexArray/FlexArrayInt.cs
Normal file
110
FlexArray/FlexArrayInt.cs
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
namespace FlexArray;
|
||||
|
||||
public sealed class FlexArrayInt
|
||||
{
|
||||
public const int DefaultStartSize = 4;
|
||||
private int[] _data;
|
||||
|
||||
public int Count { get; private set; }
|
||||
public int Capacity => _data.Length;
|
||||
|
||||
public int this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (index < 0 || index > Count)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return _data[index];
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(int value, bool firstOnly)
|
||||
{
|
||||
var removedAny = false;
|
||||
for (var i = 0; i < Count; i++)
|
||||
{
|
||||
if (value != _data[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
RemoveAt(i--);
|
||||
removedAny = true;
|
||||
if (firstOnly)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return removedAny;
|
||||
}
|
||||
|
||||
public bool RemoveAt(int index)
|
||||
{
|
||||
if (index < 0 || index >= Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index != Count - 1)
|
||||
{
|
||||
ShiftLeft(index);
|
||||
|
||||
}
|
||||
|
||||
Count--;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ShiftLeft(int fromIndex)
|
||||
{
|
||||
for (var i = fromIndex; i < Count; i++)
|
||||
{
|
||||
_data[i] = _data[i + 1];
|
||||
}
|
||||
}
|
||||
public FlexArrayInt(int? initialSize = null)
|
||||
{
|
||||
var size = Math.Max(0, initialSize ?? DefaultStartSize);
|
||||
_data = new int[size];
|
||||
}
|
||||
|
||||
public void Add(int value)
|
||||
{
|
||||
if (Capacity == Count)
|
||||
{
|
||||
Grow();
|
||||
}
|
||||
|
||||
_data[Count++] = value;
|
||||
}
|
||||
|
||||
public bool Contains(int value)
|
||||
{
|
||||
if (Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = 0; i < Count; i++)
|
||||
{
|
||||
if (_data[i] == value)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
private void Grow()
|
||||
{
|
||||
var newData = new int[Capacity * 2];
|
||||
Array.Copy(_data, newData, Count);
|
||||
_data = newData;
|
||||
|
||||
}
|
||||
}
|
||||
6
FlexArray/FlexArrayString.cs
Normal file
6
FlexArray/FlexArrayString.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
namespace FlexArray;
|
||||
|
||||
public class FlexArrayString
|
||||
{
|
||||
|
||||
}
|
||||
16
FlexArray/Program.cs
Normal file
16
FlexArray/Program.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
using System.Text;
|
||||
using FlexArray;
|
||||
|
||||
Console.OutputEncoding = Encoding.UTF8;
|
||||
|
||||
Console.WriteLine("*** FlexArray ***");
|
||||
|
||||
var energyDrinkCans = new FlexArrayInt(2);
|
||||
Console.WriteLine($"{energyDrinkCans.Count} | {energyDrinkCans.Capacity}");
|
||||
|
||||
energyDrinkCans.Add(50);
|
||||
energyDrinkCans.Add(5);
|
||||
Console.WriteLine($"{energyDrinkCans.Count} | {energyDrinkCans.Capacity}");
|
||||
|
||||
energyDrinkCans.Add(3);
|
||||
Console.WriteLine($"{energyDrinkCans.Count} | {energyDrinkCans.Capacity}");
|
||||
Loading…
Add table
Add a link
Reference in a new issue