Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.idea
.env

/bin
/obj
27 changes: 24 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
// See https://aka.ms/new-console-template for more information

using algorithms_cs.Algorithm.Sort.External.Merge;
using algorithms_cs.Algorithm.Sort.External.SeriesMerge;

const int numberWays = 4;
const string pathFile = "E:\\projects\\algorithms-cs\\resource\\1.test";
const string pathFile = "E:\\projects\\algorithms-cs\\resource\\4.txt";

var sort = new MultiwaySort(numberWays, pathFile);
sort.Start();
sort.Start();

//
// var test = new BufferedTapeReader(pathFile);
// var test2 = new TapeCollection(pathFile);
// var ss = new SeriesEnumerable(test);
//
// foreach (var VARIABLE in test2)
// {
// Console.WriteLine(VARIABLE);
// }
//
// while (!ss.IsEnd)
// {
// foreach (var VARIABLE in ss)
// {
// Console.WriteLine($"{VARIABLE.GetType()} {VARIABLE.GetValue()}");
// }
// Console.WriteLine();
// }


2 changes: 2 additions & 0 deletions TestAlgorithms-cs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin
/obj
22 changes: 22 additions & 0 deletions TestAlgorithms-cs/TestAlgorithms-cs.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>TestAlgorithms_cs</RootNamespace>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\algorithms-cs\algorithms-cs.csproj" />
</ItemGroup>

</Project>
59 changes: 59 additions & 0 deletions TestAlgorithms-cs/TestTape.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using algorithms_cs.Utils;
using algorithms_cs.Utils.Tape;

namespace TestAlgorithms_cs;

public class TestTape
{
private const string PathResources = "../../../resource/";

[Test]
public void TestDefaultInitialization()
{
var utilReturn = new UtilReturn<double>();

Assert.That(utilReturn.GetType() == UtilReturnType.TapeEnded);
}

[Test]
public void TestInitializationWithValue()
{
const double value = (double)20;
var utilReturn = new UtilReturn<double>(value);

Assert.That(utilReturn.GetType() == UtilReturnType.Correct && Math.Abs(utilReturn.GetValue() - value) == 0);
}

[Test]
public void TestTapeReader()
{
string path = PathResources + "4.test";

var answer = new List<double>
{
-10,
-9.0,
-8.0,
-7.0012,
-6.0012,
5,
4.12,
3.12
};

var tapeResponse = new List<double>();

var tape = new TapeReader(path);

for (int i = 0; i < 8; i++)
{
tapeResponse.Add(tape.Next().GetValue());
}

Assert.AreEqual(tapeResponse, answer);
}
}
19 changes: 19 additions & 0 deletions TestAlgorithms-cs/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using NUnit.Framework;

namespace TestAlgorithms_cs;

public class Tests
{
[SetUp]
public void Setup()
{
}

[Test]
public void Test1()
{


Assert.Pass();
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions TestAlgorithms-cs/resource/4.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-10 -9,0 -8.0 -7.0012 -6,0012 5 4.12 3,12
16 changes: 11 additions & 5 deletions algorithms-cs.sln
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithms-cs", "algorithms-cs.csproj", "{C310C318-7B2D-4C1C-AC20-23D3FDD31A9E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algorithms-cs", "algorithms-cs\algorithms-cs.csproj", "{91495899-7D40-4543-93B9-820F92C61B31}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAlgorithms-cs", "TestAlgorithms-cs\TestAlgorithms-cs.csproj", "{FAC8701B-6897-488A-B78F-660E8B47FC25}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C310C318-7B2D-4C1C-AC20-23D3FDD31A9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C310C318-7B2D-4C1C-AC20-23D3FDD31A9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C310C318-7B2D-4C1C-AC20-23D3FDD31A9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C310C318-7B2D-4C1C-AC20-23D3FDD31A9E}.Release|Any CPU.Build.0 = Release|Any CPU
{91495899-7D40-4543-93B9-820F92C61B31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91495899-7D40-4543-93B9-820F92C61B31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91495899-7D40-4543-93B9-820F92C61B31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91495899-7D40-4543-93B9-820F92C61B31}.Release|Any CPU.Build.0 = Release|Any CPU
{FAC8701B-6897-488A-B78F-660E8B47FC25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FAC8701B-6897-488A-B78F-660E8B47FC25}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAC8701B-6897-488A-B78F-660E8B47FC25}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FAC8701B-6897-488A-B78F-660E8B47FC25}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 2 additions & 0 deletions algorithms-cs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin
/obj
30 changes: 30 additions & 0 deletions algorithms-cs/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using algorithms_cs.Algorithm.Sort.External.SeriesMerge;

const int numberWays = 4;
const string pathFile = "@\\algorithms-cs\\resource\\4.txt";

Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);

var sort = new MultiwaySort(numberWays, pathFile);
// sort.Start();



//
// var test = new BufferedTapeReader(pathFile);
// var test2 = new TapeCollection(pathFile);
// var ss = new SeriesEnumerable(test);
//
// foreach (var VARIABLE in test2)
// {
// Console.WriteLine(VARIABLE);
// }
//
// while (!ss.IsEnd)
// {
// foreach (var VARIABLE in ss)
// {
// Console.WriteLine($"{VARIABLE.GetType()} {VARIABLE.GetValue()}");
// }
// Console.WriteLine();
// }
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<RootNamespace>algorithms_cs</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using System.Diagnostics;
using algorithms_cs.Serial;
using algorithms_cs.Utils;
using algorithms_cs.Utils.Serial;

namespace algorithms_cs.Algorithm.Sort.External.Merge;
namespace algorithms_cs.Algorithm.Sort.External.SeriesMerge;

