diff --git a/Data Structures/DataStructures.Benchmarks/Array/DynamicArrayBenchmarks.cs b/Data Structures/DataStructures.Benchmarks/Array/DynamicArrayBenchmarks.cs new file mode 100644 index 0000000..98bfc4f --- /dev/null +++ b/Data Structures/DataStructures.Benchmarks/Array/DynamicArrayBenchmarks.cs @@ -0,0 +1,32 @@ +public class DynamicArrayBenchmark +{ + private DynamicArray _dynamicArray; + + [GlobalSetup] + public void Setup() + { + _dynamicArray = new DynamicArray(); + } + + [Benchmark] + public void AddElements() + { + for (int i = 0; i < 1000; i++) + { + _dynamicArray.Add(i); + } + } + + [Benchmark] + public void RemoveElements() + { + for (int i = 0; i < 1000; i++) + { + _dynamicArray.Add(i); + } + for (int i = 999; i >= 0; i--) + { + _dynamicArray.RemoveAt(i); + } + } +} \ No newline at end of file diff --git a/Data Structures/DataStructures.Benchmarks/Array/MultidementionalArrayBenchmarks.cs b/Data Structures/DataStructures.Benchmarks/Array/MultidementionalArrayBenchmarks.cs new file mode 100644 index 0000000..df6bf76 --- /dev/null +++ b/Data Structures/DataStructures.Benchmarks/Array/MultidementionalArrayBenchmarks.cs @@ -0,0 +1,42 @@ +[MemoryDiagnoser] +public class MultidimensionalArrayBenchmark +{ + private MultidimensionalArray _array; + + [GlobalSetup] + public void Setup() + { + _array = new MultidimensionalArray(100, 100); + } + + [Benchmark] + public void AddBenchmark() + { + for (int i = 0; i < 100; i++) + for (int j = 0; j < 100; j++) + _array.Add(i, j, i * j); + } + + [Benchmark] + public void GetBenchmark() + { + for (int i = 0; i < 100; i++) + for (int j = 0; j < 100; j++) + _array.Get(i, j); + } + + [Benchmark] + public void RemoveRowBenchmark() + { + for (int i = 0; i < 100; i++) + _array.Remove(i); + } + + [Benchmark] + public void RemoveElementBenchmark() + { + for (int i = 0; i < 100; i++) + for (int j = 0; j < 100; j++) + _array.Remove(i, j); + } +} \ No newline at end of file diff --git a/Data Structures/DataStructures.Benchmarks/Program.cs b/Data Structures/DataStructures.Benchmarks/Program.cs index 0e28475..2087777 100644 --- a/Data Structures/DataStructures.Benchmarks/Program.cs +++ b/Data Structures/DataStructures.Benchmarks/Program.cs @@ -1,5 +1,7 @@ #region Array BenchmarkRunner.Run(); +BenchmarkRunner.Run(); +BenchmarkRunner.Run(); #endregion #region Stack diff --git a/Data Structures/DataStructures.Core/Array/DynamicArray.cs b/Data Structures/DataStructures.Core/Array/DynamicArray.cs new file mode 100644 index 0000000..8c3301a --- /dev/null +++ b/Data Structures/DataStructures.Core/Array/DynamicArray.cs @@ -0,0 +1,68 @@ +public class DynamicArray +{ + private T[] _array; + private int _count; + private int _capacity; + + public DynamicArray(int capacity = 4) + { + _capacity = capacity; + _array = new T[_capacity]; + _count = 0; + } + + public int Count => _count; + + public T this[int index] + { + get + { + if (index < 0 || index >= _count) + throw new IndexOutOfRangeException("Index out of range"); + return _array[index]; + } + set + { + if (index < 0 || index >= _count) + throw new IndexOutOfRangeException("Index out of range"); + _array[index] = value; + } + } + + public void Add(T item) + { + if (_count == _capacity) + { + Extend(); + } + _array[_count] = item; + _count++; + } + + private void Extend(int growthPercentage = 25) + { + int increase = (int)Math.Ceiling(_capacity * growthPercentage / 100.0); + _capacity += increase; + + T[] newArray = new T[_capacity]; + for (int i = 0; i < _count; i++) + { + newArray[i] = _array[i]; + } + _array = newArray; + } + + public void RemoveAt(int index) + { + if (index < 0 || index >= _count) + throw new IndexOutOfRangeException("Index out of range"); + + for (int i = index; i < _count - 1; i++) + { + _array[i] = _array[i + 1]; + } + + _count--; + _array[_count] = default; + } +} \ No newline at end of file diff --git a/Data Structures/DataStructures.Core/Array/MultidementionalArray.cs b/Data Structures/DataStructures.Core/Array/MultidementionalArray.cs new file mode 100644 index 0000000..695ef2d --- /dev/null +++ b/Data Structures/DataStructures.Core/Array/MultidementionalArray.cs @@ -0,0 +1,71 @@ +public class MultidimensionalArray +{ + private T[,] _array; + + public MultidimensionalArray(int rows, int columns) + { + _array = new T[rows, columns]; + } + + public T this[int row, int column] + { + get => _array[row, column]; + set => _array[row, column] = value; + } + + public int Rows => _array.GetLength(0); + public int Columns => _array.GetLength(1); + + public void Add(int row, int column, T value) + { + if (row >= Rows || column >= Columns) + throw new IndexOutOfRangeException("Invalid index"); + _array[row, column] = value; + } + + public T Get(int row, int column) + { + if (row >= Rows || column >= Columns) + throw new IndexOutOfRangeException("Invalid index"); + return _array[row, column]; + } + + public void Remove(int row) + { + if (row < 0 || row >= Rows) + throw new IndexOutOfRangeException("Invalid row index"); + + for (int r = row; r < Rows - 1; r++) + { + for (int c = 0; c < Columns; c++) + { + _array[r, c] = _array[r + 1, c]; + } + } + + // Set the last row to default values + for (int c = 0; c < Columns; c++) + { + _array[Rows - 1, c] = default(T); + } + } + + public void Remove(int row, int column) + { + if (row >= Rows || column >= Columns) + throw new IndexOutOfRangeException("Invalid index"); + + for (int r = row; r < Rows; r++) + { + for (int c = column; c < Columns - 1; c++) + { + _array[r, c] = _array[r, c + 1]; + } + if (r < Rows - 1) + { + _array[r, Columns - 1] = _array[r + 1, 0]; + } + } + _array[Rows - 1, Columns - 1] = default(T); + } +} \ No newline at end of file diff --git a/Data Structures/DataStructures.Tests/Array/DynamicArrayTests.cs b/Data Structures/DataStructures.Tests/Array/DynamicArrayTests.cs new file mode 100644 index 0000000..00d89a0 --- /dev/null +++ b/Data Structures/DataStructures.Tests/Array/DynamicArrayTests.cs @@ -0,0 +1,54 @@ +[TestFixture] +public class DynamicArrayTests +{ + [Test] + public void Add_ShouldIncreaseCount() + { + var dynamicArray = new DynamicArray(); + dynamicArray.Add(1); + + dynamicArray.Count.Should().Be(1); + } + + [Test] + public void Add_ShouldResizeWhenCapacityIsExceeded() + { + var dynamicArray = new DynamicArray(2); + dynamicArray.Add(1); + dynamicArray.Add(2); + dynamicArray.Add(3); + + dynamicArray.Count.Should().BeGreaterThan(2); + } + + [Test] + public void Indexer_Get_ShouldReturnCorrectValue() + { + var dynamicArray = new DynamicArray(); + dynamicArray.Add(1); + + dynamicArray[0].Should().Be(1); + } + + [Test] + public void Indexer_Set_ShouldUpdateValue() + { + var dynamicArray = new DynamicArray(); + dynamicArray.Add(1); + dynamicArray[0] = 10; + + dynamicArray[0].Should().Be(10); + } + + [Test] + public void RemoveAt_ShouldDecreaseCount() + { + var dynamicArray = new DynamicArray(); + dynamicArray.Add(1); + dynamicArray.Add(2); + dynamicArray.RemoveAt(0); + + dynamicArray.Count.Should().Be(1); + dynamicArray[0].Should().Be(2); + } +} \ No newline at end of file diff --git a/Data Structures/DataStructures.Tests/Array/MultidementionalArrayTests.cs b/Data Structures/DataStructures.Tests/Array/MultidementionalArrayTests.cs new file mode 100644 index 0000000..c4b83af --- /dev/null +++ b/Data Structures/DataStructures.Tests/Array/MultidementionalArrayTests.cs @@ -0,0 +1,75 @@ +[TestFixture] +public class MultidimensionalArrayTests +{ + private MultidimensionalArray _array; + + [SetUp] + public void SetUp() + { + _array = new MultidimensionalArray(3, 3); + } + + [Test] + public void Add_ShouldStoreValueAtSpecifiedPosition() + { + _array.Add(1, 1, 42); + _array.Get(1, 1).Should().Be(42); + } + + [Test] + public void Get_ShouldReturnCorrectValue() + { + _array.Add(2, 2, 99); + var value = _array.Get(2, 2); + value.Should().Be(99); + } + + [Test] + public void Remove_SpecificPosition_ShouldShiftValuesLeft() + { + _array.Add(0, 0, 10); + _array.Add(0, 1, 20); + _array.Add(0, 2, 30); + _array.Remove(0, 1); + + _array.Get(0, 1).Should().Be(30); + _array.Get(0, 2).Should().Be(default(int)); + } + + [Test] + public void Remove_EntireRow_ShouldShiftRowsUp() + { + _array.Add(0, 0, 10); + _array.Add(1, 0, 20); + _array.Add(2, 0, 30); + + _array.Remove(1); + + _array.Get(1, 0).Should().Be(30); + _array.Get(2, 0).Should().Be(default(int)); + } + + [Test] + public void Add_ShouldThrowException_WhenIndexOutOfBounds() + { + Assert.Throws(() => _array.Add(5, 5, 99)); + } + + [Test] + public void Get_ShouldThrowException_WhenIndexOutOfBounds() + { + Assert.Throws(() => _array.Get(5, 5)); + } + + [Test] + public void Remove_ShouldThrowException_WhenRowIndexOutOfBounds() + { + Assert.Throws(() => _array.Remove(5)); + } + + [Test] + public void Remove_ShouldThrowException_WhenColumnIndexOutOfBounds() + { + Assert.Throws(() => _array.Remove(0, 5)); + } +} \ No newline at end of file