From d1fa02825b5e75eb33cff8fd0aa26316973ddf8f Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 5 May 2023 22:56:18 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D1=8B=D0=B9,=20=D0=BD=D0=BE=20=D0=BD=D0=B5=20=D0=BE?= =?UTF-8?q?=D1=82=D1=82=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B9=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ListWithOmissions/ListWithOmissions.sln | 31 ++ .../IncorrectIndexException.cs | 3 + .../IncorrectMethodException.cs | 3 + .../ListWithOmissions/ListWithOmissions.cs | 421 ++++++++++++++++++ .../ListWithOmissions.csproj | 10 + .../ListWithOmissions/Program.cs | 17 + .../TestsForListWithOmissions/TestsForList.cs | 32 ++ .../TestsForListWithOmissions.csproj | 23 + .../TestsForListWithOmissions/Usings.cs | 1 + 9 files changed, 541 insertions(+) create mode 100644 ListWithOmissions/ListWithOmissions.sln create mode 100644 ListWithOmissions/ListWithOmissions/IncorrectIndexException.cs create mode 100644 ListWithOmissions/ListWithOmissions/IncorrectMethodException.cs create mode 100644 ListWithOmissions/ListWithOmissions/ListWithOmissions.cs create mode 100644 ListWithOmissions/ListWithOmissions/ListWithOmissions.csproj create mode 100644 ListWithOmissions/ListWithOmissions/Program.cs create mode 100644 ListWithOmissions/TestsForListWithOmissions/TestsForList.cs create mode 100644 ListWithOmissions/TestsForListWithOmissions/TestsForListWithOmissions.csproj create mode 100644 ListWithOmissions/TestsForListWithOmissions/Usings.cs diff --git a/ListWithOmissions/ListWithOmissions.sln b/ListWithOmissions/ListWithOmissions.sln new file mode 100644 index 0000000..a54d4de --- /dev/null +++ b/ListWithOmissions/ListWithOmissions.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}") = "ListWithOmissions", "ListWithOmissions\ListWithOmissions.csproj", "{B8A5B7AD-4E88-408D-9DAF-4C9AEA647B80}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForListWithOmissions", "TestsForListWithOmissions\TestsForListWithOmissions.csproj", "{9C12CF7E-C6D4-488A-B1CF-E59FAB8B434D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B8A5B7AD-4E88-408D-9DAF-4C9AEA647B80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B8A5B7AD-4E88-408D-9DAF-4C9AEA647B80}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B8A5B7AD-4E88-408D-9DAF-4C9AEA647B80}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B8A5B7AD-4E88-408D-9DAF-4C9AEA647B80}.Release|Any CPU.Build.0 = Release|Any CPU + {9C12CF7E-C6D4-488A-B1CF-E59FAB8B434D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9C12CF7E-C6D4-488A-B1CF-E59FAB8B434D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9C12CF7E-C6D4-488A-B1CF-E59FAB8B434D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9C12CF7E-C6D4-488A-B1CF-E59FAB8B434D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C6C44DE7-A16E-4728-A3A3-CCA5C7B266F7} + EndGlobalSection +EndGlobal diff --git a/ListWithOmissions/ListWithOmissions/IncorrectIndexException.cs b/ListWithOmissions/ListWithOmissions/IncorrectIndexException.cs new file mode 100644 index 0000000..325ee26 --- /dev/null +++ b/ListWithOmissions/ListWithOmissions/IncorrectIndexException.cs @@ -0,0 +1,3 @@ +namespace ListWithOmissions; + +public class IncorrectIndexException : Exception {} \ No newline at end of file diff --git a/ListWithOmissions/ListWithOmissions/IncorrectMethodException.cs b/ListWithOmissions/ListWithOmissions/IncorrectMethodException.cs new file mode 100644 index 0000000..0716b6d --- /dev/null +++ b/ListWithOmissions/ListWithOmissions/IncorrectMethodException.cs @@ -0,0 +1,3 @@ +namespace ListWithOmissions; + +internal class IncorrectMethodException : Exception {} \ No newline at end of file diff --git a/ListWithOmissions/ListWithOmissions/ListWithOmissions.cs b/ListWithOmissions/ListWithOmissions/ListWithOmissions.cs new file mode 100644 index 0000000..ff5a751 --- /dev/null +++ b/ListWithOmissions/ListWithOmissions/ListWithOmissions.cs @@ -0,0 +1,421 @@ +using System.Collections; + +namespace ListWithOmissions; + +public class ListWithOmissions : IList where T : IComparable +{ + private class List + { + public ElementList? element; + } + + private class MainList + { + public MainList? nextList; + public List? list; + public int size; + } + + private MainList? MajorList; + + public void Add(T item) + { + if (MajorList == null) + { + MajorList = new MainList(); + MajorList.list = new List(); + MajorList.list.element = new ElementList(0, item, 0); + MajorList.size = 1; + return; + } + + if (MajorList.list == null || MajorList.list.element == null) + { + throw new NullReferenceException(); + } + + var walker = MajorList.list.element; + var stack = new Stack(); + + ++MajorList.size; + + while (walker != null) + { + if (walker.next == null) + { + if (walker.level == 0) + { + var copy = walker.next; + walker.next = new ElementList(walker.position + 1, item, 0); + walker.next.next = copy; + var randomNumber = new Random(); + int position = 1; + var previousLevelItem = walker.next; + while (randomNumber.Next(0, 2) == 0) + { + if (stack.Count > 0) + { + var walkerFromStack = stack.Pop(); + var copyForStack = walkerFromStack.next; + walkerFromStack.next = new ElementList(walker.position + 1, item, position); + walkerFromStack.next.next = copyForStack; + walkerFromStack.next.down = previousLevelItem; + previousLevelItem = walkerFromStack.next; + position++; + } + else + { + var newList = new List(); + newList.element = new ElementList(0, MajorList.list.element.value, MajorList.list.element.level + 1); + newList.element.next = new ElementList(walker.position + 1, item, position); + newList.element.next.down = previousLevelItem; + newList.element.down = MajorList.list.element; + previousLevelItem = newList.element.next; + var copyMajorList = MajorList; + MajorList = new MainList(); + MajorList.list = newList; + MajorList.nextList = copyMajorList; + MajorList.size = copyMajorList.size; + position++; + } + } + return; + } + else + { + stack.Push(walker); + walker = walker.down; + } + } + + if (walker != null && walker.next != null && item.CompareTo(walker.next.value) >= 0) + { + walker = walker.next; + } + else if (walker != null && walker.next != null && item.CompareTo(walker.next.value) < 0) + { + walker = walker.down; + } + } + } + + private T FindElementByIndex(int index) + { + if (MajorList == null || MajorList.list == null || MajorList.list.element == null) + { + throw new NullReferenceException(); + } + + var walker = MajorList.list.element; + while (walker != null) + { + if (walker != null && walker.position == index) + { + return walker.value; + } + if (walker != null && walker.next != null && index >= walker.next.position) + { + walker = walker.next; + } + else if (walker != null && walker.next != null && index <= walker.next.position) + { + walker = walker.down; + } + } + throw new IncorrectIndexException(); + } + + public T this[int index] { get => FindElementByIndex(index); set => throw new NotImplementedException(); } + + public int Count => MajorList == null ? throw new NullReferenceException() : MajorList.size; + + public bool IsReadOnly => false; + + + public void Clear() + { + MajorList = null; + } + + public bool Contains(T item) + { + if (MajorList == null || MajorList.list == null || MajorList.list.element == null) + { + return false; + } + + var walker = MajorList.list.element; + while (walker != null) + { + if (walker.level == 0) + { + return item.CompareTo(walker.value) == 0; + } + if (walker != null && walker.next != null && item.CompareTo(walker.next.value) >= 0) + { + walker = walker.next; + } + else if (walker != null && walker.next != null && item.CompareTo(walker.next.value) < 0) + { + walker = walker.down; + } + } + return false; + } + + public void CopyTo(T[] array, int arrayIndex) + { + if (MajorList == null || MajorList.list == null || MajorList.list.element == null) + { + throw new NullReferenceException(); + } + var walker = MajorList; + while (walker.nextList != null) + { + walker = walker.nextList; + } + + if (walker.list == null || walker.list.element == null) + { + throw new NullReferenceException(); + } + + var walkerForArray = walker.list.element; + var listArray = new List(array); + + while (walkerForArray != null) + { + listArray.Add(walkerForArray.value); + } + + array = listArray.ToArray(); + } + + private class ListEnum : IEnumerator + { + private T[] listEnum; + + int position = -1; + + public ListEnum(T[] list) + { + listEnum = list; + } + + public object Current + { + get + { + try + { + return listEnum[position]; + } + catch (IndexOutOfRangeException) + { + throw new IncorrectIndexException(); + } + } + + } + + T IEnumerator.Current + { + get + { + return (T)Current; + } + } + + public void Dispose() + { + GC.SuppressFinalize(this); + } + + public bool MoveNext() + { + position++; + return (position < listEnum.Length); + } + + public void Reset() + { + position = -1; + } + } + + public IEnumerator GetEnumerator() + { + if (MajorList == null || MajorList.list == null) + { + throw new NullReferenceException(); + } + var walker = MajorList; + while (walker.nextList != null) + { + walker = walker.nextList; + } + var arrayValue = new T[MajorList.size]; + + if (walker.list == null) + { + throw new NullReferenceException(); + } + + var walkerList = walker.list.element; + if (walkerList == null) + { + throw new NullReferenceException(); + } + int i = 0; + while (walkerList != null) + { + arrayValue[i] = walkerList.value; + ++i; + walkerList = walkerList.next; + } + return new ListEnum(arrayValue); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return (IEnumerator) GetEnumerator(); + } + + public int IndexOf(T item) + { + if (MajorList == null || MajorList.list == null || MajorList.list.element == null) + { + throw new NullReferenceException(); + } + + var walker = MajorList.list.element; + while (walker != null) + { + if (walker.level == 0) + { + return item.CompareTo(walker.value) == 0 ? walker.position : -1; + } + if (walker != null && walker.next != null && item.CompareTo(walker.next.value) >= 0) + { + walker = walker.next; + } + else if (walker != null && walker.next != null && item.CompareTo(walker.next.value) < 0) + { + walker = walker.down; + } + } + return -1; + } + + public void Insert(int index, T item) + { + throw new IncorrectMethodException(); + } + + public bool Remove(T item) + { + if (MajorList == null || MajorList.list == null || MajorList.list.element == null) + { + throw new NullReferenceException(); + } + var walker = MajorList.list.element; + var previousWalker = walker; + var mainLevel = MajorList; + while (walker != null) + { + if (item.CompareTo(walker.value) == 0) + { + var copy = walker.next; + if (previousWalker == null) + { + throw new NullReferenceException(); + } + previousWalker.next = copy; + walker = walker.down; + previousWalker = walker; + if (walker == null) + { + return true; + } + } + + if (walker != null && walker.next != null && item.CompareTo(walker.next.value) >= 0) + { + previousWalker = walker; + walker = walker.next; + } + else if (walker != null && walker.next != null && item.CompareTo(walker.next.value) < 0) + { + previousWalker = walker; + if (mainLevel == null) + { + throw new NullReferenceException(); + } + mainLevel = mainLevel.nextList; + walker = walker.down; + } + } + return false; + } + + public void RemoveAt(int index) + { + if (MajorList == null || MajorList.list == null || MajorList.list.element == null) + { + throw new NullReferenceException(); + } + var walker = MajorList.list.element; + var previousWalker = walker; + var mainLevel = MajorList; + while (walker != null) + { + if (index == walker.position) + { + var copy = walker.next; + if (previousWalker == null) + { + throw new NullReferenceException(); + } + previousWalker.next = copy; + walker = walker.down; + previousWalker = walker; + } + + if (walker != null && walker.next != null && walker.next.position <= index) + { + previousWalker = walker; + walker = walker.next; + } + else if (walker != null && walker.next != null && index < walker.next.position) + { + previousWalker = walker; + if (mainLevel == null) + { + throw new NullReferenceException(); + } + mainLevel = mainLevel.nextList; + walker = walker.down; + } + } + } + + private class ElementList + { + public ElementList(int index, T item, int standart) + { + value = item; + position = index; + level = standart; + } + + public ElementList? next { get; set; } + + public ElementList? down { get; set; } + + public T? value { get; set; } + + public int position { get; set; } + + public int level { get; set; } + } +} \ No newline at end of file diff --git a/ListWithOmissions/ListWithOmissions/ListWithOmissions.csproj b/ListWithOmissions/ListWithOmissions/ListWithOmissions.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/ListWithOmissions/ListWithOmissions/ListWithOmissions.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/ListWithOmissions/ListWithOmissions/Program.cs b/ListWithOmissions/ListWithOmissions/Program.cs new file mode 100644 index 0000000..b0e783c --- /dev/null +++ b/ListWithOmissions/ListWithOmissions/Program.cs @@ -0,0 +1,17 @@ +namespace ListWithOmissions; + +class Program +{ + public static void Main(string[] args) + { + var list = new ListWithOmissions(); + list.Add(1); + list.Add(2); + list.Add(3); + var listForCheck = new List { 1, 2, 3 }; + int i = 0; + foreach (var item in list) + { + } + } +} \ No newline at end of file diff --git a/ListWithOmissions/TestsForListWithOmissions/TestsForList.cs b/ListWithOmissions/TestsForListWithOmissions/TestsForList.cs new file mode 100644 index 0000000..9816539 --- /dev/null +++ b/ListWithOmissions/TestsForListWithOmissions/TestsForList.cs @@ -0,0 +1,32 @@ +namespace TestsForListWithOmissions; + +using ListWithOmissions; + +public class Tests +{ + ListWithOmissions list; + [SetUp] + public void Setup() + { + list = new ListWithOmissions(); + } + + [Test] + public void ListWithOmissionsShouldCorrectAdd() + { + list.Add(1); + list.Add(2); + list.Add(3); + var listForCheck = new List { 1, 2, 3 }; + int i = 0; + foreach (var item in list) + { + if (item != listForCheck[i]) + { + Assert.Fail(); + } + ++i; + } + Assert.Pass(); + } +} \ No newline at end of file diff --git a/ListWithOmissions/TestsForListWithOmissions/TestsForListWithOmissions.csproj b/ListWithOmissions/TestsForListWithOmissions/TestsForListWithOmissions.csproj new file mode 100644 index 0000000..491dd70 --- /dev/null +++ b/ListWithOmissions/TestsForListWithOmissions/TestsForListWithOmissions.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/ListWithOmissions/TestsForListWithOmissions/Usings.cs b/ListWithOmissions/TestsForListWithOmissions/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/ListWithOmissions/TestsForListWithOmissions/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file