From fa6f986cdb7e931e5f57a2a0f5167d0ce3acb41d Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 20 Jul 2020 07:27:50 -0700 Subject: [PATCH] Enable nullable reference types for TreeList --- .../List/BinarySearch1.cs | 19 ++++- .../List/BinarySearch2.cs | 47 ++++++++---- .../List/BinarySearch3.cs | 49 ++++++++---- .../List/CopyTo1.cs | 4 +- .../List/CopyTo2.cs | 4 +- .../List/CopyTo3.cs | 4 +- .../List/TreeListAdd.cs | 4 +- .../List/TreeListAddRange.cs | 6 +- .../List/TreeListClear.cs | 2 - .../List/TreeListContains.cs | 6 +- .../List/TreeListCount.cs | 2 - .../List/TreeListCtor1.cs | 2 - .../List/TreeListCtor2.cs | 6 +- .../List/TreeListForEach.cs | 8 +- .../List/TreeListGetEnumerator.cs | 2 - .../List/TreeListGetRange.cs | 2 - .../List/TreeListICollectionCopyTo.cs | 4 +- .../List/TreeListICollectionIsReadOnly.cs | 2 - .../List/TreeListICollectionIsSynchronized.cs | 2 - .../List/TreeListICollectionSyncRoot.cs | 2 - .../List/TreeListIEnumerableGetEnumerator.cs | 2 - .../List/TreeListIEnumerableGetEnumerator2.cs | 6 +- .../List/TreeListIListAdd.cs | 12 ++- .../List/TreeListIListContains.cs | 8 +- .../List/TreeListIListIndexOf.cs | 12 ++- .../List/TreeListIListInsert.cs | 12 ++- .../List/TreeListIListIsFixedSize.cs | 2 - .../List/TreeListIListIsReadOnly.cs | 2 - .../List/TreeListIListItem.cs | 12 ++- .../List/TreeListIListRemove.cs | 6 +- .../List/TreeListIndexOf1.cs | 2 - .../List/TreeListIndexOf2.cs | 2 - .../List/TreeListIndexOf3.cs | 2 - .../List/TreeListInsertRange.cs | 10 +-- .../List/TreeListLastIndexOf1.cs | 4 +- .../List/TreeListLastIndexOf2.cs | 2 - .../List/TreeListLastIndexOf3.cs | 2 - .../List/TreeListRemoveAt.cs | 2 - .../List/TreeListRemoveRange.cs | 2 - .../List/TreeListReverse.cs | 2 - .../List/TreeListReverse2.cs | 2 - .../List/TreeListToArray.cs | 2 - .../TreeListTest.cs | 36 +++++---- .../PublicAPI.Unshipped.txt | 56 +++++++------- .../TreeList`1+Enumerator.cs | 14 ++-- .../TreeList`1+IndexNode.cs | 74 +++++++++++-------- .../TreeList`1+LeafNode.cs | 34 ++++----- .../TreeList`1+Node.cs | 28 ++++--- .../TreeList`1.cs | 40 +++++----- 49 files changed, 275 insertions(+), 292 deletions(-) diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch1.cs b/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch1.cs index ccd2fba..29dcde1 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch1.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch1.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -53,11 +51,21 @@ public void PosTest4() Assert.Equal(1, listObject.BinarySearch(new MyClass(20))); } + [Fact] + public void TestComparable() + { + MyClass first = new MyClass(10); + MyClass second = new MyClass(20); + Assert.Equal(-1, first.CompareTo(second)); + Assert.Equal(1, second.CompareTo(first)); + Assert.Equal(1, first.CompareTo(null)); + } + [Fact(DisplayName = "PosTest5: The item to be search is a null reference")] public void PosTest5() { string[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; - TreeList listObject = new TreeList(strArray); + TreeList listObject = new TreeList(strArray); Assert.Equal(-1, listObject.BinarySearch(null)); } @@ -78,8 +86,11 @@ public MyClass(int a) _value = a; } - public int CompareTo(object obj) + public int CompareTo(object? obj) { + if (obj is null) + return 1; + return _value.CompareTo(((MyClass)obj)._value); } } diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch2.cs b/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch2.cs index a18a57c..9d18c57 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch2.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch2.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -51,18 +49,29 @@ public void PosTest4() MyClass myclass1 = new MyClass(10); MyClass myclass2 = new MyClass(20); MyClass myclass3 = new MyClass(30); - MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; - TreeList listObject = new TreeList(mc); + MyClass?[] mc = new MyClass?[4] { myclass1, null, myclass2, myclass3 }; + TreeList listObject = new TreeList(mc); MyClassIC myclassIC = new MyClassIC(); listObject.Sort(myclassIC); - Assert.Equal(2, listObject.BinarySearch(new MyClass(10), myclassIC)); + Assert.Equal(3, listObject.BinarySearch(new MyClass(10), myclassIC)); + Assert.Equal(0, listObject.BinarySearch(null, myclassIC)); + } + + [Fact] + public void TestComparable() + { + MyClass first = new MyClass(10); + MyClass second = new MyClass(20); + Assert.Equal(-1, first.CompareTo(second)); + Assert.Equal(1, second.CompareTo(first)); + Assert.Equal(1, first.CompareTo(null)); } [Fact(DisplayName = "PosTest5: The item to be search is a null reference")] public void PosTest5() { string[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; - TreeList listObject = new TreeList(strArray); + TreeList listObject = new TreeList(strArray); listObject.Sort(); StrClass strClass = new StrClass(); Assert.Equal(-1, listObject.BinarySearch(null, strClass)); @@ -72,8 +81,8 @@ public void PosTest5() [Fact] public void PosTest5Ext() { - string[] strArray = { null, "banana", "chocolate", "dog", "food" }; - TreeList listObject = new TreeList(strArray); + string?[] strArray = { null, "banana", "chocolate", "dog", "food" }; + TreeList listObject = new TreeList(strArray); listObject.Sort(); StrClass strClass = new StrClass(); Assert.Equal(~1, listObject.BinarySearch(string.Empty, strClass)); @@ -112,8 +121,11 @@ public MyClass(int a) public int Value => _value; - public int CompareTo(object obj) + public int CompareTo(object? obj) { + if (obj is null) + return 1; + return _value.CompareTo(((MyClass)obj)._value); } } @@ -130,9 +142,9 @@ public int Compare(int x, int y) } } - public class StrClass : IComparer + public class StrClass : IComparer { - public int Compare(string x, string y) + public int Compare(string? x, string? y) { { if (x == null) @@ -182,10 +194,19 @@ public int Compare(string x, string y) } } - public class MyClassIC : IComparer + public class MyClassIC : IComparer { - public int Compare(MyClass x, MyClass y) + public int Compare(MyClass? x, MyClass? y) { + if (x is null) + { + return y is null ? 0 : -1; + } + else if (y is null) + { + return 1; + } + return (-1) * x.Value.CompareTo(y.Value); } } diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch3.cs b/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch3.cs index 8131097..7c0035d 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch3.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch3.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -51,18 +49,29 @@ public void PosTest4() MyClass myclass1 = new MyClass(10); MyClass myclass2 = new MyClass(20); MyClass myclass3 = new MyClass(30); - MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; - TreeList listObject = new TreeList(mc); + MyClass?[] mc = new MyClass?[4] { myclass1, null, myclass2, myclass3 }; + TreeList listObject = new TreeList(mc); MyClassIC myclassIC = new MyClassIC(); listObject.Sort(myclassIC); - Assert.Equal(2, listObject.BinarySearch(0, 3, new MyClass(10), myclassIC)); + Assert.Equal(3, listObject.BinarySearch(0, 4, new MyClass(10), myclassIC)); + Assert.Equal(0, listObject.BinarySearch(0, 4, null, myclassIC)); + } + + [Fact] + public void TestComparable() + { + MyClass first = new MyClass(10); + MyClass second = new MyClass(20); + Assert.Equal(-1, first.CompareTo(second)); + Assert.Equal(1, second.CompareTo(first)); + Assert.Equal(1, first.CompareTo(null)); } [Fact(DisplayName = "PosTest5: The item to be search is a null reference")] public void PosTest5() { - string[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; - TreeList listObject = new TreeList(strArray); + string?[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; + TreeList listObject = new TreeList(strArray); listObject.Sort(); StrClass strClass = new StrClass(); Assert.Equal(-1, listObject.BinarySearch(0, 3, null, strClass)); @@ -72,8 +81,8 @@ public void PosTest5() [Fact] public void PosTest5Ext() { - string[] strArray = { null, "banana", "chocolate", "dog", "food" }; - TreeList listObject = new TreeList(strArray); + string?[] strArray = { null, "banana", "chocolate", "dog", "food" }; + TreeList listObject = new TreeList(strArray); listObject.Sort(); StrClass strClass = new StrClass(); Assert.Equal(~1, listObject.BinarySearch(0, 3, string.Empty, strClass)); @@ -145,8 +154,11 @@ public MyClass(int a) public int Value => _value; - public int CompareTo(object obj) + public int CompareTo(object? obj) { + if (obj is null) + return 1; + return _value.CompareTo(((MyClass)obj)._value); } } @@ -163,9 +175,9 @@ public int Compare(int x, int y) } } - public class StrClass : IComparer + public class StrClass : IComparer { - public int Compare(string x, string y) + public int Compare(string? x, string? y) { { if (x == null) @@ -215,10 +227,19 @@ public int Compare(string x, string y) } } - public class MyClassIC : IComparer + public class MyClassIC : IComparer { - public int Compare(MyClass x, MyClass y) + public int Compare(MyClass? x, MyClass? y) { + if (x is null) + { + return y is null ? 0 : -1; + } + else if (y is null) + { + return 1; + } + return (-1) * x.Value.CompareTo(y.Value); } } diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo1.cs b/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo1.cs index 3cd06f9..bf37776 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo1.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo1.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -62,7 +60,7 @@ public void NegTest1() { int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; TreeList listObject = new TreeList(iArray); - Assert.Throws(() => listObject.CopyTo(null)); + Assert.Throws(() => listObject.CopyTo(null!)); } [Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")] diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo2.cs b/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo2.cs index c10d486..0be2124 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo2.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo2.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -75,7 +73,7 @@ public void NegTest1() { int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; TreeList listObject = new TreeList(iArray); - Assert.Throws(() => listObject.CopyTo(null, 0)); + Assert.Throws(() => listObject.CopyTo(null!, 0)); } [Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")] diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo3.cs b/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo3.cs index f66c2cb..c209137 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo3.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/CopyTo3.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -73,7 +71,7 @@ public void NegTest1() { int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; TreeList listObject = new TreeList(iArray); - Assert.Throws(() => listObject.CopyTo(0, null, 0, 10)); + Assert.Throws(() => listObject.CopyTo(0, null!, 0, 10)); } [Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")] diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListAdd.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListAdd.cs index b098020..ea933cc 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListAdd.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListAdd.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; @@ -54,7 +52,7 @@ public void PosTest3() [Fact(DisplayName = "PosTest4: Add null object to the list")] public void PosTest4() { - TreeList listObject = new TreeList(); + TreeList listObject = new TreeList(); listObject.Add(null); Assert.Null(listObject[0]); } diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListAddRange.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListAddRange.cs index c82eb3d..b716812 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListAddRange.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListAddRange.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -57,9 +55,9 @@ public void PosTest3() [Fact(DisplayName = "NegTest1: The argument is a null reference")] public void NegTest1() { - IEnumerable i = null; + IEnumerable? i = null; TreeList listObject = new TreeList(); - Assert.Throws(() => listObject.AddRange(i)); + Assert.Throws(() => listObject.AddRange(i!)); } public class MyClass diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListClear.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListClear.cs index 62c2b7b..9574d18 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListClear.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListClear.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListContains.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListContains.cs index fa06d13..3319b51 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListContains.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListContains.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - // This file contains tests for Contains... #pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection @@ -56,8 +54,8 @@ public void PosTest4() [Fact(DisplayName = "PosTest5: The argument is a null reference")] public void PosTest5() { - string[] strArray = { "apple", "banana", "chocolate", null, "food" }; - TreeList listObject = new TreeList(strArray); + string?[] strArray = { "apple", "banana", "chocolate", null, "food" }; + TreeList listObject = new TreeList(strArray); Assert.True(listObject.Contains(null)); } diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCount.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCount.cs index faa0498..4f2c084 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCount.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCount.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor1.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor1.cs index 1f73723..de648a6 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor1.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor1.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor2.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor2.cs index 128296a..9cabbfa 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor2.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor2.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -56,8 +54,8 @@ public void PosTest4() [Fact(DisplayName = "NegTest1: The argument is a null reference")] public void NegTest1() { - IEnumerable i = null; - Assert.Throws(() => new TreeList(i)); + IEnumerable? i = null; + Assert.Throws(() => new TreeList(i!)); } public class MyClass diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListForEach.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListForEach.cs index 409838b..72c7d4f 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListForEach.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListForEach.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -59,15 +57,15 @@ public void NegTest1() { int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; TreeList listObject = new TreeList(iArray); - Action action = null; - Assert.Throws(() => listObject.ForEach(action)); + Action? action = null; + Assert.Throws(() => listObject.ForEach(action!)); } public class MyClass { public int Sum { get; set; } = 0; - public string Result { get; set; } + public string? Result { get; set; } public void SumCalc(int a) { diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListGetEnumerator.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListGetEnumerator.cs index 425b2c5..b2aaa69 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListGetEnumerator.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListGetEnumerator.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListGetRange.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListGetRange.cs index fee62c8..91f517f 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListGetRange.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListGetRange.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionCopyTo.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionCopyTo.cs index b7af79a..33a6cf2 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionCopyTo.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionCopyTo.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -78,7 +76,7 @@ public void NegTest1() { int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; TreeList listObject = new TreeList(iArray); - Assert.Throws(() => ((ICollection)listObject).CopyTo(null, 0)); + Assert.Throws(() => ((ICollection)listObject).CopyTo(null!, 0)); } [Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")] diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionIsReadOnly.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionIsReadOnly.cs index 7f4d04d..fa54610 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionIsReadOnly.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionIsReadOnly.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionIsSynchronized.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionIsSynchronized.cs index 2963665..8816311 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionIsSynchronized.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionIsSynchronized.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionSyncRoot.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionSyncRoot.cs index a21d93e..135337f 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionSyncRoot.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionSyncRoot.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIEnumerableGetEnumerator.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIEnumerableGetEnumerator.cs index 227a4b8..b3c006d 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIEnumerableGetEnumerator.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIEnumerableGetEnumerator.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIEnumerableGetEnumerator2.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIEnumerableGetEnumerator2.cs index 07a6856..8a4bc23 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIEnumerableGetEnumerator2.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIEnumerableGetEnumerator2.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections; @@ -32,7 +30,7 @@ public void PosTest1() int j = 0; for (IEnumerator itr = returnValue; itr.MoveNext();) { - int current = (int)itr.Current; + int current = (int)itr.Current!; Assert.Equal(expectValue[j], current); j++; @@ -57,7 +55,7 @@ public void PosTest2() int j = 0; for (IEnumerator itr = returnValue; itr.MoveNext();) { - string current = (string)itr.Current; + string? current = (string?)itr.Current; Assert.Equal(expectValue[j], current); j++; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListAdd.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListAdd.cs index a000cb4..7329647 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListAdd.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListAdd.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -23,7 +21,7 @@ public void PosTest1() int count = 10; int[] expectValue = new int[10]; IList myIList = myList; - object element = null; + object? element = null; for (int i = 1; i <= count; i++) { element = i * count; @@ -35,7 +33,7 @@ public void PosTest1() int j = 0; for (IEnumerator itr = returnValue; itr.MoveNext();) { - int current = (int)itr.Current; + int current = (int)itr.Current!; Assert.Equal(expectValue[j], current); j++; @@ -47,8 +45,8 @@ public void PosTest2() { TreeList myList = new TreeList(); int count = 10; - string[] expectValue = new string[10]; - object element = null; + string?[] expectValue = new string?[10]; + object? element = null; IList myIList = myList; for (int i = 1; i <= count; i++) { @@ -61,7 +59,7 @@ public void PosTest2() int j = 0; for (IEnumerator itr = returnValue; itr.MoveNext();) { - string current = (string)itr.Current; + string? current = (string?)itr.Current; Assert.Equal(expectValue[j], current); j++; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListContains.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListContains.cs index 60ad004..94f53fa 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListContains.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListContains.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections; @@ -22,7 +20,7 @@ public void PosTest1() int count = 10; int[] expectValue = new int[10]; IList myIList = myList; - object element = null; + object? element = null; for (int i = 1; i <= count; i++) { element = i * count; @@ -38,8 +36,8 @@ public void PosTest2() { TreeList myList = new TreeList(); int count = 10; - string[] expectValue = new string[10]; - object element = null; + string?[] expectValue = new string?[10]; + object? element = null; IList myIList = myList; for (int i = 1; i <= count; i++) { diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIndexOf.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIndexOf.cs index 7c6de9c..ec0bf8b 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIndexOf.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIndexOf.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections; @@ -22,7 +20,7 @@ public void PosTest1() int count = 10; int[] expectValue = new int[10]; IList myIList = myList; - object element = null; + object? element = null; for (int i = 1; i <= count; i++) { element = i * count; @@ -42,8 +40,8 @@ public void PosTest2() { TreeList myList = new TreeList(); int count = 10; - string[] expectValue = new string[10]; - object element = null; + string?[] expectValue = new string?[10]; + object? element = null; IList myIList = myList; for (int i = 1; i <= count; i++) { @@ -64,8 +62,8 @@ public void PosTest3() { TreeList myList = new TreeList(); int count = 10; - string[] expectValue = new string[10]; - object element = null; + string?[] expectValue = new string?[10]; + object? element = null; IList myIList = myList; for (int i = 1; i <= count; i++) { diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListInsert.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListInsert.cs index 0e935ca..6af1a45 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListInsert.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListInsert.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -28,7 +26,7 @@ public void PosTest1() } IList myIList = myList; - object element = null; + object? element = null; for (int i = 1; i <= count; i++) { element = 0; @@ -46,7 +44,7 @@ public void PosTest1() int j = 0; for (IEnumerator itr = returnValue; itr.MoveNext();) { - int current = (int)itr.Current; + int current = (int)itr.Current!; Assert.Equal(expectValue[j], current); j++; @@ -58,13 +56,13 @@ public void PosTest2() { TreeList myList = new TreeList(); int count = 10; - string[] expectValue = new string[20]; + string?[] expectValue = new string?[20]; for (int z = 0; z < 20; z++) { expectValue[z] = string.Empty; } - object element = null; + object? element = null; IList myIList = myList; for (int i = 1; i <= count; i++) { @@ -83,7 +81,7 @@ public void PosTest2() int j = 0; for (IEnumerator itr = returnValue; itr.MoveNext();) { - string current = (string)itr.Current; + string current = (string)itr.Current!; Assert.Equal(expectValue[j], current); j++; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIsFixedSize.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIsFixedSize.cs index eea6e36..93dcb07 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIsFixedSize.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIsFixedSize.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIsReadOnly.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIsReadOnly.cs index ad8b626..6663cb4 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIsReadOnly.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIsReadOnly.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListItem.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListItem.cs index 27844e9..b456b13 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListItem.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListItem.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -23,7 +21,7 @@ public void PosTest1() int count = 10; int[] expectValue = new int[10]; IList myIList = myList; - object element = null; + object? element = null; for (int i = 1; i <= count; i++) { element = i * count; @@ -33,7 +31,7 @@ public void PosTest1() for (int j = 0; j < myIList.Count; j++) { - int current = (int)myIList[j]; + int current = (int)myIList[j]!; Assert.Equal(expectValue[j], current); } } @@ -43,8 +41,8 @@ public void PosTest2() { TreeList myList = new TreeList(); int count = 10; - string[] expectValue = new string[10]; - object element = null; + string?[] expectValue = new string?[10]; + object? element = null; IList myIList = myList; for (int i = 1; i <= count; i++) { @@ -55,7 +53,7 @@ public void PosTest2() for (int j = 0; j < myIList.Count; j++) { - string current = (string)myIList[j]; + string current = (string)myIList[j]!; Assert.Equal(expectValue[j], current); } } diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListRemove.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListRemove.cs index a8da2e3..18972a2 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListRemove.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListRemove.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections; @@ -21,7 +19,7 @@ public void PosTest1() TreeList myList = new TreeList(); int count = 10; IList myIList = myList; - object element = null; + object? element = null; for (int i = 1; i <= count; i++) { element = i * count; @@ -41,7 +39,7 @@ public void PosTest2() { TreeList myList = new TreeList(); int count = 10; - object element = null; + object? element = null; IList myIList = myList; for (int i = 1; i <= count; i++) { diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf1.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf1.cs index 15bec63..6ab8085 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf1.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf1.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf2.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf2.cs index d6fcd24..a96063c 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf2.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf2.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf3.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf3.cs index 2c66ba9..d17a63e 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf3.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf3.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListInsertRange.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListInsertRange.cs index e527550..0a79b27 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListInsertRange.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListInsertRange.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; @@ -69,8 +67,8 @@ public void PosTest3() public void PosTest4() { string[] strArray = { "apple", "dog", "banana", "food" }; - TreeList listObject = new TreeList(strArray); - string[] insert = new string[2] { null, null }; + TreeList listObject = new TreeList(strArray); + string?[] insert = new string?[2] { null, null }; int index = Generator.GetInt32(0, 4); listObject.InsertRange(index, insert); Assert.Equal(6, listObject.Count); @@ -83,9 +81,9 @@ public void NegTest1() { string[] strArray = { "apple", "dog", "banana", "food" }; TreeList listObject = new TreeList(strArray); - string[] insert = null; + string[]? insert = null; int index = Generator.GetInt32(0, 4); - Assert.Throws(() => listObject.InsertRange(index, insert)); + Assert.Throws(() => listObject.InsertRange(index, insert!)); } [Fact(DisplayName = "NegTest2: The index is negative")] diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf1.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf1.cs index 49f786c..ee8591d 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf1.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf1.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; @@ -72,7 +70,7 @@ public void PosTest5() public void PosTest6() { string[] strArray = { "apple", "banana", "chocolate" }; - TreeList listObject = new TreeList(strArray); + TreeList listObject = new TreeList(strArray); int result = listObject.LastIndexOf(null); Assert.Equal(-1, result); } diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf2.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf2.cs index 24f9447..2cc340a 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf2.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf2.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf3.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf3.cs index bea8643..c049801 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf3.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf3.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListRemoveAt.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListRemoveAt.cs index ebc0649..b542d2a 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListRemoveAt.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListRemoveAt.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListRemoveRange.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListRemoveRange.cs index 8f50547..3248362 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListRemoveRange.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListRemoveRange.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListReverse.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListReverse.cs index 32d45b0..b978ec1 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListReverse.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListReverse.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListReverse2.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListReverse2.cs index 1798f8c..4750a3b 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListReverse2.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListReverse2.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System; diff --git a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListToArray.cs b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListToArray.cs index 741e244..54f3962 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/List/TreeListToArray.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/List/TreeListToArray.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test.List { using System.Collections.Generic; diff --git a/TunnelVisionLabs.Collections.Trees.Test/TreeListTest.cs b/TunnelVisionLabs.Collections.Trees.Test/TreeListTest.cs index ea83f36..02ac2a0 100644 --- a/TunnelVisionLabs.Collections.Trees.Test/TreeListTest.cs +++ b/TunnelVisionLabs.Collections.Trees.Test/TreeListTest.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees.Test { using System; @@ -79,7 +77,7 @@ private static void TestIListInterfaceImpl(IList list, bool supportsNullValues) list.Remove("Text"); Assert.Equal(originalCount, list.Count); - object removedItem = list[0]; + object? removedItem = list[0]; list.Remove(list[0]); Assert.Equal(originalCount - 1, list.Count); Assert.True(list.Contains(removedItem)); @@ -210,8 +208,8 @@ public void TestIndexer() public void TestCopyToValidation() { TreeList list = new TreeList(Enumerable.Range(0, 10)); - Assert.Throws("dest", () => list.CopyTo(null)); - Assert.Throws("dest", () => list.CopyTo(0, null, 0, list.Count)); + Assert.Throws("dest", () => list.CopyTo(null!)); + Assert.Throws("dest", () => list.CopyTo(0, null!, 0, list.Count)); Assert.Throws("srcIndex", () => list.CopyTo(-1, new int[list.Count], 0, list.Count)); Assert.Throws("dstIndex", () => list.CopyTo(0, new int[list.Count], -1, list.Count)); Assert.Throws("length", () => list.CopyTo(0, new int[list.Count], 0, -1)); @@ -219,7 +217,7 @@ public void TestCopyToValidation() Assert.Throws(string.Empty, () => list.CopyTo(0, new int[list.Count], 1, list.Count)); ICollection collection = list; - Assert.Throws("dest", () => collection.CopyTo(null, 0)); + Assert.Throws("dest", () => collection.CopyTo(null!, 0)); Assert.Throws("dstIndex", () => collection.CopyTo(new int[collection.Count], -1)); Assert.Throws("dstIndex", () => collection.CopyTo(Array.CreateInstance(typeof(int), new[] { list.Count }, new[] { 1 }), 0)); Assert.Throws(string.Empty, () => collection.CopyTo(new int[collection.Count], collection.Count + 1)); @@ -476,9 +474,9 @@ public void TestFindIndex() reference.Insert(index, i); } - Assert.Throws(() => list.FindIndex(null)); - Assert.Throws(() => list.FindIndex(0, null)); - Assert.Throws(() => list.FindIndex(0, 0, null)); + Assert.Throws(() => list.FindIndex(null!)); + Assert.Throws(() => list.FindIndex(0, null!)); + Assert.Throws(() => list.FindIndex(0, 0, null!)); Assert.Throws(() => list.FindIndex(-1, i => true)); Assert.Throws(() => list.FindIndex(0, -1, i => true)); @@ -517,9 +515,9 @@ public void TestFindLastIndex() reference.Insert(index, i); } - Assert.Throws(() => list.FindLastIndex(null)); - Assert.Throws(() => list.FindLastIndex(-1, null)); - Assert.Throws(() => list.FindLastIndex(-1, 0, null)); + Assert.Throws(() => list.FindLastIndex(null!)); + Assert.Throws(() => list.FindLastIndex(-1, null!)); + Assert.Throws(() => list.FindLastIndex(-1, 0, null!)); Assert.Throws(() => list.FindLastIndex(list.Count, i => true)); Assert.Throws(() => list.FindLastIndex(list.Count - 1, -1, i => true)); @@ -716,7 +714,7 @@ public void TestSortComparison() reference.Insert(index, item); } - Assert.Throws(() => list.Sort((Comparison)null)); + Assert.Throws(() => list.Sort((Comparison)null!)); Comparison comparison = (x, y) => x - y; list.Sort(comparison); @@ -937,7 +935,7 @@ public void TestRemoveValue() public void TestRemoveAll() { var list = new TreeList(4, Enumerable.Range(0, 10)); - Assert.Throws(() => list.RemoveAll(null)); + Assert.Throws(() => list.RemoveAll(null!)); Assert.Equal(5, list.RemoveAll(i => (i % 2) == 0)); Assert.Equal(new[] { 1, 3, 5, 7, 9 }, list); @@ -949,7 +947,7 @@ public void TestRemoveAll() public void TestExists() { var list = new TreeList(4, Enumerable.Range(0, 10)); - Assert.Throws(() => list.Exists(null)); + Assert.Throws(() => list.Exists(null!)); Assert.False(list.Exists(value => value < 0)); foreach (var i in list) @@ -964,7 +962,7 @@ public void TestExists() public void TestFind() { var list = new TreeList(4, Enumerable.Range(1, 10)); - Assert.Throws(() => list.Find(null)); + Assert.Throws(() => list.Find(null!)); Assert.Equal(0, list.Find(value => value < 0)); foreach (var i in list) @@ -979,7 +977,7 @@ public void TestFind() public void TestFindAll() { var list = new TreeList(4, Enumerable.Range(0, 10)); - Assert.Throws(() => list.FindAll(null)); + Assert.Throws(() => list.FindAll(null!)); TreeList found = list.FindAll(i => (i % 2) == 0); @@ -995,7 +993,7 @@ public void TestFindLast() { var list = new TreeList(4, Enumerable.Range(1, 10)); var reference = new List(Enumerable.Range(1, 10)); - Assert.Throws(() => list.FindLast(null)); + Assert.Throws(() => list.FindLast(null!)); Assert.Equal(0, list.FindLast(i => i < 0)); Assert.Equal(0, reference.FindLast(i => i < 0)); @@ -1012,7 +1010,7 @@ public void TestTrueForAll() { var list = new TreeList(); Assert.True(list.TrueForAll(i => false)); - Assert.Throws(() => list.TrueForAll(null)); + Assert.Throws(() => list.TrueForAll(null!)); list.Add(1); Assert.True(list.TrueForAll(i => i > 0)); diff --git a/TunnelVisionLabs.Collections.Trees/PublicAPI.Unshipped.txt b/TunnelVisionLabs.Collections.Trees/PublicAPI.Unshipped.txt index 17f9e34..5d9f924 100644 --- a/TunnelVisionLabs.Collections.Trees/PublicAPI.Unshipped.txt +++ b/TunnelVisionLabs.Collections.Trees/PublicAPI.Unshipped.txt @@ -587,59 +587,59 @@ TunnelVisionLabs.Collections.Trees.TreeDictionary.this[TKey key].g TunnelVisionLabs.Collections.Trees.TreeDictionary.this[TKey key].set -> void TunnelVisionLabs.Collections.Trees.TreeList TunnelVisionLabs.Collections.Trees.TreeList.Add(T item) -> void -~TunnelVisionLabs.Collections.Trees.TreeList.AddRange(System.Collections.Generic.IEnumerable collection) -> void +TunnelVisionLabs.Collections.Trees.TreeList.AddRange(System.Collections.Generic.IEnumerable! collection) -> void TunnelVisionLabs.Collections.Trees.TreeList.BinarySearch(T item) -> int -~TunnelVisionLabs.Collections.Trees.TreeList.BinarySearch(T item, System.Collections.Generic.IComparer comparer) -> int -~TunnelVisionLabs.Collections.Trees.TreeList.BinarySearch(int index, int count, T item, System.Collections.Generic.IComparer comparer) -> int +TunnelVisionLabs.Collections.Trees.TreeList.BinarySearch(T item, System.Collections.Generic.IComparer? comparer) -> int +TunnelVisionLabs.Collections.Trees.TreeList.BinarySearch(int index, int count, T item, System.Collections.Generic.IComparer? comparer) -> int TunnelVisionLabs.Collections.Trees.TreeList.Clear() -> void TunnelVisionLabs.Collections.Trees.TreeList.Contains(T item) -> bool -~TunnelVisionLabs.Collections.Trees.TreeList.ConvertAll(System.Func converter) -> TunnelVisionLabs.Collections.Trees.TreeList -~TunnelVisionLabs.Collections.Trees.TreeList.CopyTo(T[] array) -> void -~TunnelVisionLabs.Collections.Trees.TreeList.CopyTo(T[] array, int arrayIndex) -> void -~TunnelVisionLabs.Collections.Trees.TreeList.CopyTo(int srcIndex, T[] dest, int dstIndex, int length) -> void +TunnelVisionLabs.Collections.Trees.TreeList.ConvertAll(System.Func! converter) -> TunnelVisionLabs.Collections.Trees.TreeList! +TunnelVisionLabs.Collections.Trees.TreeList.CopyTo(T[]! array) -> void +TunnelVisionLabs.Collections.Trees.TreeList.CopyTo(T[]! array, int arrayIndex) -> void +TunnelVisionLabs.Collections.Trees.TreeList.CopyTo(int srcIndex, T[]! dest, int dstIndex, int length) -> void TunnelVisionLabs.Collections.Trees.TreeList.Count.get -> int TunnelVisionLabs.Collections.Trees.TreeList.Enumerator TunnelVisionLabs.Collections.Trees.TreeList.Enumerator.Current.get -> T TunnelVisionLabs.Collections.Trees.TreeList.Enumerator.Dispose() -> void TunnelVisionLabs.Collections.Trees.TreeList.Enumerator.MoveNext() -> bool -~TunnelVisionLabs.Collections.Trees.TreeList.Exists(System.Predicate match) -> bool -~TunnelVisionLabs.Collections.Trees.TreeList.Find(System.Predicate match) -> T -~TunnelVisionLabs.Collections.Trees.TreeList.FindAll(System.Predicate match) -> TunnelVisionLabs.Collections.Trees.TreeList -~TunnelVisionLabs.Collections.Trees.TreeList.FindIndex(System.Predicate match) -> int -~TunnelVisionLabs.Collections.Trees.TreeList.FindIndex(int startIndex, System.Predicate match) -> int -~TunnelVisionLabs.Collections.Trees.TreeList.FindIndex(int startIndex, int count, System.Predicate match) -> int -~TunnelVisionLabs.Collections.Trees.TreeList.FindLast(System.Predicate match) -> T -~TunnelVisionLabs.Collections.Trees.TreeList.FindLastIndex(System.Predicate match) -> int -~TunnelVisionLabs.Collections.Trees.TreeList.FindLastIndex(int startIndex, System.Predicate match) -> int -~TunnelVisionLabs.Collections.Trees.TreeList.FindLastIndex(int startIndex, int count, System.Predicate match) -> int -~TunnelVisionLabs.Collections.Trees.TreeList.ForEach(System.Action action) -> void +TunnelVisionLabs.Collections.Trees.TreeList.Exists(System.Predicate! match) -> bool +TunnelVisionLabs.Collections.Trees.TreeList.Find(System.Predicate! match) -> T +TunnelVisionLabs.Collections.Trees.TreeList.FindAll(System.Predicate! match) -> TunnelVisionLabs.Collections.Trees.TreeList! +TunnelVisionLabs.Collections.Trees.TreeList.FindIndex(System.Predicate! match) -> int +TunnelVisionLabs.Collections.Trees.TreeList.FindIndex(int startIndex, System.Predicate! match) -> int +TunnelVisionLabs.Collections.Trees.TreeList.FindIndex(int startIndex, int count, System.Predicate! match) -> int +TunnelVisionLabs.Collections.Trees.TreeList.FindLast(System.Predicate! match) -> T +TunnelVisionLabs.Collections.Trees.TreeList.FindLastIndex(System.Predicate! match) -> int +TunnelVisionLabs.Collections.Trees.TreeList.FindLastIndex(int startIndex, System.Predicate! match) -> int +TunnelVisionLabs.Collections.Trees.TreeList.FindLastIndex(int startIndex, int count, System.Predicate! match) -> int +TunnelVisionLabs.Collections.Trees.TreeList.ForEach(System.Action! action) -> void TunnelVisionLabs.Collections.Trees.TreeList.GetEnumerator() -> TunnelVisionLabs.Collections.Trees.TreeList.Enumerator -~TunnelVisionLabs.Collections.Trees.TreeList.GetRange(int index, int count) -> TunnelVisionLabs.Collections.Trees.TreeList +TunnelVisionLabs.Collections.Trees.TreeList.GetRange(int index, int count) -> TunnelVisionLabs.Collections.Trees.TreeList! TunnelVisionLabs.Collections.Trees.TreeList.IndexOf(T item) -> int TunnelVisionLabs.Collections.Trees.TreeList.IndexOf(T item, int index) -> int TunnelVisionLabs.Collections.Trees.TreeList.IndexOf(T item, int index, int count) -> int TunnelVisionLabs.Collections.Trees.TreeList.Insert(int index, T item) -> void -~TunnelVisionLabs.Collections.Trees.TreeList.InsertRange(int index, System.Collections.Generic.IEnumerable collection) -> void +TunnelVisionLabs.Collections.Trees.TreeList.InsertRange(int index, System.Collections.Generic.IEnumerable! collection) -> void TunnelVisionLabs.Collections.Trees.TreeList.LastIndexOf(T item) -> int TunnelVisionLabs.Collections.Trees.TreeList.LastIndexOf(T item, int index) -> int TunnelVisionLabs.Collections.Trees.TreeList.LastIndexOf(T item, int index, int count) -> int TunnelVisionLabs.Collections.Trees.TreeList.Remove(T item) -> bool -~TunnelVisionLabs.Collections.Trees.TreeList.RemoveAll(System.Predicate match) -> int +TunnelVisionLabs.Collections.Trees.TreeList.RemoveAll(System.Predicate! match) -> int TunnelVisionLabs.Collections.Trees.TreeList.RemoveAt(int index) -> void TunnelVisionLabs.Collections.Trees.TreeList.RemoveRange(int index, int count) -> void TunnelVisionLabs.Collections.Trees.TreeList.Reverse() -> void TunnelVisionLabs.Collections.Trees.TreeList.Reverse(int index, int count) -> void TunnelVisionLabs.Collections.Trees.TreeList.Sort() -> void -~TunnelVisionLabs.Collections.Trees.TreeList.Sort(System.Collections.Generic.IComparer comparer) -> void -~TunnelVisionLabs.Collections.Trees.TreeList.Sort(System.Comparison comparison) -> void -~TunnelVisionLabs.Collections.Trees.TreeList.Sort(int index, int count, System.Collections.Generic.IComparer comparer) -> void -~TunnelVisionLabs.Collections.Trees.TreeList.ToArray() -> T[] +TunnelVisionLabs.Collections.Trees.TreeList.Sort(System.Collections.Generic.IComparer? comparer) -> void +TunnelVisionLabs.Collections.Trees.TreeList.Sort(System.Comparison! comparison) -> void +TunnelVisionLabs.Collections.Trees.TreeList.Sort(int index, int count, System.Collections.Generic.IComparer? comparer) -> void +TunnelVisionLabs.Collections.Trees.TreeList.ToArray() -> T[]! TunnelVisionLabs.Collections.Trees.TreeList.TreeList() -> void -~TunnelVisionLabs.Collections.Trees.TreeList.TreeList(System.Collections.Generic.IEnumerable collection) -> void +TunnelVisionLabs.Collections.Trees.TreeList.TreeList(System.Collections.Generic.IEnumerable! collection) -> void TunnelVisionLabs.Collections.Trees.TreeList.TreeList(int branchingFactor) -> void -~TunnelVisionLabs.Collections.Trees.TreeList.TreeList(int branchingFactor, System.Collections.Generic.IEnumerable collection) -> void +TunnelVisionLabs.Collections.Trees.TreeList.TreeList(int branchingFactor, System.Collections.Generic.IEnumerable! collection) -> void TunnelVisionLabs.Collections.Trees.TreeList.TrimExcess() -> void -~TunnelVisionLabs.Collections.Trees.TreeList.TrueForAll(System.Predicate match) -> bool +TunnelVisionLabs.Collections.Trees.TreeList.TrueForAll(System.Predicate! match) -> bool TunnelVisionLabs.Collections.Trees.TreeList.this[int index].get -> T TunnelVisionLabs.Collections.Trees.TreeList.this[int index].set -> void TunnelVisionLabs.Collections.Trees.TreeQueue diff --git a/TunnelVisionLabs.Collections.Trees/TreeList`1+Enumerator.cs b/TunnelVisionLabs.Collections.Trees/TreeList`1+Enumerator.cs index 5798b93..1666e03 100644 --- a/TunnelVisionLabs.Collections.Trees/TreeList`1+Enumerator.cs +++ b/TunnelVisionLabs.Collections.Trees/TreeList`1+Enumerator.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees { using System; @@ -18,7 +16,7 @@ public struct Enumerator : IEnumerator private readonly int _version; private int _index; - private LeafNode _leafNode; + private LeafNode? _leafNode; private int _leafIndex; private T _current; @@ -35,12 +33,12 @@ internal Enumerator(TreeList list, TreeSpan span) _index = -1; _leafNode = null; _leafIndex = -1; - _current = default; + _current = default!; } public T Current => _current; - object IEnumerator.Current => Current; + object? IEnumerator.Current => Current; public void Dispose() { @@ -72,7 +70,7 @@ public bool MoveNext() _index = _span.Start - 1; _leafIndex--; } - else if (_leafIndex == _leafNode.Count - 1) + else if (_leafIndex == _leafNode!.Count - 1) { // Need to move to the next leaf _leafNode = _leafNode.Next; @@ -87,7 +85,7 @@ public bool MoveNext() } _leafIndex++; - _current = _leafNode[_leafIndex]; + _current = _leafNode![_leafIndex]; return true; } @@ -101,7 +99,7 @@ internal void InternalReset() _leafNode = null; _index = -1; _leafIndex = -1; - _current = default; + _current = default!; } } } diff --git a/TunnelVisionLabs.Collections.Trees/TreeList`1+IndexNode.cs b/TunnelVisionLabs.Collections.Trees/TreeList`1+IndexNode.cs index 9818e87..f208218 100644 --- a/TunnelVisionLabs.Collections.Trees/TreeList`1+IndexNode.cs +++ b/TunnelVisionLabs.Collections.Trees/TreeList`1+IndexNode.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees { using System; @@ -15,7 +13,7 @@ private sealed class IndexNode : Node { private readonly int[] _offsets; private readonly Node[] _nodes; - private IndexNode _next; + private IndexNode? _next; private int _nodeCount; private int _count; @@ -37,11 +35,11 @@ internal IndexNode(int branchingFactor, Node child1, Node child2) _count = child1.Count + child2.Count; } - internal IndexNode(int branchingFactor, Node firstChild, Node lastChild, out IndexNode lastNode) + internal IndexNode(int branchingFactor, Node firstChild, Node lastChild, out IndexNode? lastNode) : this(branchingFactor) { lastNode = null; - for (Node current = firstChild; current != null; current = current == lastChild ? null : current.NextNode) + for (Node? current = firstChild; current != null; current = current == lastChild ? null : current.NextNode) { if (_nodeCount == _nodes.Length) { @@ -61,13 +59,21 @@ internal IndexNode(int branchingFactor, Node firstChild, Node lastChild, out Ind internal override int Count => _count; - internal override LeafNode FirstLeaf => _nodes[0].FirstLeaf; + internal override LeafNode FirstLeaf + { + get + { + LeafNode? firstLeaf = _nodes[0].FirstLeaf; + Debug.Assert(firstLeaf is object, $"Assertion failed: {nameof(firstLeaf)} is object"); + return firstLeaf; + } + } - internal override Node NextNode => Next; + internal override Node? NextNode => Next; internal override Node FirstChild => _nodes[0]; - internal IndexNode Next => _next; + internal IndexNode? Next => _next; internal override T this[int index] { @@ -140,10 +146,10 @@ internal override int LastIndexOf(T item, TreeSpan span) return -1; } - internal override Node Insert(int branchingFactor, bool isAppend, int index, T item) + internal override Node? Insert(int branchingFactor, bool isAppend, int index, T item) { int pageIndex = FindLowerBound(_offsets, _nodeCount, index); - Node splitChild = _nodes[pageIndex].Insert(branchingFactor, isAppend, index - _offsets[pageIndex], item); + Node? splitChild = _nodes[pageIndex].Insert(branchingFactor, isAppend, index - _offsets[pageIndex], item); if (splitChild == null) { for (int i = pageIndex + 1; i < _nodeCount; i++) @@ -160,11 +166,11 @@ internal override Node Insert(int branchingFactor, bool isAppend, int index, T i return InsertIndex(branchingFactor, isAppend, pageIndex + 1, splitChild); } - internal override Node InsertRange(int branchingFactor, bool isAppend, int index, IEnumerable collection) + internal override Node? InsertRange(int branchingFactor, bool isAppend, int index, IEnumerable collection) { int pageIndex = FindLowerBound(_offsets, _nodeCount, index); int previousCount = _nodes[pageIndex].Count; - Node lastImpactedChild = _nodes[pageIndex].InsertRange(branchingFactor, isAppend, index - _offsets[pageIndex], collection); + Node? lastImpactedChild = _nodes[pageIndex].InsertRange(branchingFactor, isAppend, index - _offsets[pageIndex], collection); if (lastImpactedChild == null) { int insertionCount = _nodes[pageIndex].Count - previousCount; @@ -182,13 +188,13 @@ internal override Node InsertRange(int branchingFactor, bool isAppend, int index pageIndex++; IndexNode insertionNode = this; - Node lastIndexNode = null; - for (Node item = _nodes[pageIndex - 1].NextNode; true; item = item.NextNode) + Node? lastIndexNode = null; + for (Node? item = _nodes[pageIndex - 1].NextNode; true; item = item.NextNode) { Debug.Assert(item != null, "Assertion failed: item != null"); Debug.Assert(pageIndex >= 0 && pageIndex <= insertionNode._nodes.Length, "Assertion failed: pageIndex >= 0 && pageIndex <= insertionNode._nodes.Length"); - IndexNode newLastIndex = insertionNode.InsertIndex(branchingFactor, isAppend, pageIndex, item); + IndexNode? newLastIndex = insertionNode.InsertIndex(branchingFactor, isAppend, pageIndex, item); if (newLastIndex != null) { // this insertion resulted in a split, so at minimum 'pageIndex' must be updated @@ -261,7 +267,7 @@ internal override bool RemoveLast() Debug.Assert(removedChild, $"Assertion failed: removedChild"); _nodeCount--; _offsets[_nodeCount] = 0; - _nodes[_nodeCount] = null; + _nodes[_nodeCount] = null!; _count--; return false; } @@ -283,8 +289,8 @@ internal override bool RemoveAt(int index) // _next cannot be null if (pageIndex == _nodeCount - 1), because there is no way we needed to rebalance // the children nodes for that case. This method would have already returned above. - Node expectedNext = pageIndex == _nodeCount - 1 ? _next._nodes[0] : _nodes[pageIndex + 1]; - Node nextChild = _nodes[pageIndex].NextNode; + Node expectedNext = pageIndex == _nodeCount - 1 ? _next!._nodes[0] : _nodes[pageIndex + 1]; + Node? nextChild = _nodes[pageIndex].NextNode; bool removedChild = nextChild != expectedNext; if (!removedChild) { @@ -299,24 +305,28 @@ internal override bool RemoveAt(int index) _count = _offsets[_nodeCount - 1] + _nodes[_nodeCount - 1].Count; - bool affectedNextPage = pageIndex == _nodeCount - 1 && _next != null; - if (affectedNextPage) + if (pageIndex == _nodeCount - 1 && _next != null) { + // The next page was affected for (int i = 1; i < _next._nodeCount; i++) { _next._offsets[i] = _next._offsets[i - 1] + _next._nodes[i - 1].Count; } _next._count = _next._offsets[_next._nodeCount - 1] + _next._nodes[_next._nodeCount - 1].Count; + return true; + } + else + { + return false; } - - return affectedNextPage; } else { bool removedFromNextPage = pageIndex == _nodeCount - 1; if (removedFromNextPage) { + Debug.Assert(_next is object, $"Per the explanation above, {nameof(_next)} cannot be null when {nameof(removedFromNextPage)} is true."); if (_next._nodeCount == 1) { // Removed the only child of the next page @@ -332,7 +342,7 @@ internal override bool RemoveAt(int index) _next._nodes[0] = _nodes[_nodeCount - 1]; _count = _offsets[_nodeCount - 1]; _offsets[_nodeCount - 1] = 0; - _nodes[_nodeCount - 1] = null; + _nodes[_nodeCount - 1] = null!; _nodeCount--; for (int i = 1; i < _next._nodeCount; i++) { @@ -350,7 +360,7 @@ internal override bool RemoveAt(int index) } _offsets[_nodeCount - 1] = 0; - _nodes[_nodeCount - 1] = default; + _nodes[_nodeCount - 1] = null!; _nodeCount--; _count = _offsets[_nodeCount - 1] + _nodes[_nodeCount - 1].Count; } @@ -557,7 +567,7 @@ internal override int BinarySearch(TreeSpan span, T item, IComparer comparer) int page = lowPage + ((highPage - lowPage + 1) >> 1); Debug.Assert(page > firstPage, $"Assertion failed: {nameof(page)} > {nameof(firstPage)}"); - T value = _nodes[page].FirstLeaf[0]; + T value = _nodes[page].FirstLeaf![0]; int c; try @@ -598,11 +608,11 @@ internal override int BinarySearch(TreeSpan span, T item, IComparer comparer) } } - internal override TreeList.Node ConvertAll(Func converter, TreeList.Node convertedNextNode) + internal override TreeList.Node ConvertAll(Func converter, TreeList.Node? convertedNextNode) { var result = new TreeList.IndexNode(_nodes.Length); - TreeList.Node convertedNextChild = convertedNextNode?.FirstChild; + TreeList.Node? convertedNextChild = convertedNextNode?.FirstChild; for (int i = _nodeCount - 1; i >= 0; i--) { convertedNextChild = _nodes[i].ConvertAll(converter, convertedNextChild); @@ -610,7 +620,7 @@ internal override TreeList.Node ConvertAll(Func co } Array.Copy(_offsets, result._offsets, _nodeCount); - result._next = (TreeList.IndexNode)convertedNextNode; + result._next = (TreeList.IndexNode?)convertedNextNode; result._count = _count; result._nodeCount = _nodeCount; return result; @@ -622,14 +632,16 @@ internal override bool TrimExcess() return false; // Simply rebuild this level by walking child nodes - IndexNode first = this; + IndexNode? first = this; int firstOffset = 0; _nodeCount = 0; _count = 0; - for (Node child = FirstChild; child != null; child = child.NextNode) + for (Node? child = FirstChild; child != null; child = child.NextNode) { if (firstOffset == first._nodes.Length) { + Debug.Assert(first.Next is object, "'child.NextNode' was not null, but it pointed to a node which is not a direct child of 'first'."); + first = first.Next; firstOffset = 0; first._nodeCount = 0; @@ -660,7 +672,7 @@ private TreeSpan MapSpanDownToChild(TreeSpan span, int childIndex) return TreeSpan.Intersect(mappedFullSpan, _nodes[childIndex].Span); } - private IndexNode InsertIndex(int branchingFactor, bool isAppend, int index, Node node) + private IndexNode? InsertIndex(int branchingFactor, bool isAppend, int index, Node node) { if (_nodeCount < _nodes.Length) { diff --git a/TunnelVisionLabs.Collections.Trees/TreeList`1+LeafNode.cs b/TunnelVisionLabs.Collections.Trees/TreeList`1+LeafNode.cs index 9554721..f6cbdb9 100644 --- a/TunnelVisionLabs.Collections.Trees/TreeList`1+LeafNode.cs +++ b/TunnelVisionLabs.Collections.Trees/TreeList`1+LeafNode.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees { using System; @@ -14,7 +12,7 @@ public partial class TreeList private sealed class LeafNode : Node { private readonly T[] _data; - private LeafNode _next; + private LeafNode? _next; private int _count; internal LeafNode(int branchingFactor) @@ -26,11 +24,11 @@ internal LeafNode(int branchingFactor) internal override LeafNode FirstLeaf => this; - internal override Node NextNode => Next; + internal override Node? NextNode => Next; - internal override Node FirstChild => null; + internal override Node? FirstChild => null; - internal LeafNode Next => _next; + internal LeafNode? Next => _next; internal override T this[int index] { @@ -71,7 +69,7 @@ internal override int LastIndexOf(T item, TreeSpan span) return Array.LastIndexOf(_data, item, span.EndInclusive, span.Count); } - internal override TreeList.Node ConvertAll(Func converter, TreeList.Node convertedNextNode) + internal override TreeList.Node ConvertAll(Func converter, TreeList.Node? convertedNextNode) { var result = new TreeList.LeafNode(_data.Length); @@ -80,12 +78,12 @@ internal override TreeList.Node ConvertAll(Func co result._data[i] = converter(_data[i]); } - result._next = (TreeList.LeafNode)convertedNextNode; + result._next = (TreeList.LeafNode?)convertedNextNode; result._count = _count; return result; } - internal override Node Insert(int branchingFactor, bool isAppend, int index, T item) + internal override Node? Insert(int branchingFactor, bool isAppend, int index, T item) { if (_count < _data.Length) { @@ -100,7 +98,7 @@ internal override Node Insert(int branchingFactor, bool isAppend, int index, T i if (isAppend) { // optimize the case of adding at the end of the overall list - var result = (LeafNode)Empty.Insert(branchingFactor, isAppend, 0, item); + var result = (LeafNode?)Empty.Insert(branchingFactor, isAppend, 0, item); _next = result; return result; } @@ -144,15 +142,15 @@ internal override Node Insert(int branchingFactor, bool isAppend, int index, T i } } - internal override Node InsertRange(int branchingFactor, bool isAppend, int index, IEnumerable collection) + internal override Node? InsertRange(int branchingFactor, bool isAppend, int index, IEnumerable collection) { Node insertionNode = this; - Node lastLeaf = null; + Node? lastLeaf = null; foreach (T item in collection) { Debug.Assert(index >= 0 && index <= ((LeafNode)insertionNode)._data.Length, "Assertion failed: index >= 0 && index <= ((LeafNode)insertionNode)._data.Length"); - Node newLastLeaf = insertionNode.Insert(branchingFactor, isAppend, index, item); + Node? newLastLeaf = insertionNode.Insert(branchingFactor, isAppend, index, item); if (newLastLeaf != null) { // this insertion resulted in a split, so at minimum 'index' must be updated @@ -206,7 +204,7 @@ internal override bool RemoveLast() { Debug.Assert(_count > 1, $"Assertion failed: _count > 1"); _count--; - _data[_count] = default; + _data[_count] = default!; return false; } } @@ -218,7 +216,7 @@ internal override bool RemoveAt(int index) _data[i] = _data[i + 1]; } - _data[_count - 1] = default; + _data[_count - 1] = default!; _count--; if (_count < _data.Length / 2 && _next != null) @@ -283,9 +281,9 @@ internal override int BinarySearch(TreeSpan span, T item, IComparer comparer) internal override bool TrimExcess() { bool changedAnything = false; - LeafNode first = this; + LeafNode? first = this; int firstOffset = 0; - LeafNode second = null; + LeafNode? second = null; int secondOffset = 0; while (first != null) { @@ -312,7 +310,7 @@ internal override bool TrimExcess() first._count = firstOffset; for (int i = firstOffset; i < first._data.Length; i++) { - first._data[i] = default; + first._data[i] = default!; } first._next = null; diff --git a/TunnelVisionLabs.Collections.Trees/TreeList`1+Node.cs b/TunnelVisionLabs.Collections.Trees/TreeList`1+Node.cs index 0bed807..98baf9d 100644 --- a/TunnelVisionLabs.Collections.Trees/TreeList`1+Node.cs +++ b/TunnelVisionLabs.Collections.Trees/TreeList`1+Node.cs @@ -1,8 +1,6 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees { using System; @@ -23,17 +21,17 @@ internal abstract int Count get; } - internal abstract LeafNode FirstLeaf + internal abstract LeafNode? FirstLeaf { get; } - internal abstract Node NextNode + internal abstract Node? NextNode { get; } - internal abstract Node FirstChild + internal abstract Node? FirstChild { get; } @@ -51,7 +49,7 @@ internal static Node Insert(Node root, int branchingFactor, int index, T item) if (index > root.Count) throw new ArgumentOutOfRangeException(nameof(index)); - Node splitNode = root.Insert(branchingFactor, index == root.Count, index, item); + Node? splitNode = root.Insert(branchingFactor, index == root.Count, index, item); if (splitNode == null) return root; @@ -79,11 +77,11 @@ internal static Node InsertRange(Node root, int branchingFactor, int index, IEnu if (root == Empty) root = new LeafNode(branchingFactor); - Node splitNode = root.InsertRange(branchingFactor, index == root.Count, index, collection); + Node? splitNode = root.InsertRange(branchingFactor, index == root.Count, index, collection); while (splitNode != null) { // Make a new level, walking nodes on the previous root level from 'node' to 'splitNode' - IndexNode newRoot = new IndexNode(branchingFactor, root, splitNode, out IndexNode newSplitNode); + IndexNode newRoot = new IndexNode(branchingFactor, root, splitNode, out IndexNode? newSplitNode); root = newRoot; splitNode = newSplitNode == newRoot ? null : newSplitNode; } @@ -184,9 +182,9 @@ internal void Reverse(TreeSpan span) internal abstract int LastIndexOf(T item, TreeSpan span); - internal abstract Node Insert(int branchingFactor, bool isAppend, int index, T item); + internal abstract Node? Insert(int branchingFactor, bool isAppend, int index, T item); - internal abstract Node InsertRange(int branchingFactor, bool isAppend, int index, IEnumerable collection); + internal abstract Node? InsertRange(int branchingFactor, bool isAppend, int index, IEnumerable collection); internal abstract bool RemoveLast(); @@ -207,7 +205,7 @@ internal TreeList.Node ConvertAll(Func converter) return ConvertAll(converter, null); } - internal abstract TreeList.Node ConvertAll(Func converter, TreeList.Node convertedNextNode); + internal abstract TreeList.Node ConvertAll(Func converter, TreeList.Node? convertedNextNode); internal abstract bool TrimExcess(); @@ -223,11 +221,11 @@ internal override int Count } } - internal override LeafNode FirstLeaf => null; + internal override LeafNode? FirstLeaf => null; - internal override Node NextNode => null; + internal override Node? NextNode => null; - internal override Node FirstChild => null; + internal override Node? FirstChild => null; [ExcludeFromCodeCoverage] internal override T this[int index] @@ -318,7 +316,7 @@ internal override int BinarySearch(TreeSpan span, T item, IComparer comparer) return ~0; } - internal override TreeList.Node ConvertAll(Func converter, TreeList.Node convertedNextNode) + internal override TreeList.Node ConvertAll(Func converter, TreeList.Node? convertedNextNode) { return TreeList.Node.Empty; } diff --git a/TunnelVisionLabs.Collections.Trees/TreeList`1.cs b/TunnelVisionLabs.Collections.Trees/TreeList`1.cs index 8b48010..c370679 100644 --- a/TunnelVisionLabs.Collections.Trees/TreeList`1.cs +++ b/TunnelVisionLabs.Collections.Trees/TreeList`1.cs @@ -1,14 +1,13 @@ // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. -#nullable disable - namespace TunnelVisionLabs.Collections.Trees { using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; + using System.Diagnostics.CodeAnalysis; using System.Linq; using ICollection = System.Collections.ICollection; using IList = System.Collections.IList; @@ -94,7 +93,7 @@ public T this[int index] } } - object IList.this[int index] + object? IList.this[int index] { get { @@ -113,10 +112,11 @@ object IList.this[int index] try { - this[index] = (T)value; + this[index] = (T)value!; } catch (InvalidCastException) { + Debug.Assert(value is object, $"Assertion failed: {nameof(value)} is object"); throw new ArgumentException($"The value \"{value.GetType()}\" isn't of type \"{typeof(T)}\" and can't be used in this generic collection.", nameof(value)); } } @@ -136,17 +136,18 @@ public void AddRange(IEnumerable collection) _version++; } - int IList.Add(object value) + int IList.Add(object? value) { if (value == null && default(T) != null) throw new ArgumentNullException(nameof(value)); try { - Add((T)value); + Add((T)value!); } catch (InvalidCastException) { + Debug.Assert(value is object, $"Assertion failed: {nameof(value)} is object"); throw new ArgumentException($"The value \"{value.GetType()}\" isn't of type \"{typeof(T)}\" and can't be used in this generic collection.", nameof(value)); } @@ -167,12 +168,12 @@ public bool Contains(T item) return IndexOf(item) >= 0; } - bool IList.Contains(object value) + bool IList.Contains(object? value) { if (value == null) { if (default(T) == null) - return Contains(default); + return Contains(default!); } else if (value is T) { @@ -225,7 +226,7 @@ void ICollection.CopyTo(Array dest, int index) try { int offset = index; - LeafNode leaf = _root.FirstLeaf; + LeafNode? leaf = _root.FirstLeaf; while (leaf != null) { leaf.CopyToArray(dest, offset); @@ -270,12 +271,12 @@ public int IndexOf(T item, int index, int count) return _root.IndexOf(item, new TreeSpan(index, count)); } - int IList.IndexOf(object value) + int IList.IndexOf(object? value) { if (value == null) { if (default(T) == null) - return IndexOf(default); + return IndexOf(default!); } else if (value is T) { @@ -297,7 +298,7 @@ public void InsertRange(int index, IEnumerable collection) _version++; } - void IList.Insert(int index, object value) + void IList.Insert(int index, object? value) { if (value == null && default(T) != null) throw new ArgumentNullException(nameof(value)); @@ -306,10 +307,11 @@ void IList.Insert(int index, object value) try { - Insert(index, (T)value); + Insert(index, (T)value!); } catch (InvalidCastException) { + Debug.Assert(value is object, $"Assertion failed: {nameof(value)} is object"); throw new ArgumentException(string.Format("The value \"{0}\" isn't of type \"{1}\" and can't be used in this generic collection.", value.GetType(), typeof(T)), nameof(value)); } } @@ -326,7 +328,7 @@ public bool Remove(T item) return false; } - void IList.Remove(object value) + void IList.Remove(object? value) { int index = ((IList)this).IndexOf(value); if (index >= 0) @@ -362,12 +364,12 @@ public int BinarySearch(T item) return BinarySearch(0, Count, item, null); } - public int BinarySearch(T item, IComparer comparer) + public int BinarySearch(T item, IComparer? comparer) { return BinarySearch(0, Count, item, comparer); } - public int BinarySearch(int index, int count, T item, IComparer comparer) + public int BinarySearch(int index, int count, T item, IComparer? comparer) { if (index < 0) throw new ArgumentOutOfRangeException(nameof(index)); @@ -390,6 +392,7 @@ public bool Exists(Predicate match) return FindIndex(match) >= 0; } + [return: MaybeNull] public T Find(Predicate match) { if (match == null) @@ -436,6 +439,7 @@ public int FindIndex(int startIndex, int count, Predicate match) return _root.FindIndex(new TreeSpan(startIndex, count), match); } + [return: MaybeNull] public T FindLast(Predicate match) { int index = FindLastIndex(match); @@ -559,12 +563,12 @@ public void Sort() Sort(0, Count, null); } - public void Sort(IComparer comparer) + public void Sort(IComparer? comparer) { Sort(0, Count, comparer); } - public void Sort(int index, int count, IComparer comparer) + public void Sort(int index, int count, IComparer? comparer) { if (index < 0) throw new ArgumentOutOfRangeException(nameof(index));