internal struct MinReturn
{
public SeriesReturn<double> Element;
public UtilReturn<double> Element;
public int Index;
}

public class Collector
{
private readonly List<Series> _seriesCollection;
private readonly List<SeriesReturn<double>> _bufferSeriesReturns;
private readonly List<UtilReturn<double>> _bufferSeriesReturns;

public Collector(List<Series> seriesCollection)
{
_seriesCollection = seriesCollection;
_bufferSeriesReturns = new List<SeriesReturn<double>>();
_bufferSeriesReturns = new List<UtilReturn<double>>();
foreach (var seriesReturn in _seriesCollection)
{
_bufferSeriesReturns.Add(seriesReturn.Next());
Expand All @@ -36,7 +36,7 @@ private MinReturn Min()

for (var i = 0; i < _bufferSeriesReturns.Count; i++)
{
if (_bufferSeriesReturns[i].GetType() == SeriesReturnType.Correct
if (_bufferSeriesReturns[i].GetType() == UtilReturnType.Correct
&& value > _bufferSeriesReturns[i].GetValue())
{
value = _bufferSeriesReturns[i].GetValue();
Expand All @@ -48,16 +48,16 @@ private MinReturn Min()
return minReturn;
}

private bool IsEmpty() => _bufferSeriesReturns.All(x => x.GetType() != SeriesReturnType.Correct);
private bool IsEmpty() => _bufferSeriesReturns.All(x => x.GetType() != UtilReturnType.Correct);

public bool TapeEnded() => _bufferSeriesReturns.All(x => x.GetType() == SeriesReturnType.TapeEnded);
public bool TapeEnded() => _bufferSeriesReturns.All(x => x.GetType() == UtilReturnType.TapeEnded);

public SeriesReturn<double> Next()
public UtilReturn<double> Next()
{
// Returns minimal Correct value from multiple Series, else returns SeriesEnd

var seriesEnded = IsEmpty();
if (seriesEnded) return new SeriesReturn<double>(SeriesReturnType.SeriesEnded);
if (seriesEnded) return new UtilReturn<double>(UtilReturnType.SeriesEnded);

var minElement = Min();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using algorithms_cs.Serial;
using algorithms_cs.Tape;
using algorithms_cs.Utils;
using algorithms_cs.Utils.Serial;
using algorithms_cs.Utils.Tape;

namespace algorithms_cs.Algorithm.Sort.External.Merge;
namespace algorithms_cs.Algorithm.Sort.External.SeriesMerge;

public class MultiwaySort: Sort
public class MultiwaySort
{
private readonly string _sourceFilePath;
private readonly string _tempDirectory;
private readonly int _n;
private RotatingDominoes _dominoes;
private List<Tape.Tape> _tapes;
private List<Tape> _tapes;
private readonly string _templateNameFiles;

public MultiwaySort(int N, string sourceFilePath)
Expand All @@ -25,7 +26,7 @@ public MultiwaySort(int N, string sourceFilePath)
_templateNameFiles = _tempDirectory + "tempfile-{0}.multiwaymergesort";
Directory.CreateDirectory(_tempDirectory);

_tapes = new List<Tape.Tape>();
_tapes = new List<Tape>();
_n = N;

var firstFilenames = new List<string>();
Expand Down Expand Up @@ -57,7 +58,7 @@ public void Start()

private void TrashDelete(string outFilename)
{
File.Copy(outFilename, _sourceFilePath+"-sorted");
if (!File.Exists(_sourceFilePath+"-sorted")) File.Copy(outFilename, _sourceFilePath+"-sorted");
foreach (var filename in _dominoes.ReadFilenames)
{
File.Delete(filename);
Expand All @@ -76,26 +77,25 @@ private void InitTapeSplit()
var tapeWrites = _dominoes.WriteFilenames.Select(filename => new TapeWriter<double>(filename)).ToList();

var indexTape = 0;
if (indexTape >= _n) throw new IndexOutOfRangeException("indexTape out of range tapeWrites");

var s1 = new Series(initTape);
SeriesReturn<double> value;
UtilReturn<double> value;

do
{
do
{
value = s1.Next();
if (value.GetType() == SeriesReturnType.Correct)
if (value.GetType() == UtilReturnType.Correct)
{
tapeWrites[indexTape].Write(value.GetValue());
}
} while (value.GetType() == SeriesReturnType.Correct);
} while (value.GetType() == UtilReturnType.Correct);

indexTape += 1;
if (indexTape >= _n) indexTape = 0;
s1 = new Series(initTape);
} while (value?.GetType() != SeriesReturnType.TapeEnded);
} while (value?.GetType() != UtilReturnType.TapeEnded);

foreach (var tape in tapeWrites)
{
Expand Down Expand Up @@ -131,17 +131,17 @@ private int MainRound()
var multipleSeries = new List<Series>();
multipleSeries.AddRange(readTapes.Select(tape => new Series(tape)));
var collector = new Collector(multipleSeries);
SeriesReturn<double> value;
UtilReturn<double> value;
do
{
do
{
value = collector.Next();
if (value.GetType() == SeriesReturnType.Correct)
if (value.GetType() == UtilReturnType.Correct)
{
writeTapes[indexTape].Write(value.GetValue());
}
} while (value.GetType() == SeriesReturnType.Correct);
} while (value.GetType() == UtilReturnType.Correct);

count++;
if (count > 2) count = 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace algorithms_cs.Algorithm.Sort.External.Merge;
namespace algorithms_cs.Algorithm.Sort.External.SeriesMerge;

public class RotatingDominoes
{
Expand Down
Loading