From 116db3f40c3b9837a8a5a0ce24797956526d81ea Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 16 Mar 2023 22:09:41 +0300 Subject: [PATCH 1/6] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewBor/NewBor/Program.cs | 1 + 1 file changed, 1 insertion(+) create mode 100644 NewBor/NewBor/Program.cs diff --git a/NewBor/NewBor/Program.cs b/NewBor/NewBor/Program.cs new file mode 100644 index 0000000..3d6b713 --- /dev/null +++ b/NewBor/NewBor/Program.cs @@ -0,0 +1 @@ +Console.WriteLine("Get started with Bor, a class created specifically for easy handling of strings!"); \ No newline at end of file From 8c8e55c0eefe9c7bff64bb15aee9812021d0ca08 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 16 Mar 2023 22:16:32 +0300 Subject: [PATCH 2/6] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=83=D1=8E=20=D0=B2=D0=B5=D0=BA=D1=82=D1=83?= =?UTF-8?q?=20=D0=B8=20=D1=82=D1=83=D0=B4=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=D1=91=D1=81=20=D0=B1=D0=BE=D1=80=20+=20=D1=80=D0=B5?= =?UTF-8?q?=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewBor/BorTests/BorTests.cs | 79 +++++++++++++++++ NewBor/NewBor.sln | 31 +++++++ NewBor/NewBor/Bor.cs | 169 ++++++++++++++++++++++++++++++++++++ 3 files changed, 279 insertions(+) create mode 100644 NewBor/BorTests/BorTests.cs create mode 100644 NewBor/NewBor.sln create mode 100644 NewBor/NewBor/Bor.cs diff --git a/NewBor/BorTests/BorTests.cs b/NewBor/BorTests/BorTests.cs new file mode 100644 index 0000000..699ad4a --- /dev/null +++ b/NewBor/BorTests/BorTests.cs @@ -0,0 +1,79 @@ +namespace Bor; + +public class Tests +{ + [SetUp] + public void Setup() + { + + } + + Bor bor; + + [Test] + public void TestInitialize() + { + bor = new Bor(); + } + + [Test] + public void TheAddedElementShouldbeFoundInBor() + { + TestInitialize(); + bor.Add("end"); + Assert.That(bor.Contains("end"), "Problems with the addition test!\n"); + } + + [Test] + public void ByAddingAndRemovingAnElementItShouldNotStayInBor() + { + TestInitialize(); + bor.Add("end"); + bor.Remove("end"); + Assert.That(!bor.Contains("end"), "Problems with the deletion test!\n"); + } + + [Test] + public void AddTwoStringsWithTheSamePrefixBorTheNumberOfStringsWithThisPrefixIsTwo() + { + TestInitialize(); + bor.Add("endProgram"); + bor.Add("endFunction"); + Assert.That(bor.HowManyStartsWithPrefix("end") == 2, "Problems with the prefix test!"); + } + + [Test] + public void WhenEnteringDifferentStringsTheNumberOfStringsWithTheSamePrefixNotZeroMustBeOne() + { + TestInitialize(); + bor.Add("aaaaa"); + bor.Add("bbbbb"); + bor.Add("ccccc"); + Assert.That(bor.HowManyStartsWithPrefix("a") == 1, "Problems with the prefix test!"); + } + + [Test] + public void TheAnswerToANoExistentPrefixInTheBorShouldBeOne() + { + TestInitialize(); + bor.Add("adadasd"); + Assert.That(bor.HowManyStartsWithPrefix("dsdsd") == 0, "Problems with non-existent prefix!"); + } + + [Test] + public void AnEmptyPrefixShouldGiveOuAllTheLinesInTheBor() + { + TestInitialize(); + bor.Add("adads"); + bor.Add("ddsds"); + bor.Add("End"); + Assert.That(bor.HowManyStartsWithPrefix("") == 3, "Problems with null prefix!"); + } + + [Test] + public void EmptyBorShouldGiveZeroStringsInBor() + { + TestInitialize(); + Assert.That(bor.HowManyStartsWithPrefix("") == 0, "Problems with null bor"); + } +} \ No newline at end of file diff --git a/NewBor/NewBor.sln b/NewBor/NewBor.sln new file mode 100644 index 0000000..50e9c32 --- /dev/null +++ b/NewBor/NewBor.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewBor", "NewBor\NewBor.csproj", "{FB45EC9E-CE6D-43B8-8E3C-A5ED346FB2F7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BorTests", "BorTests\BorTests.csproj", "{7D93AA52-05D4-42D2-96A9-713F239D88E9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FB45EC9E-CE6D-43B8-8E3C-A5ED346FB2F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FB45EC9E-CE6D-43B8-8E3C-A5ED346FB2F7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FB45EC9E-CE6D-43B8-8E3C-A5ED346FB2F7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FB45EC9E-CE6D-43B8-8E3C-A5ED346FB2F7}.Release|Any CPU.Build.0 = Release|Any CPU + {7D93AA52-05D4-42D2-96A9-713F239D88E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D93AA52-05D4-42D2-96A9-713F239D88E9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D93AA52-05D4-42D2-96A9-713F239D88E9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D93AA52-05D4-42D2-96A9-713F239D88E9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3285C05D-C82C-458A-B8A8-997DE3F629C2} + EndGlobalSection +EndGlobal diff --git a/NewBor/NewBor/Bor.cs b/NewBor/NewBor/Bor.cs new file mode 100644 index 0000000..44cb43b --- /dev/null +++ b/NewBor/NewBor/Bor.cs @@ -0,0 +1,169 @@ +namespace Bor; + +// A container for storing strings, in the form of a suspended tree +public class Bor +{ + private const int alphabetSize = 65536; + + private BorElement root = new BorElement(); + + // Adding an element + public bool Add(string element) + { + if (root == null) + { + throw new InvalidOperationException(); + } + if (element == null && !root.IsTerminal) + { + root.HowManyStringInDictionary++; + root.IsTerminal = true; + return true; + } + if (element != null) + { + root.HowManyStringInDictionary++; + } + if (element == null) + { + return false; + } + var walker = root; + int i = 0; + while (i < element.Length) + { + int number = (int)element[i]; + if (!walker.Next.ContainsKey(number)) + { + walker.Next.Add(number, new BorElement()); + ++walker.SizeDictionary; + } + ++walker.Next[number].HowManyStringInDictionary; + walker = walker.Next[number]; + i++; + } + return walker.IsTerminal == false ? walker.IsTerminal = true && true : false; + } + + private bool RemoveHelp(BorElement walker, string element, int position, ref bool isDeleted) + { + if (position == element.Length) + { + if (walker.IsTerminal) + { + walker.IsTerminal = false; + return true; + } + return false; + } + + if (walker.Next.ContainsKey(element[position])) + { + bool isCorrect = RemoveHelp(walker.Next[element[position]], element, position + 1, ref isDeleted); + if (!isCorrect) + { + return false; + } + if (walker.Next[element[position]].HowManyStringInDictionary == 1) + { + walker.Next.Remove(element[position]); + isDeleted = true; + return true; + } + if (isDeleted == true) + { + --walker.Next[element[position]].SizeDictionary; + isDeleted = false; + } + --walker.Next[element[position]].HowManyStringInDictionary; + } + else + { + return false; + } + return true; + } + + // Deleting an element in the tree + public bool Remove(string element) + { + if (root == null) + { + throw new InvalidOperationException(); + } + if (element == null) + { + root.IsTerminal = false; + return true; + } + var walker = root; + bool flag = false; + if (RemoveHelp(walker, element, 0, ref flag)) + { + --root.HowManyStringInDictionary; + return true; + } + return false; + } + + // Counts the number of rows with the same prefix + public int HowManyStartsWithPrefix(String prefix) + { + if (root == null) + { + if (prefix == null) + { + return 1; + } + return 0; + } + var walker = root; + int i = 0; + while (i < prefix.Length) + { + if (!walker.Next.ContainsKey(prefix[i])) + { + return 0; + } + walker = walker.Next[prefix[i]]; + ++i; + } + return walker.HowManyStringInDictionary; + } + + // Checks for the presence of a string + public bool Contains(string element) + { + if (root == null) + { + return element == null; + } + var walker = root; + int i = 0; + while (i < element.Length) + { + if (!walker.Next.ContainsKey(element[i])) + { + return false; + } + walker = walker.Next[element[i]]; + ++i; + } + return true; + } + + private class BorElement + { + public Dictionary Next { get; set; } + public bool IsTerminal { get; set; } + + public int SizeDictionary { get; set; } + + public int HowManyStringInDictionary { get; set; } + + public BorElement() + { + Next = new Dictionary(); + } + } +} \ No newline at end of file From c523e6af14504d5ae21d55e5876c06b7966059a9 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 31 Mar 2023 09:16:40 +0300 Subject: [PATCH 3/6] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D1=91=D1=81?= =?UTF-8?q?=20=D0=B2=D1=81=D1=91=20=D0=B2=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82,=20+csproj,=20+usings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewBor/NewBor.sln | 31 ------------------- NewBor/NewBor/Program.cs | 1 - NewBorForCI/NewBorForCI.sln | 31 +++++++++++++++++++ .../NewBor => NewBorForCI/NewBorForCI}/Bor.cs | 0 NewBorForCI/NewBorForCI/NewBorForCI.csproj | 10 ++++++ NewBorForCI/NewBorForCI/Program.cs | 11 +++++++ .../TestsBor/TestsBor.cs | 0 NewBorForCI/TestsBor/TestsBor.csproj | 23 ++++++++++++++ NewBorForCI/TestsBor/Usings.cs | 1 + 9 files changed, 76 insertions(+), 32 deletions(-) delete mode 100644 NewBor/NewBor.sln delete mode 100644 NewBor/NewBor/Program.cs create mode 100644 NewBorForCI/NewBorForCI.sln rename {NewBor/NewBor => NewBorForCI/NewBorForCI}/Bor.cs (100%) create mode 100644 NewBorForCI/NewBorForCI/NewBorForCI.csproj create mode 100644 NewBorForCI/NewBorForCI/Program.cs rename NewBor/BorTests/BorTests.cs => NewBorForCI/TestsBor/TestsBor.cs (100%) create mode 100644 NewBorForCI/TestsBor/TestsBor.csproj create mode 100644 NewBorForCI/TestsBor/Usings.cs diff --git a/NewBor/NewBor.sln b/NewBor/NewBor.sln deleted file mode 100644 index 50e9c32..0000000 --- a/NewBor/NewBor.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.4.33403.182 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewBor", "NewBor\NewBor.csproj", "{FB45EC9E-CE6D-43B8-8E3C-A5ED346FB2F7}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BorTests", "BorTests\BorTests.csproj", "{7D93AA52-05D4-42D2-96A9-713F239D88E9}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {FB45EC9E-CE6D-43B8-8E3C-A5ED346FB2F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FB45EC9E-CE6D-43B8-8E3C-A5ED346FB2F7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FB45EC9E-CE6D-43B8-8E3C-A5ED346FB2F7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FB45EC9E-CE6D-43B8-8E3C-A5ED346FB2F7}.Release|Any CPU.Build.0 = Release|Any CPU - {7D93AA52-05D4-42D2-96A9-713F239D88E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7D93AA52-05D4-42D2-96A9-713F239D88E9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7D93AA52-05D4-42D2-96A9-713F239D88E9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7D93AA52-05D4-42D2-96A9-713F239D88E9}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {3285C05D-C82C-458A-B8A8-997DE3F629C2} - EndGlobalSection -EndGlobal diff --git a/NewBor/NewBor/Program.cs b/NewBor/NewBor/Program.cs deleted file mode 100644 index 3d6b713..0000000 --- a/NewBor/NewBor/Program.cs +++ /dev/null @@ -1 +0,0 @@ -Console.WriteLine("Get started with Bor, a class created specifically for easy handling of strings!"); \ No newline at end of file diff --git a/NewBorForCI/NewBorForCI.sln b/NewBorForCI/NewBorForCI.sln new file mode 100644 index 0000000..9e8056e --- /dev/null +++ b/NewBorForCI/NewBorForCI.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewBorForCI", "NewBorForCI\NewBorForCI.csproj", "{9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsBor", "TestsBor\TestsBor.csproj", "{8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}.Release|Any CPU.Build.0 = Release|Any CPU + {8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {22B84F78-A6D8-4810-95B6-A79AEF403E7F} + EndGlobalSection +EndGlobal diff --git a/NewBor/NewBor/Bor.cs b/NewBorForCI/NewBorForCI/Bor.cs similarity index 100% rename from NewBor/NewBor/Bor.cs rename to NewBorForCI/NewBorForCI/Bor.cs diff --git a/NewBorForCI/NewBorForCI/NewBorForCI.csproj b/NewBorForCI/NewBorForCI/NewBorForCI.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/NewBorForCI/NewBorForCI/NewBorForCI.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/NewBorForCI/NewBorForCI/Program.cs b/NewBorForCI/NewBorForCI/Program.cs new file mode 100644 index 0000000..ac84d09 --- /dev/null +++ b/NewBorForCI/NewBorForCI/Program.cs @@ -0,0 +1,11 @@ +namespace Bor; + +using System; + +class Program +{ + public static void Main() + { + + } +} \ No newline at end of file diff --git a/NewBor/BorTests/BorTests.cs b/NewBorForCI/TestsBor/TestsBor.cs similarity index 100% rename from NewBor/BorTests/BorTests.cs rename to NewBorForCI/TestsBor/TestsBor.cs diff --git a/NewBorForCI/TestsBor/TestsBor.csproj b/NewBorForCI/TestsBor/TestsBor.csproj new file mode 100644 index 0000000..bd2f697 --- /dev/null +++ b/NewBorForCI/TestsBor/TestsBor.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/NewBorForCI/TestsBor/Usings.cs b/NewBorForCI/TestsBor/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/NewBorForCI/TestsBor/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file From 288aa90fce7e9902d23d951a2ed1d3afd609e758 Mon Sep 17 00:00:00 2001 From: Artem Date: Sun, 14 May 2023 21:40:50 +0300 Subject: [PATCH 4/6] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewBorForCI/NewBorForCI.sln | 4 +- NewBorForCI/NewBorForCI/Bor.cs | 169 ------------------ NewBorForCI/NewBorForCI/NewBorForCI.csproj | 2 +- NewBorForCI/NewBorForCI/Program.cs | 11 -- NewBorForCI/TestsBor/TestsBor.cs | 79 -------- NewBorForCI/TestsBor/TestsTrie.cs | 77 ++++++++ .../{TestsBor.csproj => TestsTrie.csproj} | 0 7 files changed, 80 insertions(+), 262 deletions(-) delete mode 100644 NewBorForCI/NewBorForCI/Bor.cs delete mode 100644 NewBorForCI/NewBorForCI/Program.cs delete mode 100644 NewBorForCI/TestsBor/TestsBor.cs create mode 100644 NewBorForCI/TestsBor/TestsTrie.cs rename NewBorForCI/TestsBor/{TestsBor.csproj => TestsTrie.csproj} (100%) diff --git a/NewBorForCI/NewBorForCI.sln b/NewBorForCI/NewBorForCI.sln index 9e8056e..87768e0 100644 --- a/NewBorForCI/NewBorForCI.sln +++ b/NewBorForCI/NewBorForCI.sln @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33403.182 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewBorForCI", "NewBorForCI\NewBorForCI.csproj", "{9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewBorForCI", "NewBorForCI\NewBorForCI.csproj", "{9A77CDBE-D0EE-4CED-B5B7-92B3567E6BAE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsBor", "TestsBor\TestsBor.csproj", "{8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestsTrie", "TestsBor\TestsTrie.csproj", "{8ECB64A5-1B15-4C3F-85A6-34DDE66CE741}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/NewBorForCI/NewBorForCI/Bor.cs b/NewBorForCI/NewBorForCI/Bor.cs deleted file mode 100644 index 44cb43b..0000000 --- a/NewBorForCI/NewBorForCI/Bor.cs +++ /dev/null @@ -1,169 +0,0 @@ -namespace Bor; - -// A container for storing strings, in the form of a suspended tree -public class Bor -{ - private const int alphabetSize = 65536; - - private BorElement root = new BorElement(); - - // Adding an element - public bool Add(string element) - { - if (root == null) - { - throw new InvalidOperationException(); - } - if (element == null && !root.IsTerminal) - { - root.HowManyStringInDictionary++; - root.IsTerminal = true; - return true; - } - if (element != null) - { - root.HowManyStringInDictionary++; - } - if (element == null) - { - return false; - } - var walker = root; - int i = 0; - while (i < element.Length) - { - int number = (int)element[i]; - if (!walker.Next.ContainsKey(number)) - { - walker.Next.Add(number, new BorElement()); - ++walker.SizeDictionary; - } - ++walker.Next[number].HowManyStringInDictionary; - walker = walker.Next[number]; - i++; - } - return walker.IsTerminal == false ? walker.IsTerminal = true && true : false; - } - - private bool RemoveHelp(BorElement walker, string element, int position, ref bool isDeleted) - { - if (position == element.Length) - { - if (walker.IsTerminal) - { - walker.IsTerminal = false; - return true; - } - return false; - } - - if (walker.Next.ContainsKey(element[position])) - { - bool isCorrect = RemoveHelp(walker.Next[element[position]], element, position + 1, ref isDeleted); - if (!isCorrect) - { - return false; - } - if (walker.Next[element[position]].HowManyStringInDictionary == 1) - { - walker.Next.Remove(element[position]); - isDeleted = true; - return true; - } - if (isDeleted == true) - { - --walker.Next[element[position]].SizeDictionary; - isDeleted = false; - } - --walker.Next[element[position]].HowManyStringInDictionary; - } - else - { - return false; - } - return true; - } - - // Deleting an element in the tree - public bool Remove(string element) - { - if (root == null) - { - throw new InvalidOperationException(); - } - if (element == null) - { - root.IsTerminal = false; - return true; - } - var walker = root; - bool flag = false; - if (RemoveHelp(walker, element, 0, ref flag)) - { - --root.HowManyStringInDictionary; - return true; - } - return false; - } - - // Counts the number of rows with the same prefix - public int HowManyStartsWithPrefix(String prefix) - { - if (root == null) - { - if (prefix == null) - { - return 1; - } - return 0; - } - var walker = root; - int i = 0; - while (i < prefix.Length) - { - if (!walker.Next.ContainsKey(prefix[i])) - { - return 0; - } - walker = walker.Next[prefix[i]]; - ++i; - } - return walker.HowManyStringInDictionary; - } - - // Checks for the presence of a string - public bool Contains(string element) - { - if (root == null) - { - return element == null; - } - var walker = root; - int i = 0; - while (i < element.Length) - { - if (!walker.Next.ContainsKey(element[i])) - { - return false; - } - walker = walker.Next[element[i]]; - ++i; - } - return true; - } - - private class BorElement - { - public Dictionary Next { get; set; } - public bool IsTerminal { get; set; } - - public int SizeDictionary { get; set; } - - public int HowManyStringInDictionary { get; set; } - - public BorElement() - { - Next = new Dictionary(); - } - } -} \ No newline at end of file diff --git a/NewBorForCI/NewBorForCI/NewBorForCI.csproj b/NewBorForCI/NewBorForCI/NewBorForCI.csproj index f02677b..dc2bb05 100644 --- a/NewBorForCI/NewBorForCI/NewBorForCI.csproj +++ b/NewBorForCI/NewBorForCI/NewBorForCI.csproj @@ -1,7 +1,7 @@ - Exe + Library net7.0 enable enable diff --git a/NewBorForCI/NewBorForCI/Program.cs b/NewBorForCI/NewBorForCI/Program.cs deleted file mode 100644 index ac84d09..0000000 --- a/NewBorForCI/NewBorForCI/Program.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Bor; - -using System; - -class Program -{ - public static void Main() - { - - } -} \ No newline at end of file diff --git a/NewBorForCI/TestsBor/TestsBor.cs b/NewBorForCI/TestsBor/TestsBor.cs deleted file mode 100644 index 699ad4a..0000000 --- a/NewBorForCI/TestsBor/TestsBor.cs +++ /dev/null @@ -1,79 +0,0 @@ -namespace Bor; - -public class Tests -{ - [SetUp] - public void Setup() - { - - } - - Bor bor; - - [Test] - public void TestInitialize() - { - bor = new Bor(); - } - - [Test] - public void TheAddedElementShouldbeFoundInBor() - { - TestInitialize(); - bor.Add("end"); - Assert.That(bor.Contains("end"), "Problems with the addition test!\n"); - } - - [Test] - public void ByAddingAndRemovingAnElementItShouldNotStayInBor() - { - TestInitialize(); - bor.Add("end"); - bor.Remove("end"); - Assert.That(!bor.Contains("end"), "Problems with the deletion test!\n"); - } - - [Test] - public void AddTwoStringsWithTheSamePrefixBorTheNumberOfStringsWithThisPrefixIsTwo() - { - TestInitialize(); - bor.Add("endProgram"); - bor.Add("endFunction"); - Assert.That(bor.HowManyStartsWithPrefix("end") == 2, "Problems with the prefix test!"); - } - - [Test] - public void WhenEnteringDifferentStringsTheNumberOfStringsWithTheSamePrefixNotZeroMustBeOne() - { - TestInitialize(); - bor.Add("aaaaa"); - bor.Add("bbbbb"); - bor.Add("ccccc"); - Assert.That(bor.HowManyStartsWithPrefix("a") == 1, "Problems with the prefix test!"); - } - - [Test] - public void TheAnswerToANoExistentPrefixInTheBorShouldBeOne() - { - TestInitialize(); - bor.Add("adadasd"); - Assert.That(bor.HowManyStartsWithPrefix("dsdsd") == 0, "Problems with non-existent prefix!"); - } - - [Test] - public void AnEmptyPrefixShouldGiveOuAllTheLinesInTheBor() - { - TestInitialize(); - bor.Add("adads"); - bor.Add("ddsds"); - bor.Add("End"); - Assert.That(bor.HowManyStartsWithPrefix("") == 3, "Problems with null prefix!"); - } - - [Test] - public void EmptyBorShouldGiveZeroStringsInBor() - { - TestInitialize(); - Assert.That(bor.HowManyStartsWithPrefix("") == 0, "Problems with null bor"); - } -} \ No newline at end of file diff --git a/NewBorForCI/TestsBor/TestsTrie.cs b/NewBorForCI/TestsBor/TestsTrie.cs new file mode 100644 index 0000000..a031e3b --- /dev/null +++ b/NewBorForCI/TestsBor/TestsTrie.cs @@ -0,0 +1,77 @@ +namespace Trie; + +public class Tests +{ + private Trie? trie; + + [Test] + public void TestInitialize() + { + trie = new(); + if (trie == null) + { + throw new NullReferenceException(); + } + } + + [Test] + public void TheAddedElementShouldbeFoundInBor() + { + TestInitialize(); + trie.Add("end"); + Assert.True(trie.Contains("end")); + } + + [Test] + public void ByAddingAndRemovingAnElementItShouldNotStayInBor() + { + TestInitialize(); + trie.Add("end"); + trie.Remove("end"); + Assert.False(trie.Contains("end")); + } + + [Test] + public void AddTwoStringsWithTheSamePrefixBorTheNumberOfStringsWithThisPrefixIsTwo() + { + TestInitialize(); + trie.Add("endProgram"); + trie.Add("endFunction"); + Assert.That(trie.HowManyStartsWithPrefix("end") == 2); + } + + [Test] + public void WhenEnteringDifferentStringsTheNumberOfStringsWithTheSamePrefixNotZeroMustBeOne() + { + TestInitialize(); + trie.Add("aaaaa"); + trie.Add("bbbbb"); + trie.Add("ccccc"); + Assert.That(trie.HowManyStartsWithPrefix("a") == 1); + } + + [Test] + public void TheAnswerToANoExistentPrefixInTheBorShouldBeOne() + { + TestInitialize(); + trie.Add("adadasd"); + Assert.That(trie.HowManyStartsWithPrefix("dsdsd") == 0); + } + + [Test] + public void AnEmptyPrefixShouldGiveOuAllTheLinesInTheBor() + { + TestInitialize(); + trie.Add("adads"); + trie.Add("ddsds"); + trie.Add("End"); + Assert.That(trie.HowManyStartsWithPrefix("") == 3); + } + + [Test] + public void EmptyBorShouldGiveZeroStringsInBor() + { + TestInitialize(); + Assert.That(trie.HowManyStartsWithPrefix("") == 0); + } +} \ No newline at end of file diff --git a/NewBorForCI/TestsBor/TestsBor.csproj b/NewBorForCI/TestsBor/TestsTrie.csproj similarity index 100% rename from NewBorForCI/TestsBor/TestsBor.csproj rename to NewBorForCI/TestsBor/TestsTrie.csproj From b2ab7ee8c586700ec1769af4c57b82df8d264dd7 Mon Sep 17 00:00:00 2001 From: Artem Date: Tue, 23 May 2023 15:37:38 +0300 Subject: [PATCH 5/6] =?UTF-8?q?+=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewBorForCI/NewBorForCI/Trie.cs | 157 ++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 NewBorForCI/NewBorForCI/Trie.cs diff --git a/NewBorForCI/NewBorForCI/Trie.cs b/NewBorForCI/NewBorForCI/Trie.cs new file mode 100644 index 0000000..1b6293a --- /dev/null +++ b/NewBorForCI/NewBorForCI/Trie.cs @@ -0,0 +1,157 @@ +namespace Trie; + +// A container for storing strings, in the form of a suspended tree +public class Trie +{ + private const int alphabetSize = 65536; + + private TrieElement root = new(); + + // Adding an element + public bool Add(string element) + { + if (element == null) + { + throw new ArgumentNullException(); + } + root.NumberOfLinesInTheDictionary++; + var walker = root; + int i = 0; + while (i < element.Length) + { + char number = element[i]; + if (!walker.Next.ContainsKey(number)) + { + walker.Next.Add(number, new TrieElement()); + ++walker.DictionarySize; + } + ++walker.Next[number].NumberOfLinesInTheDictionary; + walker = walker.Next[number]; + i++; + } + if (walker.IsTerminal == false) + { + walker.IsTerminal = true; + return true; + } + return false; + } + + private bool RemoveHelp(TrieElement walker, string element, int position, ref bool isDeleted) + { + if (position == element.Length) + { + if (walker.IsTerminal) + { + walker.IsTerminal = false; + return true; + } + return false; + } + + if (walker.Next.ContainsKey(element[position])) + { + bool isCorrect = RemoveHelp(walker.Next[element[position]], element, position + 1, ref isDeleted); + if (!isCorrect) + { + return false; + } + if (walker.Next[element[position]].NumberOfLinesInTheDictionary == 1) + { + walker.Next.Remove(element[position]); + isDeleted = true; + return true; + } + if (isDeleted) + { + --walker.Next[element[position]].DictionarySize; + isDeleted = false; + } + --walker.Next[element[position]].NumberOfLinesInTheDictionary; + } + else + { + return false; + } + return true; + } + + // Deleting an element in the tree + public bool Remove(string element) + { + if (root == null) + { + throw new InvalidOperationException(); + } + if (element == null) + { + root.IsTerminal = false; + return true; + } + var walker = root; + bool flag = false; + if (RemoveHelp(walker, element, 0, ref flag)) + { + --root.NumberOfLinesInTheDictionary; + return true; + } + return false; + } + + // Counts the number of rows with the same prefix + public int HowManyStartsWithPrefix(string prefix) + { + if (root == null) + { + return prefix == null ? 1 : 0; + } + var walker = root; + int i = 0; + while (i < prefix.Length) + { + if (!walker.Next.ContainsKey(prefix[i])) + { + return 0; + } + walker = walker.Next[prefix[i]]; + ++i; + } + return walker.NumberOfLinesInTheDictionary; + } + + // Checks for the presence of a string + public bool Contains(string element) + { + if (root == null) + { + return element == null; + } + var walker = root; + int i = 0; + while (i < element.Length) + { + if (!walker.Next.ContainsKey(element[i])) + { + return false; + } + walker = walker.Next[element[i]]; + ++i; + } + return true; + } + + private class TrieElement + { + public Dictionary Next { get; set; } + public bool IsTerminal { get; set; } + + public int DictionarySize { get; set; } + + public int NumberOfLinesInTheDictionary { get; set; } + + public TrieElement() + { + Next = new Dictionary(); + } + } +} \ No newline at end of file From 14b807fb2582249964eb3b3a20aad1a9208d3539 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 26 May 2023 12:41:34 +0300 Subject: [PATCH 6/6] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewBorForCI/NewBorForCI/Trie.cs | 44 +++++++++++++------------------ NewBorForCI/TestsBor/TestsTrie.cs | 7 ++--- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/NewBorForCI/NewBorForCI/Trie.cs b/NewBorForCI/NewBorForCI/Trie.cs index 1b6293a..2fa0433 100644 --- a/NewBorForCI/NewBorForCI/Trie.cs +++ b/NewBorForCI/NewBorForCI/Trie.cs @@ -1,19 +1,16 @@ -namespace Trie; +using System.Xml.Linq; + +namespace Trie; // A container for storing strings, in the form of a suspended tree public class Trie { - private const int alphabetSize = 65536; - private TrieElement root = new(); // Adding an element public bool Add(string element) { - if (element == null) - { - throw new ArgumentNullException(); - } + ArgumentNullException.ThrowIfNull(element); root.NumberOfLinesInTheDictionary++; var walker = root; int i = 0; @@ -23,13 +20,12 @@ public bool Add(string element) if (!walker.Next.ContainsKey(number)) { walker.Next.Add(number, new TrieElement()); - ++walker.DictionarySize; } ++walker.Next[number].NumberOfLinesInTheDictionary; walker = walker.Next[number]; i++; } - if (walker.IsTerminal == false) + if (!walker.IsTerminal) { walker.IsTerminal = true; return true; @@ -64,16 +60,12 @@ private bool RemoveHelp(TrieElement walker, string element, int position, ref bo } if (isDeleted) { - --walker.Next[element[position]].DictionarySize; isDeleted = false; } --walker.Next[element[position]].NumberOfLinesInTheDictionary; + return true; } - else - { - return false; - } - return true; + return false; } // Deleting an element in the tree @@ -84,13 +76,18 @@ public bool Remove(string element) throw new InvalidOperationException(); } if (element == null) + { + throw new ArgumentNullException(); + } + + if (element == "") { root.IsTerminal = false; return true; } var walker = root; - bool flag = false; - if (RemoveHelp(walker, element, 0, ref flag)) + bool isDeleted = false; + if (RemoveHelp(walker, element, 0, ref isDeleted)) { --root.NumberOfLinesInTheDictionary; return true; @@ -101,10 +98,8 @@ public bool Remove(string element) // Counts the number of rows with the same prefix public int HowManyStartsWithPrefix(string prefix) { - if (root == null) - { - return prefix == null ? 1 : 0; - } + ArgumentNullException.ThrowIfNull(root); + var walker = root; int i = 0; while (i < prefix.Length) @@ -122,10 +117,8 @@ public int HowManyStartsWithPrefix(string prefix) // Checks for the presence of a string public bool Contains(string element) { - if (root == null) - { - return element == null; - } + ArgumentNullException.ThrowIfNull(root); + var walker = root; int i = 0; while (i < element.Length) @@ -145,7 +138,6 @@ private class TrieElement public Dictionary Next { get; set; } public bool IsTerminal { get; set; } - public int DictionarySize { get; set; } public int NumberOfLinesInTheDictionary { get; set; } diff --git a/NewBorForCI/TestsBor/TestsTrie.cs b/NewBorForCI/TestsBor/TestsTrie.cs index a031e3b..475f028 100644 --- a/NewBorForCI/TestsBor/TestsTrie.cs +++ b/NewBorForCI/TestsBor/TestsTrie.cs @@ -4,14 +4,11 @@ public class Tests { private Trie? trie; - [Test] + [SetUp] public void TestInitialize() { trie = new(); - if (trie == null) - { - throw new NullReferenceException(); - } + Assert.IsNotNull(trie); } [Test]