├── TunnelVisionLabs.Collections.Trees ├── PublicAPI.Shipped.txt ├── ExceptionUtilities.cs ├── ExcludeFromCodeCoverageAttribute.cs ├── ValidationRules.cs ├── KeyOfPairComparer`2.cs ├── ComparisonComparer`1.cs ├── Immutable │ ├── ImmutableSortedTreeSet`1+Enumerator.cs │ ├── ImmutableTreeSet`1+Enumerator.cs │ ├── ImmutableSortedTreeList`1+Enumerator.cs │ ├── ImmutableTreeQueue`1+Enumerator.cs │ ├── ImmutableTreeStack`1+Enumerator.cs │ ├── ImmutableTreeList.cs │ ├── ImmutableTreeDictionary`2+KeyCollection+Enumerator.cs │ ├── ImmutableTreeDictionary`2+ValueCollection+Enumerator.cs │ ├── ImmutableSortedTreeDictionary`2+KeyCollection+Enumerator.cs │ ├── ImmutableSortedTreeDictionary`2+ValueCollection+Enumerator.cs │ ├── ImmutableTreeStack.cs │ ├── ImmutableTreeQueue.cs │ ├── ImmutableSortedTreeList.cs │ ├── ImmutableTreeStack`1.cs │ ├── ImmutableTreeQueue`1.cs │ ├── ImmutableTreeSet.cs │ ├── ImmutableSortedTreeSet.cs │ ├── ImmutableTreeDictionary`2+Enumerator.cs │ └── ImmutableSortedTreeDictionary`2+Enumerator.cs ├── TreeQueue`1+Enumerator.cs ├── TreeStack`1+Enumerator.cs ├── SortedTreeSet`1+Enumerator.cs ├── SortedTreeList`1+Enumerator.cs ├── TreeSet`1+Enumerator.cs ├── KeyOfPairEqualityComparer`2.cs ├── TreeDictionary`2+KeyCollection+Enumerator.cs ├── TreeDictionary`2+ValueCollection+Enumerator.cs ├── SortedTreeDictionary`2+KeyCollection+Enumerator.cs ├── SortedTreeDictionary`2+ValueCollection+Enumerator.cs ├── TunnelVisionLabs.Collections.Trees.csproj ├── Properties │ └── AssemblyInfo.cs ├── TreeDictionary`2+Enumerator.cs ├── SortedTreeDictionary`2+Enumerator.cs ├── TreeSpan.cs ├── TreeStack`1.cs ├── TreeQueue`1.cs ├── TreeList`1+Enumerator.cs └── TreeDictionary`2+KeyCollection.cs ├── TunnelVisionLabs.Collections.Trees.Experimental ├── PublicAPI.Shipped.txt ├── PublicAPI.Unshipped.txt ├── TunnelVisionLabs.Collections.Trees.Experimental.csproj └── Properties │ └── AssemblyInfo.cs ├── global.json ├── .nuget └── packages.config ├── TunnelVisionLabs.Collections.Trees.Benchmarks ├── App.config ├── Properties │ ├── launchSettings.json │ └── AssemblyInfo.cs ├── Program.cs ├── MemoryDiagnoserConfig.cs ├── DummyBenchmark.cs ├── TunnelVisionLabs.Collections.Trees.Benchmarks.csproj └── Immutable │ └── ImmutableTreeListBenchmark.cs ├── .gitignore ├── NuGet.Config ├── TunnelVisionLabs.Collections.Trees.Test ├── App.config ├── packages.config ├── ExceptionUtilitiesTest.cs ├── EverythingThrowsEnumerable`1.cs ├── Immutable │ ├── ImmutableTreeSetBuilderTest+ImmutableArguments.cs │ ├── ImmutableSortedTreeSetBuilderTest+ImmutableArguments.cs │ ├── FixedArray8Test.cs │ ├── ImmutableTreeListFactoryTest.cs │ ├── ImmutableTreeListTest+ForEach.cs │ └── ImmutableTreeListBuilderTest+ForEach.cs ├── ZeroHashCodeEqualityComparer`1.cs ├── ReverseComparer`1.cs ├── TunnelVisionLabs.Collections.Trees.Test.csproj ├── CollectionAssert.cs ├── Properties │ └── AssemblyInfo.cs ├── List │ ├── TreeListIListIsReadOnly.cs │ ├── TreeListIListIsFixedSize.cs │ ├── TreeListICollectionSyncRoot.cs │ ├── TreeListCtor1.cs │ ├── TreeListICollectionIsSynchronized.cs │ ├── TreeListCount.cs │ ├── TreeListICollectionIsReadOnly.cs │ ├── TreeListIListContains.cs │ ├── TreeListToArray.cs │ ├── TreeListIListRemove.cs │ ├── TreeListClear.cs │ ├── TreeListAdd.cs │ ├── TreeListIEnumerableGetEnumerator.cs │ ├── TreeListIEnumerableGetEnumerator2.cs │ ├── TreeListAddRange.cs │ ├── TreeListCtor2.cs │ ├── TreeListContains.cs │ ├── TreeListGetEnumerator.cs │ ├── TreeListIndexOf1.cs │ ├── TreeListIListAdd.cs │ ├── TreeListRemoveAt.cs │ ├── TreeListIListIndexOf.cs │ ├── TreeListIListItem.cs │ ├── CopyTo1.cs │ ├── TreeListReverse.cs │ ├── TreeListLastIndexOf1.cs │ ├── TreeListRemoveRange.cs │ ├── TreeListForEach.cs │ ├── TreeListIndexOf2.cs │ ├── TreeListLastIndexOf2.cs │ └── BinarySearch1.cs ├── SubsetHashCodeEqualityComparer`1.cs ├── ReverseComparerTests.cs ├── EverythingThrowsEnumerableTest.cs ├── GeneratorTest.cs ├── Generator.cs └── ThrowingTraceListener.cs ├── .gitattributes ├── version.json ├── codecov.yml ├── stylecop.json ├── TunnelVisionLabs.Collections.Trees.Internal.ruleset ├── appveyor.yml ├── LICENSE ├── Directory.Build.targets ├── README.md └── TunnelVisionLabs.Collections.Trees.ruleset /TunnelVisionLabs.Collections.Trees/PublicAPI.Shipped.txt: -------------------------------------------------------------------------------- 1 | #nullable enable 2 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Experimental/PublicAPI.Shipped.txt: -------------------------------------------------------------------------------- 1 | #nullable enable 2 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Experimental/PublicAPI.Unshipped.txt: -------------------------------------------------------------------------------- 1 | #nullable enable 2 | -------------------------------------------------------------------------------- /global.json: -------------------------------------------------------------------------------- 1 | { 2 | "sdk": { 3 | "version": "3.1.400", 4 | "rollForward": "latestPatch" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /.nuget/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Benchmarks/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Build and test output 2 | packages/ 3 | bin/ 4 | obj/ 5 | 6 | # Per-user files created by IDEs 7 | *.suo 8 | *.user 9 | .vs/ 10 | 11 | # Created by the build script 12 | build/nuget/ 13 | .nuget/nuget.exe 14 | /TunnelVisionLabs.Collections.Trees.Benchmarks/BenchmarkDotNet.Artifacts/* 15 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Benchmarks/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "Benchmark (All)": { 4 | "commandName": "Project", 5 | "commandLineArgs": "--filter *" 6 | }, 7 | "Benchmark (Interactive)": { 8 | "commandName": "Project" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /NuGet.Config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set the default behavior (used when a rule below doesn't match) 2 | * text=auto 3 | 4 | # Source code files 5 | *.cs text 6 | *.config text 7 | *.resx text 8 | *.nuspec text 9 | *.ps1 text 10 | *.cd text 11 | 12 | # Text files 13 | *.md text 14 | *.txt text 15 | LICENSE text 16 | 17 | # Projects and solutions 18 | *.sln text 19 | *.csproj text 20 | *.yml text 21 | *.StyleCop text 22 | *.ruleset 23 | 24 | # Known binary files 25 | *.snk binary 26 | -------------------------------------------------------------------------------- /version.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", 3 | "version": "1.0.0-alpha.{height}", 4 | "assemblyVersion": { 5 | "precision": "minor" 6 | }, 7 | "publicReleaseRefSpec": [ 8 | "^refs/heads/master$" 9 | ], 10 | "nugetPackageVersion": { 11 | "semVer": 2 12 | }, 13 | "cloudBuild": { 14 | "buildNumber": { 15 | "enabled": true 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/ExceptionUtilities.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System; 7 | 8 | internal static class ExceptionUtilities 9 | { 10 | public static Exception Unreachable => new InvalidOperationException("This code should not be reachable."); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/ExcludeFromCodeCoverageAttribute.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | #if NETSTANDARD1_1 5 | 6 | namespace System.Diagnostics.CodeAnalysis 7 | { 8 | [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] 9 | internal sealed class ExcludeFromCodeCoverageAttribute : Attribute 10 | { 11 | } 12 | } 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Benchmarks/Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Benchmarks 5 | { 6 | using BenchmarkDotNet.Running; 7 | 8 | internal class Program 9 | { 10 | private static void Main(string[] args) 11 | { 12 | new BenchmarkSwitcher(typeof(Program).Assembly).Run(args); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Benchmarks/MemoryDiagnoserConfig.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Benchmarks 5 | { 6 | using BenchmarkDotNet.Configs; 7 | using BenchmarkDotNet.Diagnosers; 8 | 9 | public class MemoryDiagnoserConfig : ManualConfig 10 | { 11 | public MemoryDiagnoserConfig() 12 | { 13 | Add(MemoryDiagnoser.Default); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | # https://docs.codecov.io/docs/codecov-yaml 2 | # https://github.com/codecov/support/wiki/Codecov-Yaml 3 | 4 | coverage: 5 | status: 6 | project: 7 | default: false 8 | patch: 9 | default: false 10 | 11 | comment: 12 | layout: "diff" 13 | 14 | flags: 15 | production: 16 | paths: 17 | - TunnelVisionLabs.Collections.Trees/ 18 | - TunnelVisionLabs.Collections.Trees.Experimental/ 19 | test: 20 | paths: 21 | - TunnelVisionLabs.Collections.Trees.Test/ 22 | # Filter results to only show debug reports until the accuracy problems with 23 | # the report merge process are fixed. 24 | Release: 25 | paths: 26 | - nonexistent/ 27 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/ValidationRules.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System; 7 | 8 | [Flags] 9 | internal enum ValidationRules 10 | { 11 | /// 12 | /// No special validation rules apply. 13 | /// 14 | None = 0, 15 | 16 | /// 17 | /// The tree is packed (i.e. there is empty space except at the end of each level). 18 | /// 19 | RequirePacked = 0b0001, 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Benchmarks/DummyBenchmark.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Benchmarks 5 | { 6 | using BenchmarkDotNet.Attributes; 7 | 8 | [ShortRunJob] 9 | public class DummyBenchmark 10 | { 11 | private static readonly int[] Ints = { 1, 2, 3 }; 12 | 13 | [Benchmark(Baseline = true)] 14 | public object NewTreeList1() 15 | { 16 | return new TreeList(Ints); 17 | } 18 | 19 | [Benchmark] 20 | public object NewTreeList2() 21 | { 22 | return new TreeList(Ints); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Benchmarks/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | using System.Reflection; 6 | using System.Runtime.InteropServices; 7 | 8 | // General Information about an assembly is controlled through the following 9 | // set of attributes. Change these attribute values to modify the information 10 | // associated with an assembly. 11 | [assembly: AssemblyTrademark("")] 12 | [assembly: AssemblyCulture("")] 13 | [assembly: CLSCompliant(false)] 14 | [assembly: ComVisible(false)] 15 | 16 | [assembly: BenchmarkDotNet.Attributes.Config(typeof(TunnelVisionLabs.Collections.Trees.Benchmarks.MemoryDiagnoserConfig))] 17 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/ExceptionUtilitiesTest.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System; 7 | using Xunit; 8 | 9 | public class ExceptionUtilitiesTest 10 | { 11 | [Fact] 12 | public void TestUnreachable() 13 | { 14 | // Make sure it has the correct type 15 | Assert.IsAssignableFrom(ExceptionUtilities.Unreachable); 16 | 17 | // Make sure a new instance is created each time it's accessed 18 | Assert.NotSame(ExceptionUtilities.Unreachable, ExceptionUtilities.Unreachable); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /stylecop.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", 3 | "settings": { 4 | "documentationRules": { 5 | "companyName": "Tunnel Vision Laboratories, LLC", 6 | "copyrightText": "Copyright (c) {companyName}. All Rights Reserved.\r\nLicensed under the MIT License. See LICENSE in the project root for license information.", 7 | "xmlHeader": false, 8 | "fileNamingConvention": "metadata" 9 | }, 10 | "layoutRules": { 11 | "newlineAtEndOfFile": "require" 12 | }, 13 | "orderingRules": { 14 | // Ignore accessibility for determining the element order 15 | "elementOrder": [ "kind", "constant" ] 16 | }, 17 | "namingRules": { 18 | "tupleElementNameCasing": "camelCase" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/EverythingThrowsEnumerable`1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | 10 | public sealed class EverythingThrowsEnumerable : IEnumerable 11 | { 12 | public static readonly EverythingThrowsEnumerable Instance = new EverythingThrowsEnumerable(); 13 | 14 | private EverythingThrowsEnumerable() 15 | { 16 | } 17 | 18 | public IEnumerator GetEnumerator() => throw new NotSupportedException(); 19 | 20 | IEnumerator IEnumerable.GetEnumerator() => throw new NotSupportedException(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Experimental/TunnelVisionLabs.Collections.Trees.Experimental.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | net45;netstandard1.1;netstandard2.0;netstandard2.1 6 | TunnelVisionLabs.Collections.Trees 7 | true 8 | 9 | 10 | 11 | ..\TunnelVisionLabs.Collections.Trees.ruleset 12 | 13 | 14 | 15 | true 16 | ..\build\keys\UnitTestingKey.snk 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Internal.ruleset: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/Immutable/ImmutableTreeSetBuilderTest+ImmutableArguments.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.Immutable 5 | { 6 | using System.Collections.Generic; 7 | using TunnelVisionLabs.Collections.Trees.Immutable; 8 | 9 | public partial class ImmutableTreeSetBuilderTest 10 | { 11 | public class ImmutableArguments : AbstractSetTest 12 | { 13 | protected override ISet CreateSet() 14 | { 15 | return ImmutableTreeSet.CreateBuilder(); 16 | } 17 | 18 | protected override IEnumerable TransformEnumerableForSetOperation(IEnumerable enumerable) 19 | { 20 | return ImmutableTreeSet.CreateRange(enumerable); 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/ZeroHashCodeEqualityComparer`1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System.Collections.Generic; 7 | using System.Diagnostics.CodeAnalysis; 8 | 9 | internal sealed class ZeroHashCodeEqualityComparer : IEqualityComparer 10 | { 11 | public static readonly ZeroHashCodeEqualityComparer Default = new ZeroHashCodeEqualityComparer(null); 12 | private readonly IEqualityComparer _comparer; 13 | 14 | public ZeroHashCodeEqualityComparer(IEqualityComparer? comparer) 15 | { 16 | _comparer = comparer ?? EqualityComparer.Default; 17 | } 18 | 19 | public bool Equals([AllowNull] T x, [AllowNull] T y) => _comparer.Equals(x, y); 20 | 21 | public int GetHashCode(T obj) => 0; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/Immutable/ImmutableSortedTreeSetBuilderTest+ImmutableArguments.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.Immutable 5 | { 6 | using System.Collections.Generic; 7 | using TunnelVisionLabs.Collections.Trees.Immutable; 8 | 9 | public partial class ImmutableSortedTreeSetBuilderTest 10 | { 11 | public class ImmutableArguments : AbstractSetTest 12 | { 13 | protected override ISet CreateSet() 14 | { 15 | return ImmutableSortedTreeSet.CreateBuilder(); 16 | } 17 | 18 | protected override IEnumerable TransformEnumerableForSetOperation(IEnumerable enumerable) 19 | { 20 | return ImmutableSortedTreeSet.CreateRange(enumerable); 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Experimental/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | using System.Reflection; 6 | using System.Runtime.CompilerServices; 7 | 8 | // General Information about an assembly is controlled through the following 9 | // set of attributes. Change these attribute values to modify the information 10 | // associated with an assembly. 11 | [assembly: AssemblyTrademark("")] 12 | [assembly: AssemblyCulture("")] 13 | [assembly: CLSCompliant(true)] 14 | 15 | [assembly: InternalsVisibleTo("TunnelVisionLabs.Collections.Trees.Test, PublicKey=002400000480000094000000060200000024000052534131000400000100010063bb30979a5f932c6ecc131ea6e9cf2e2a50a8476dd94a803452df56f75882df32a98244f6b162da6873515f5cd84217bb773aa18053fb4b2c4a74b60eb8c4d7dce9fa916819c8a85f4df559d9a10d9d89c1db47e5f18730b2047b636220156b9c3761acd9588f566b14d84ba110be8c4aa52868c0204e2ee2b51430a9611cd0")] 16 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/ReverseComparer`1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System.Collections.Generic; 7 | using System.Diagnostics.CodeAnalysis; 8 | 9 | internal sealed class ReverseComparer : IComparer 10 | { 11 | public static readonly ReverseComparer Default = new ReverseComparer(null); 12 | 13 | private readonly IComparer _comparer; 14 | 15 | public ReverseComparer(IComparer? comparer) 16 | { 17 | _comparer = comparer ?? Comparer.Default; 18 | } 19 | 20 | public int Compare([AllowNull] T x, [AllowNull] T y) 21 | { 22 | var direct = _comparer.Compare(x, y); 23 | if (direct == int.MinValue) 24 | return int.MaxValue; 25 | 26 | return -direct; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/KeyOfPairComparer`2.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections.Generic; 7 | using System.Diagnostics; 8 | 9 | internal sealed class KeyOfPairComparer : IComparer> 10 | { 11 | internal KeyOfPairComparer(IComparer comparer) 12 | { 13 | Debug.Assert(comparer != null, $"Assertion failed: {nameof(comparer)} != null"); 14 | KeyComparer = comparer; 15 | } 16 | 17 | internal static KeyOfPairComparer Default { get; } 18 | = new KeyOfPairComparer(Comparer.Default); 19 | 20 | internal IComparer KeyComparer { get; } 21 | 22 | public int Compare(KeyValuePair x, KeyValuePair y) 23 | => KeyComparer.Compare(x.Key, y.Key); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: '{build}' 2 | image: Visual Studio 2019 3 | configuration: 4 | - Debug 5 | - Release 6 | init: 7 | - git config --global core.autocrlf true 8 | before_build: 9 | - nuget restore 10 | skip_tags: true 11 | build: 12 | project: dotnet-trees.sln 13 | verbosity: minimal 14 | test_script: 15 | - .\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -register:Path32 -target:"%xunit20%\xunit.console.x86.exe" -targetargs:"C:\projects\dotnet-trees\TunnelVisionLabs.Collections.Trees.Test\bin\%configuration%\net452\TunnelVisionLabs.Collections.Trees.Test.dll -noshadow -appveyor" -threshold:1 -oldStyle -returntargetcode -filter:"+[TunnelVisionLabs*]*" -excludebyattribute:*.ExcludeFromCodeCoverage* -excludebyfile:*\*Designer.cs -hideskipped:All -output:.\trees_coverage.xml 16 | - .\packages\Codecov.1.12.1\tools\codecov.exe -n "%configuration%" -f "trees_coverage.xml" --flag "%configuration%" 17 | cache: 18 | - packages -> **\packages.config 19 | artifacts: 20 | - path: 'TunnelVisionLabs.Collections.Trees\**\*.nupkg' 21 | - path: 'TunnelVisionLabs.Collections.Trees.Experimental\**\*.nupkg' 22 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/ComparisonComparer`1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Diagnostics; 9 | using System.Diagnostics.CodeAnalysis; 10 | 11 | internal sealed class ComparisonComparer : IComparer 12 | { 13 | private readonly Comparison _comparison; 14 | 15 | public ComparisonComparer(Comparison comparison) 16 | { 17 | Debug.Assert(comparison != null, $"Assertion failed: {nameof(comparison)} != null"); 18 | _comparison = comparison; 19 | } 20 | 21 | #pragma warning disable CS8604 // Possible null reference argument. (.NET 5 corrected the signature of Comparison) 22 | public int Compare([AllowNull] T x, [AllowNull] T y) => _comparison(x, y); 23 | #pragma warning restore CS8604 // Possible null reference argument. 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/TunnelVisionLabs.Collections.Trees.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | net452 6 | 7 | 8 | 9 | ..\TunnelVisionLabs.Collections.Trees.Internal.ruleset 10 | 11 | 12 | 13 | true 14 | ..\build\keys\UnitTestingKey.snk 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/CollectionAssert.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | public static class CollectionAssert 11 | { 12 | public static void EnumeratorInvalidated(IEnumerable enumerable, Action action) 13 | { 14 | using (IEnumerator enumerator = enumerable.GetEnumerator()) 15 | { 16 | action(); 17 | Assert.Throws(() => enumerator.MoveNext()); 18 | } 19 | } 20 | 21 | public static void EnumeratorNotInvalidated(IEnumerable enumerable, Action action) 22 | { 23 | using (IEnumerator enumerator = enumerable.GetEnumerator()) 24 | { 25 | action(); 26 | enumerator.MoveNext(); 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableSortedTreeSet`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class ImmutableSortedTreeSet 10 | { 11 | public struct Enumerator : IEnumerator 12 | { 13 | private ImmutableSortedTreeList.Enumerator _enumerator; 14 | 15 | internal Enumerator(ImmutableSortedTreeList.Enumerator enumerator) 16 | { 17 | _enumerator = enumerator; 18 | } 19 | 20 | public T Current => _enumerator.Current; 21 | 22 | object? IEnumerator.Current => Current; 23 | 24 | public void Dispose() => _enumerator.Dispose(); 25 | 26 | public bool MoveNext() => _enumerator.MoveNext(); 27 | 28 | public void Reset() => _enumerator.Reset(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeQueue`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class TreeQueue 10 | { 11 | public struct Enumerator : IEnumerator 12 | { 13 | private TreeList.Enumerator _enumerator; 14 | 15 | internal Enumerator(TreeList.Enumerator enumerator) 16 | { 17 | _enumerator = enumerator; 18 | } 19 | 20 | public T Current => _enumerator.Current; 21 | 22 | object? IEnumerator.Current => _enumerator.Current; 23 | 24 | public void Dispose() => _enumerator.Dispose(); 25 | 26 | public bool MoveNext() => _enumerator.MoveNext(); 27 | 28 | void IEnumerator.Reset() => InternalReset(); 29 | 30 | internal void InternalReset() => _enumerator.InternalReset(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeStack`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class TreeStack 10 | { 11 | public struct Enumerator : IEnumerator 12 | { 13 | private TreeList.Enumerator _enumerator; 14 | 15 | internal Enumerator(TreeList.Enumerator enumerator) 16 | { 17 | _enumerator = enumerator; 18 | } 19 | 20 | public T Current => _enumerator.Current; 21 | 22 | object? IEnumerator.Current => _enumerator.Current; 23 | 24 | public void Dispose() => _enumerator.Dispose(); 25 | 26 | public bool MoveNext() => _enumerator.MoveNext(); 27 | 28 | void IEnumerator.Reset() => InternalReset(); 29 | 30 | internal void InternalReset() => _enumerator.InternalReset(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Tunnel Vision Laboratories, LLC 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/SortedTreeSet`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class SortedTreeSet 10 | { 11 | public struct Enumerator : IEnumerator 12 | { 13 | private SortedTreeList.Enumerator _enumerator; 14 | 15 | internal Enumerator(SortedTreeList.Enumerator enumerator) 16 | { 17 | _enumerator = enumerator; 18 | } 19 | 20 | public T Current => _enumerator.Current; 21 | 22 | object? IEnumerator.Current => Current; 23 | 24 | public void Dispose() => _enumerator.Dispose(); 25 | 26 | public bool MoveNext() => _enumerator.MoveNext(); 27 | 28 | void IEnumerator.Reset() => InternalReset(); 29 | 30 | internal void InternalReset() => _enumerator.InternalReset(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeSet`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class ImmutableTreeSet 10 | { 11 | public struct Enumerator : IEnumerator 12 | { 13 | private ImmutableSortedTreeList<(int hashCode, T value)>.Enumerator _enumerator; 14 | 15 | internal Enumerator(ImmutableSortedTreeList<(int hashCode, T value)>.Enumerator enumerator) 16 | { 17 | _enumerator = enumerator; 18 | } 19 | 20 | public T Current => _enumerator.Current.value; 21 | 22 | object? IEnumerator.Current => Current; 23 | 24 | public void Dispose() => _enumerator.Dispose(); 25 | 26 | public bool MoveNext() => _enumerator.MoveNext(); 27 | 28 | public void Reset() => _enumerator.Reset(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableSortedTreeList`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | internal sealed partial class ImmutableSortedTreeList 10 | { 11 | public struct Enumerator : IEnumerator 12 | { 13 | private ImmutableTreeList.Enumerator _enumerator; 14 | 15 | internal Enumerator(ImmutableTreeList.Enumerator enumerator) 16 | { 17 | _enumerator = enumerator; 18 | } 19 | 20 | public T Current => _enumerator.Current; 21 | 22 | object? IEnumerator.Current => Current; 23 | 24 | public void Dispose() 25 | => _enumerator.Dispose(); 26 | 27 | public bool MoveNext() 28 | => _enumerator.MoveNext(); 29 | 30 | public void Reset() 31 | => _enumerator.Reset(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/SortedTreeList`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | 10 | public partial class SortedTreeList 11 | { 12 | public struct Enumerator : IEnumerator 13 | { 14 | private TreeList.Enumerator _enumerator; 15 | 16 | internal Enumerator(TreeList.Enumerator enumerator) 17 | { 18 | _enumerator = enumerator; 19 | } 20 | 21 | public T Current => _enumerator.Current; 22 | 23 | object? IEnumerator.Current => Current; 24 | 25 | public void Dispose() => ((IDisposable)_enumerator).Dispose(); 26 | 27 | public bool MoveNext() => _enumerator.MoveNext(); 28 | 29 | void IEnumerator.Reset() => InternalReset(); 30 | 31 | internal void InternalReset() => _enumerator.InternalReset(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeSet`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class TreeSet 10 | { 11 | public struct Enumerator : IEnumerator 12 | { 13 | private SortedTreeList<(int hashCode, T value)>.Enumerator _enumerator; 14 | 15 | internal Enumerator(SortedTreeList<(int hashCode, T value)>.Enumerator enumerator) 16 | { 17 | _enumerator = enumerator; 18 | } 19 | 20 | public T Current => _enumerator.Current.value; 21 | 22 | object? IEnumerator.Current => Current; 23 | 24 | public void Dispose() => _enumerator.Dispose(); 25 | 26 | public bool MoveNext() => _enumerator.MoveNext(); 27 | 28 | void IEnumerator.Reset() => InternalReset(); 29 | 30 | internal void InternalReset() => _enumerator.InternalReset(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | using System.Reflection; 6 | using System.Runtime.InteropServices; 7 | using Xunit; 8 | 9 | // General Information about an assembly is controlled through the following 10 | // set of attributes. Change these attribute values to modify the information 11 | // associated with an assembly. 12 | [assembly: AssemblyTrademark("")] 13 | [assembly: AssemblyCulture("")] 14 | [assembly: CLSCompliant(false)] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("73541434-2511-441c-bbaf-05044be33019")] 23 | 24 | // Disable test parallelization since GeneratorTest manipulates mutable shared data 25 | [assembly: CollectionBehavior(DisableTestParallelization = true)] 26 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeQueue`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | 10 | public partial class ImmutableTreeQueue 11 | { 12 | public struct Enumerator : IEnumerator 13 | { 14 | private ImmutableTreeList.Enumerator _enumerator; 15 | 16 | internal Enumerator(ImmutableTreeList.Enumerator enumerator) 17 | { 18 | _enumerator = enumerator; 19 | } 20 | 21 | public T Current => _enumerator.Current; 22 | 23 | object? IEnumerator.Current => _enumerator.Current; 24 | 25 | public bool MoveNext() => _enumerator.MoveNext(); 26 | 27 | void IDisposable.Dispose() => _enumerator.Dispose(); 28 | 29 | void IEnumerator.Reset() => InternalReset(); 30 | 31 | internal void InternalReset() => _enumerator.Reset(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeStack`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | 10 | public partial class ImmutableTreeStack 11 | { 12 | public struct Enumerator : IEnumerator 13 | { 14 | private ImmutableTreeList.Enumerator _enumerator; 15 | 16 | internal Enumerator(ImmutableTreeList.Enumerator enumerator) 17 | { 18 | _enumerator = enumerator; 19 | } 20 | 21 | public T Current => _enumerator.Current; 22 | 23 | object? IEnumerator.Current => _enumerator.Current; 24 | 25 | public bool MoveNext() => _enumerator.MoveNext(); 26 | 27 | void IDisposable.Dispose() => _enumerator.Dispose(); 28 | 29 | void IEnumerator.Reset() => InternalReset(); 30 | 31 | internal void InternalReset() => _enumerator.Reset(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Benchmarks/TunnelVisionLabs.Collections.Trees.Benchmarks.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | net461 6 | Exe 7 | true 8 | 9 | 10 | 11 | ..\TunnelVisionLabs.Collections.Trees.Internal.ruleset 12 | 13 | 14 | 15 | true 16 | ..\build\keys\UnitTestingKey.snk 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Directory.Build.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIsReadOnly.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for the implementation of , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListIListIsReadOnly 15 | { 16 | [Fact(DisplayName = "PosTest1: this IsFixedSize property always returns false.")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; 20 | TreeList listObject = new TreeList(iArray); 21 | Assert.False(((IList)listObject).IsReadOnly); 22 | 23 | string[] sArray = { "1", "9", "3", "6", "5", "8", "7", "2", "4", "0" }; 24 | TreeList listObject1 = new TreeList(sArray); 25 | Assert.False(((IList)listObject).IsReadOnly); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIsFixedSize.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for the implementation of , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListIListIsFixedSize 15 | { 16 | [Fact(DisplayName = "PosTest1: this IsFixedSize property always returns false.")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; 20 | TreeList listObject = new TreeList(iArray); 21 | Assert.False(((IList)listObject).IsFixedSize); 22 | 23 | string[] sArray = { "1", "9", "3", "6", "5", "8", "7", "2", "4", "0" }; 24 | TreeList listObject1 = new TreeList(sArray); 25 | Assert.False(((IList)listObject).IsFixedSize); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/Immutable/FixedArray8Test.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.Immutable 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using TunnelVisionLabs.Collections.Trees.Immutable; 9 | using Xunit; 10 | 11 | public class FixedArray8Test 12 | { 13 | [Fact] 14 | public void TestBoundsCheck() 15 | { 16 | FixedArray8 array = default; 17 | Assert.Throws(() => array[-1]); 18 | Assert.Throws(() => array[-1] = 0); 19 | Assert.Throws(() => array[array.Length]); 20 | Assert.Throws(() => array[array.Length] = 0); 21 | } 22 | 23 | [Fact] 24 | public void TestSortSmallRange() 25 | { 26 | FixedArray8 array = default; 27 | array.Sort(0, 0, Comparer.Default); 28 | array.Sort(1, 1, Comparer.Default); 29 | Assert.Equal(default, array); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeList.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections.Generic; 7 | 8 | public static class ImmutableTreeList 9 | { 10 | public static ImmutableTreeList Create() => ImmutableTreeList.Empty; 11 | 12 | public static ImmutableTreeList Create(T item) => ImmutableTreeList.Empty.Add(item); 13 | 14 | public static ImmutableTreeList Create(params T[] items) => ImmutableTreeList.Empty.AddRange(items); 15 | 16 | public static ImmutableTreeList.Builder CreateBuilder() => Create().ToBuilder(); 17 | 18 | public static ImmutableTreeList CreateRange(IEnumerable items) => ImmutableTreeList.Empty.AddRange(items); 19 | 20 | public static ImmutableTreeList ToImmutableTreeList(this IEnumerable source) 21 | { 22 | if (source is ImmutableTreeList existingList) 23 | return existingList; 24 | 25 | return ImmutableTreeList.Empty.AddRange(source); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # .NET Trees 2 | 3 | Efficient implementations of standard .NET collection interfaces using tree data structures. 4 | 5 | [![Build status](https://ci.appveyor.com/api/projects/status/wdrb3wg41lomsaxt/branch/master?svg=true)](https://ci.appveyor.com/project/sharwell/dotnet-trees/branch/master) 6 | 7 | [![codecov](https://codecov.io/gh/tunnelvisionlabs/dotnet-trees/branch/master/graph/badge.svg)](https://codecov.io/gh/tunnelvisionlabs/dotnet-trees) 8 | 9 | [![Join the chat at https://gitter.im/tunnelvisionlabs/dotnet-trees](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/tunnelvisionlabs/dotnet-trees?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 10 | 11 | ## Releases 12 | 13 | [![NuGet](https://img.shields.io/nuget/v/TunnelVisionLabs.Collections.Trees.svg)](https://www.nuget.org/packages/TunnelVisionLabs.Collections.Trees) [![NuGet Beta](https://img.shields.io/nuget/vpre/TunnelVisionLabs.Collections.Trees.svg)](https://www.nuget.org/packages/TunnelVisionLabs.Collections.Trees/absoluteLatest) 14 | 15 | * [Binaries (NuGet)](https://www.nuget.org/packages/TunnelVisionLabs.Collections.Trees) 16 | * [Release Notes](https://github.com/tunnelvisionlabs/dotnet-trees/releases) 17 | * [License](https://github.com/tunnelvisionlabs/dotnet-trees/blob/master/LICENSE) 18 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/SubsetHashCodeEqualityComparer`1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Diagnostics.CodeAnalysis; 9 | 10 | internal sealed class SubsetHashCodeEqualityComparer : IEqualityComparer 11 | { 12 | private readonly IEqualityComparer _equalityComparer; 13 | private readonly Func _getHashCode; 14 | 15 | public SubsetHashCodeEqualityComparer(IEqualityComparer equalityComparer, IEqualityComparer hashCodeEqualityComparer) 16 | : this(equalityComparer, hashCodeEqualityComparer.GetHashCode) 17 | { 18 | } 19 | 20 | public SubsetHashCodeEqualityComparer(IEqualityComparer equalityComparer, Func getHashCode) 21 | { 22 | _equalityComparer = equalityComparer; 23 | _getHashCode = getHashCode; 24 | } 25 | 26 | public bool Equals([AllowNull] T x, [AllowNull] T y) => _equalityComparer.Equals(x, y); 27 | 28 | public int GetHashCode(T obj) => _getHashCode(obj); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeDictionary`2+KeyCollection+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class ImmutableTreeDictionary 10 | { 11 | public partial struct KeyCollection 12 | { 13 | public struct Enumerator : IEnumerator 14 | { 15 | private ImmutableTreeDictionary.Enumerator _enumerator; 16 | 17 | internal Enumerator(ImmutableTreeDictionary.Enumerator enumerator) 18 | { 19 | _enumerator = enumerator; 20 | } 21 | 22 | public TKey Current => _enumerator.Current.Key; 23 | 24 | object IEnumerator.Current => Current; 25 | 26 | public void Dispose() => _enumerator.Dispose(); 27 | 28 | public bool MoveNext() => _enumerator.MoveNext(); 29 | 30 | public void Reset() => _enumerator.Reset(); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeDictionary`2+ValueCollection+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class ImmutableTreeDictionary 10 | { 11 | public partial struct ValueCollection 12 | { 13 | public struct Enumerator : IEnumerator 14 | { 15 | private ImmutableTreeDictionary.Enumerator _enumerator; 16 | 17 | internal Enumerator(ImmutableTreeDictionary.Enumerator enumerator) 18 | { 19 | _enumerator = enumerator; 20 | } 21 | 22 | public TValue Current => _enumerator.Current.Value; 23 | 24 | object? IEnumerator.Current => Current; 25 | 26 | public void Dispose() => _enumerator.Dispose(); 27 | 28 | public bool MoveNext() => _enumerator.MoveNext(); 29 | 30 | public void Reset() => _enumerator.Reset(); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableSortedTreeDictionary`2+KeyCollection+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class ImmutableSortedTreeDictionary 10 | { 11 | public partial struct KeyCollection 12 | { 13 | public struct Enumerator : IEnumerator 14 | { 15 | private ImmutableSortedTreeDictionary.Enumerator _enumerator; 16 | 17 | internal Enumerator(ImmutableSortedTreeDictionary.Enumerator enumerator) 18 | { 19 | _enumerator = enumerator; 20 | } 21 | 22 | public TKey Current => _enumerator.Current.Key; 23 | 24 | object IEnumerator.Current => Current; 25 | 26 | public void Dispose() => _enumerator.Dispose(); 27 | 28 | public bool MoveNext() => _enumerator.MoveNext(); 29 | 30 | public void Reset() => _enumerator.Reset(); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/KeyOfPairEqualityComparer`2.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections.Generic; 7 | using System.Diagnostics; 8 | 9 | internal sealed class KeyOfPairEqualityComparer : IEqualityComparer> 10 | { 11 | internal KeyOfPairEqualityComparer(IEqualityComparer comparer) 12 | { 13 | Debug.Assert(comparer != null, $"Assertion failed: {nameof(comparer)} != null"); 14 | KeyComparer = comparer; 15 | } 16 | 17 | internal static KeyOfPairEqualityComparer Default { get; } 18 | = new KeyOfPairEqualityComparer(EqualityComparer.Default); 19 | 20 | internal IEqualityComparer KeyComparer { get; } 21 | 22 | public bool Equals(KeyValuePair x, KeyValuePair y) 23 | { 24 | return KeyComparer.Equals(x.Key, y.Key); 25 | } 26 | 27 | public int GetHashCode(KeyValuePair obj) 28 | { 29 | return KeyComparer.GetHashCode(obj.Key); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableSortedTreeDictionary`2+ValueCollection+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class ImmutableSortedTreeDictionary 10 | { 11 | public partial struct ValueCollection 12 | { 13 | public struct Enumerator : IEnumerator 14 | { 15 | private ImmutableSortedTreeDictionary.Enumerator _enumerator; 16 | 17 | internal Enumerator(ImmutableSortedTreeDictionary.Enumerator enumerator) 18 | { 19 | _enumerator = enumerator; 20 | } 21 | 22 | public TValue Current => _enumerator.Current.Value; 23 | 24 | object? IEnumerator.Current => Current; 25 | 26 | public void Dispose() => _enumerator.Dispose(); 27 | 28 | public bool MoveNext() => _enumerator.MoveNext(); 29 | 30 | public void Reset() => _enumerator.Reset(); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeDictionary`2+KeyCollection+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class TreeDictionary 10 | { 11 | public partial struct KeyCollection 12 | { 13 | public struct Enumerator : IEnumerator 14 | { 15 | private TreeDictionary.Enumerator _enumerator; 16 | 17 | internal Enumerator(TreeDictionary.Enumerator enumerator) 18 | { 19 | _enumerator = enumerator; 20 | } 21 | 22 | public TKey Current => _enumerator.Current.Key; 23 | 24 | object IEnumerator.Current => Current; 25 | 26 | public void Dispose() => _enumerator.Dispose(); 27 | 28 | public bool MoveNext() => _enumerator.MoveNext(); 29 | 30 | void IEnumerator.Reset() => InternalReset(); 31 | 32 | internal void InternalReset() => _enumerator.InternalReset(); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeDictionary`2+ValueCollection+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class TreeDictionary 10 | { 11 | public partial struct ValueCollection 12 | { 13 | public struct Enumerator : IEnumerator 14 | { 15 | private TreeDictionary.Enumerator _enumerator; 16 | 17 | internal Enumerator(TreeDictionary.Enumerator enumerator) 18 | { 19 | _enumerator = enumerator; 20 | } 21 | 22 | public TValue Current => _enumerator.Current.Value; 23 | 24 | object? IEnumerator.Current => Current; 25 | 26 | public void Dispose() => _enumerator.Dispose(); 27 | 28 | public bool MoveNext() => _enumerator.MoveNext(); 29 | 30 | void IEnumerator.Reset() => InternalReset(); 31 | 32 | internal void InternalReset() => _enumerator.InternalReset(); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/SortedTreeDictionary`2+KeyCollection+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class SortedTreeDictionary 10 | { 11 | public partial struct KeyCollection 12 | { 13 | public struct Enumerator : IEnumerator 14 | { 15 | private SortedTreeDictionary.Enumerator _enumerator; 16 | 17 | internal Enumerator(SortedTreeDictionary.Enumerator enumerator) 18 | { 19 | _enumerator = enumerator; 20 | } 21 | 22 | public TKey Current => _enumerator.Current.Key; 23 | 24 | object IEnumerator.Current => Current; 25 | 26 | public void Dispose() => _enumerator.Dispose(); 27 | 28 | public bool MoveNext() => _enumerator.MoveNext(); 29 | 30 | void IEnumerator.Reset() => InternalReset(); 31 | 32 | internal void InternalReset() => _enumerator.InternalReset(); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/SortedTreeDictionary`2+ValueCollection+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public partial class SortedTreeDictionary 10 | { 11 | public partial struct ValueCollection 12 | { 13 | public struct Enumerator : IEnumerator 14 | { 15 | private SortedTreeDictionary.Enumerator _enumerator; 16 | 17 | internal Enumerator(SortedTreeDictionary.Enumerator enumerator) 18 | { 19 | _enumerator = enumerator; 20 | } 21 | 22 | public TValue Current => _enumerator.Current.Value; 23 | 24 | object? IEnumerator.Current => Current; 25 | 26 | public void Dispose() => _enumerator.Dispose(); 27 | 28 | public bool MoveNext() => _enumerator.MoveNext(); 29 | 30 | void IEnumerator.Reset() => InternalReset(); 31 | 32 | internal void InternalReset() => _enumerator.InternalReset(); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TunnelVisionLabs.Collections.Trees.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | net45;netstandard1.1;netstandard2.0;netstandard2.1 6 | true 7 | 8 | 9 | 10 | true 11 | 12 | 13 | 14 | ..\TunnelVisionLabs.Collections.Trees.ruleset 15 | 16 | 17 | 18 | true 19 | ..\build\keys\TunnelVisionLabs.Collections.Trees.snk 20 | ..\build\keys\TunnelVisionLabs.Collections.Trees.dev.snk 21 | 22 | 23 | 24 | $(DefineConstants);DEVELOPMENT_KEY 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionSyncRoot.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for the implementation of , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListICollectionSyncRoot 15 | { 16 | [Fact(DisplayName = "PosTest1: this SyncRoot property always returns the current instance.")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; 20 | TreeList listObject = new TreeList(iArray); 21 | object actualValue = ((ICollection)listObject).SyncRoot; 22 | Assert.NotNull(actualValue); 23 | 24 | string[] sArray = { "1", "9", "3", "6", "5", "8", "7", "2", "4", "0" }; 25 | TreeList listObject1 = new TreeList(sArray); 26 | actualValue = ((ICollection)listObject1).SyncRoot; 27 | Assert.NotNull(actualValue); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/ReverseComparerTests.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | public class ReverseComparerTests 10 | { 11 | private static readonly IComparer DifferencingComparer = 12 | new ComparisonComparer((x, y) => x - y); 13 | 14 | [Fact] 15 | public void TestMinValueHandling() 16 | { 17 | Assert.Equal(int.MinValue, DifferencingComparer.Compare(0, int.MinValue)); 18 | Assert.Equal(int.MaxValue, new ReverseComparer(DifferencingComparer).Compare(0, int.MinValue)); 19 | } 20 | 21 | [Fact] 22 | public void TestDefaultComparer() 23 | { 24 | Assert.True(Comparer.Default.Compare(0, 1) < 0); 25 | Assert.True(ReverseComparer.Default.Compare(0, 1) > 0); 26 | 27 | Assert.True(Comparer.Default.Compare(1, 0) > 0); 28 | Assert.True(ReverseComparer.Default.Compare(1, 0) < 0); 29 | 30 | Assert.True(Comparer.Default.Compare(0, 0) == 0); 31 | Assert.True(ReverseComparer.Default.Compare(0, 0) == 0); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListCtor1 14 | { 15 | [Fact(DisplayName = "PosTest1: The genaric type is a value type")] 16 | public void PosTest1() 17 | { 18 | TreeList listObject = new TreeList(); 19 | Assert.NotNull(listObject); 20 | } 21 | 22 | [Fact(DisplayName = "PosTest2: The generic type is a reference type")] 23 | public void PosTest2() 24 | { 25 | TreeList listObject = new TreeList(); 26 | Assert.NotNull(listObject); 27 | } 28 | 29 | [Fact(DisplayName = "PosTest3: The generic type is a custom type")] 30 | public void PosTest3() 31 | { 32 | TreeList listObject = new TreeList(); 33 | Assert.NotNull(listObject); 34 | } 35 | 36 | public class MyClass 37 | { 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionIsSynchronized.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for the implementation of , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListICollectionIsSynchronized 15 | { 16 | [Fact(DisplayName = "PosTest1: In the default implementation of List, this property always returns false.")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; 20 | TreeList listObject = new TreeList(iArray); 21 | bool actualValue = ((ICollection)listObject).IsSynchronized; 22 | Assert.False(actualValue); 23 | 24 | string[] sArray = { "1", "9", "3", "6", "5", "8", "7", "2", "4", "0" }; 25 | TreeList listObject1 = new TreeList(sArray); 26 | actualValue = ((ICollection)listObject).IsSynchronized; 27 | Assert.False(actualValue); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeStack.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | 9 | public static class ImmutableTreeStack 10 | { 11 | public static ImmutableTreeStack Create() 12 | => ImmutableTreeStack.Empty; 13 | 14 | public static ImmutableTreeStack Create(T item) 15 | => ImmutableTreeStack.Empty.Push(item); 16 | 17 | public static ImmutableTreeStack Create(params T[] items) 18 | { 19 | if (items is null) 20 | throw new ArgumentNullException(nameof(items)); 21 | 22 | ImmutableTreeStack result = ImmutableTreeStack.Empty; 23 | foreach (T item in items) 24 | result = result.Push(item); 25 | 26 | return result; 27 | } 28 | 29 | public static ImmutableTreeStack CreateRange(IEnumerable items) 30 | { 31 | if (items is null) 32 | throw new ArgumentNullException(nameof(items)); 33 | 34 | ImmutableTreeStack result = ImmutableTreeStack.Empty; 35 | foreach (T item in items) 36 | result = result.Push(item); 37 | 38 | return result; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeQueue.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | 9 | public static class ImmutableTreeQueue 10 | { 11 | public static ImmutableTreeQueue Create() 12 | => ImmutableTreeQueue.Empty; 13 | 14 | public static ImmutableTreeQueue Create(T item) 15 | => ImmutableTreeQueue.Empty.Enqueue(item); 16 | 17 | public static ImmutableTreeQueue Create(params T[] items) 18 | { 19 | if (items is null) 20 | throw new ArgumentNullException(nameof(items)); 21 | 22 | ImmutableTreeQueue result = ImmutableTreeQueue.Empty; 23 | foreach (T item in items) 24 | result = result.Enqueue(item); 25 | 26 | return result; 27 | } 28 | 29 | public static ImmutableTreeQueue CreateRange(IEnumerable items) 30 | { 31 | if (items is null) 32 | throw new ArgumentNullException(nameof(items)); 33 | 34 | ImmutableTreeQueue result = ImmutableTreeQueue.Empty; 35 | foreach (T item in items) 36 | result = result.Enqueue(item); 37 | 38 | return result; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/EverythingThrowsEnumerableTest.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | using IEnumerable = System.Collections.IEnumerable; 10 | 11 | public class EverythingThrowsEnumerableTest 12 | { 13 | [Fact] 14 | public void TestEverythingThrows() 15 | { 16 | TestEverythingThrowsImpl(); 17 | TestEverythingThrowsImpl(); 18 | TestEverythingThrowsImpl(); 19 | TestEverythingThrowsImpl(); 20 | } 21 | 22 | private void TestEverythingThrowsImpl() 23 | { 24 | Assert.NotNull(EverythingThrowsEnumerable.Instance); 25 | Assert.Same(EverythingThrowsEnumerable.Instance, EverythingThrowsEnumerable.Instance); 26 | Assert.Throws(() => EverythingThrowsEnumerable.Instance.GetEnumerator()); 27 | 28 | IEnumerable enumerableT = EverythingThrowsEnumerable.Instance; 29 | Assert.Throws(() => enumerableT.GetEnumerator()); 30 | 31 | IEnumerable enumerable = EverythingThrowsEnumerable.Instance; 32 | Assert.Throws(() => enumerable.GetEnumerator()); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Benchmarks/Immutable/ImmutableTreeListBenchmark.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Benchmarks.Immutable 5 | { 6 | using System.Collections.Immutable; 7 | using System.Linq; 8 | using BenchmarkDotNet.Attributes; 9 | using TunnelVisionLabs.Collections.Trees.Immutable; 10 | 11 | public class ImmutableTreeListBenchmark 12 | { 13 | [ShortRunJob] 14 | public class RangeToList 15 | { 16 | [Params(10, 1000, 100000, 10000000)] 17 | public int Count 18 | { 19 | get; 20 | set; 21 | } 22 | 23 | [Benchmark(Baseline = true, Description = "ImmutableList")] 24 | public ImmutableList List() 25 | { 26 | return ImmutableList.CreateRange(Enumerable.Range(0, Count)); 27 | } 28 | 29 | [Benchmark(Description = "ImmutableArray")] 30 | public ImmutableArray Array() 31 | { 32 | return ImmutableArray.CreateRange(Enumerable.Range(0, Count)); 33 | } 34 | 35 | [Benchmark(Description = "ImmutableTreeList")] 36 | public ImmutableTreeList TreeList() 37 | { 38 | return ImmutableTreeList.CreateRange(Enumerable.Range(0, Count)); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListCount.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListCount 14 | { 15 | [Fact(DisplayName = "PosTest1: Calling count property of List,T is Value type.")] 16 | public void PosTest1() 17 | { 18 | TreeList myList = new TreeList(); 19 | int count = 10; 20 | 21 | int element = 0; 22 | for (int i = 1; i <= count; i++) 23 | { 24 | element = i * count; 25 | myList.Add(element); 26 | } 27 | 28 | Assert.Equal(count, myList.Count); 29 | } 30 | 31 | [Fact(DisplayName = "PosTest2: Calling count property of List,T is reference type.")] 32 | public void PosTest2() 33 | { 34 | TreeList myList = new TreeList(); 35 | int count = 10; 36 | string element = string.Empty; 37 | for (int i = 1; i <= count; i++) 38 | { 39 | element = i.ToString(); 40 | myList.Add(element); 41 | } 42 | 43 | Assert.Equal(count, myList.Count); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/GeneratorTest.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System.Diagnostics; 7 | using System.Reflection; 8 | using Xunit; 9 | 10 | public class GeneratorTest 11 | { 12 | [Fact] 13 | public void TestSeed() 14 | { 15 | ResetSeed(null); 16 | Assert.Null(Generator.Seed); 17 | Generator.Seed = null; 18 | Assert.Null(Generator.Seed); 19 | 20 | Generator.Seed = 50; 21 | Assert.Equal(50, Generator.Seed); 22 | Generator.Seed = 64; 23 | Assert.Equal(50, Generator.Seed); 24 | 25 | void ResetSeed(int? seed) 26 | { 27 | FieldInfo? seedField = typeof(Generator).GetField("_seed", BindingFlags.Static | BindingFlags.NonPublic); 28 | Debug.Assert(seedField is object, $"Assertion failed: {nameof(seedField)} is object"); 29 | seedField.SetValue(null, seed); 30 | } 31 | } 32 | 33 | [Fact] 34 | public void TestGetInt32SingleElement() 35 | { 36 | Assert.Equal(13, Generator.GetInt32(13, 13)); 37 | Assert.Equal(20, Generator.GetInt32(20, 20)); 38 | } 39 | 40 | [Fact] 41 | public void TestGetInt32NegativeRange() 42 | { 43 | Assert.Equal(13, Generator.GetInt32(13, 10)); 44 | Assert.Equal(-2, Generator.GetInt32(-2, -5)); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListICollectionIsReadOnly.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for the implementation of , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListICollectionIsReadOnly 14 | { 15 | [Fact(DisplayName = "PosTest1: In the default implementation of List, this property IsReadOnly always returns false.")] 16 | public void PosTest1() 17 | { 18 | TreeList myList = new TreeList(); 19 | bool expectValue = false; 20 | bool returnValue = ((ICollection)myList).IsReadOnly; 21 | Assert.Equal(expectValue, returnValue); 22 | } 23 | 24 | [Fact(DisplayName = "PosTest2: In the user define implementation of List, this property IsReadOnly may return true.")] 25 | public void PosTest2() 26 | { 27 | MyTestListICollection myList = new MyTestListICollection(); 28 | bool expectValue = true; 29 | bool returnValue = ((ICollection)myList).IsReadOnly; 30 | Assert.Equal(expectValue, returnValue); 31 | } 32 | 33 | public class MyTestListICollection : TreeList, ICollection 34 | { 35 | bool ICollection.IsReadOnly => true; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListContains.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for the implementation of , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListIListContains 15 | { 16 | [Fact(DisplayName = "PosTest1: Calling Contains method of IList,T is Value type.")] 17 | public void PosTest1() 18 | { 19 | TreeList myList = new TreeList(); 20 | int count = 10; 21 | int[] expectValue = new int[10]; 22 | IList myIList = myList; 23 | object? element = null; 24 | for (int i = 1; i <= count; i++) 25 | { 26 | element = i * count; 27 | myIList.Add(element); 28 | expectValue[i - 1] = (int)element; 29 | } 30 | 31 | Assert.True(myIList.Contains(element)); 32 | } 33 | 34 | [Fact(DisplayName = "PosTest2: Calling Contains method of IList,T is reference type.")] 35 | public void PosTest2() 36 | { 37 | TreeList myList = new TreeList(); 38 | int count = 10; 39 | string?[] expectValue = new string?[10]; 40 | object? element = null; 41 | IList myIList = myList; 42 | for (int i = 1; i <= count; i++) 43 | { 44 | element = i.ToString(); 45 | myIList.Add(element); 46 | expectValue[i - 1] = element.ToString(); 47 | } 48 | 49 | Assert.True(myIList.Contains(element)); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListToArray.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListToArray 14 | { 15 | [Fact(DisplayName = "PosTest1: Calling ToArray method of List,T is Value type.")] 16 | public void PosTest1() 17 | { 18 | TreeList myList = new TreeList(); 19 | int count = 10; 20 | 21 | int element = 0; 22 | for (int i = 1; i <= count; i++) 23 | { 24 | element = i * count; 25 | myList.Add(element); 26 | } 27 | 28 | int[] actualArray = myList.ToArray(); 29 | for (int j = 0; j < myList.Count; j++) 30 | { 31 | int current = myList[j]; 32 | Assert.Equal(actualArray[j], current); 33 | } 34 | } 35 | 36 | [Fact(DisplayName = "PosTest2: Calling ToArray method of List,T is reference type.")] 37 | public void PosTest2() 38 | { 39 | TreeList myList = new TreeList(); 40 | int count = 10; 41 | string element = string.Empty; 42 | for (int i = 1; i <= count; i++) 43 | { 44 | element = i.ToString(); 45 | myList.Add(element); 46 | } 47 | 48 | string[] actualArray = myList.ToArray(); 49 | for (int j = 0; j < myList.Count; j++) 50 | { 51 | string current = myList[j]; 52 | Assert.Equal(actualArray[j], current); 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListRemove.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for the implementation of , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListIListRemove 15 | { 16 | [Fact(DisplayName = "PosTest1: Calling Remove method of IList,T is Value type.")] 17 | public void PosTest1() 18 | { 19 | TreeList myList = new TreeList(); 20 | int count = 10; 21 | IList myIList = myList; 22 | object? element = null; 23 | for (int i = 1; i <= count; i++) 24 | { 25 | element = i * count; 26 | myIList.Add(element); 27 | } 28 | 29 | for (int j = 0; j < count; j++) 30 | { 31 | myIList.Remove(myIList[0]); 32 | } 33 | 34 | Assert.Empty(myList); 35 | } 36 | 37 | [Fact(DisplayName = "PosTest2: Calling Remove method of IList,T is reference type.")] 38 | public void PosTest2() 39 | { 40 | TreeList myList = new TreeList(); 41 | int count = 10; 42 | object? element = null; 43 | IList myIList = myList; 44 | for (int i = 1; i <= count; i++) 45 | { 46 | element = i.ToString(); 47 | myIList.Add(element); 48 | } 49 | 50 | for (int j = 0; j < count; j++) 51 | { 52 | myIList.Remove(myIList[0]); 53 | } 54 | 55 | Assert.Empty(myIList); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | using System; 5 | using System.Reflection; 6 | using System.Runtime.CompilerServices; 7 | 8 | // General Information about an assembly is controlled through the following 9 | // set of attributes. Change these attribute values to modify the information 10 | // associated with an assembly. 11 | [assembly: AssemblyTrademark("")] 12 | [assembly: AssemblyCulture("")] 13 | [assembly: CLSCompliant(true)] 14 | 15 | #if DEVELOPMENT_KEY 16 | // PublicKey=00240000048000009400000006020000002400005253413100040000010001000dcce2c53712ba9ac823fa18bc7b75737a0379ebc555777b8319dbeb2958fe688873264053fd22390a44429f3f529ed619ce51dca135095e6145feae02a0661edb966f9d7cddc9270d65478f0c7212ad5da8fc88b52fd18c09bd9dec1385609ac436934aa0ef0a4319f327c26dabf90a43215ca58f70e19671fcb50d89a6dcad 17 | #else 18 | // PublicKey=0024000004800000940000000602000000240000525341310004000001000100b780b4c914551cf629c72926ea5ef857f0fab39b54a0972ce5bae79df0279d9ed2cead8e4022fcf972054aeb1a65ee4724561e6c4ace322848a9244bc7b014bb6de77c7f646e0163035a2debd372ccde583c5329e4ba01b0e2dba4ee11977dbadbb9d17a53ce924c13da4b9d3622aed03f4b329878fc1f7e9647f46e27b886ce 19 | #endif 20 | 21 | [assembly: InternalsVisibleTo("TunnelVisionLabs.Collections.Trees.Experimental, PublicKey=002400000480000094000000060200000024000052534131000400000100010063bb30979a5f932c6ecc131ea6e9cf2e2a50a8476dd94a803452df56f75882df32a98244f6b162da6873515f5cd84217bb773aa18053fb4b2c4a74b60eb8c4d7dce9fa916819c8a85f4df559d9a10d9d89c1db47e5f18730b2047b636220156b9c3761acd9588f566b14d84ba110be8c4aa52868c0204e2ee2b51430a9611cd0")] 22 | [assembly: InternalsVisibleTo("TunnelVisionLabs.Collections.Trees.Test, PublicKey=002400000480000094000000060200000024000052534131000400000100010063bb30979a5f932c6ecc131ea6e9cf2e2a50a8476dd94a803452df56f75882df32a98244f6b162da6873515f5cd84217bb773aa18053fb4b2c4a74b60eb8c4d7dce9fa916819c8a85f4df559d9a10d9d89c1db47e5f18730b2047b636220156b9c3761acd9588f566b14d84ba110be8c4aa52868c0204e2ee2b51430a9611cd0")] 23 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListClear.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListClear 14 | { 15 | [Fact(DisplayName = "PosTest1: Remove int elements from the list")] 16 | public void PosTest1() 17 | { 18 | int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; 19 | TreeList listObject = new TreeList(iArray); 20 | listObject.Clear(); 21 | Assert.Empty(listObject); 22 | } 23 | 24 | [Fact(DisplayName = "PosTest2: Remove string elements from the list")] 25 | public void PosTest2() 26 | { 27 | string[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; 28 | TreeList listObject = new TreeList(strArray); 29 | listObject.Clear(); 30 | Assert.Empty(listObject); 31 | } 32 | 33 | [Fact(DisplayName = "PosTest3: Remove the elements from the list of custom type")] 34 | public void PosTest3() 35 | { 36 | MyClass myclass1 = new MyClass(); 37 | MyClass myclass2 = new MyClass(); 38 | MyClass myclass3 = new MyClass(); 39 | MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; 40 | TreeList listObject = new TreeList(mc); 41 | listObject.Clear(); 42 | Assert.Empty(listObject); 43 | } 44 | 45 | [Fact(DisplayName = "PosTest4: Remove the elements from the empty list")] 46 | public void PosTest4() 47 | { 48 | TreeList listObject = new TreeList(); 49 | listObject.Clear(); 50 | Assert.Empty(listObject); 51 | } 52 | 53 | public class MyClass 54 | { 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListAdd.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListAdd 14 | { 15 | [Fact(DisplayName = "PosTest1: The item to be added is type of byte")] 16 | public void PosTest1() 17 | { 18 | byte[] byteObject = new byte[1000]; 19 | Generator.GetBytes(-55, byteObject); 20 | TreeList listObject = new TreeList(); 21 | for (int i = 0; i < 1000; i++) 22 | { 23 | listObject.Add(byteObject[i]); 24 | } 25 | 26 | for (int i = 0; i < 1000; i++) 27 | { 28 | Assert.Equal(byteObject[i], listObject[i]); 29 | } 30 | } 31 | 32 | [Fact(DisplayName = "PosTest2: The item to be added is type of string")] 33 | public void PosTest2() 34 | { 35 | string[] strArray = { "Hello" }; 36 | TreeList listObject = new TreeList(strArray); 37 | string str1 = "World"; 38 | listObject.Add(str1); 39 | Assert.Equal(2, listObject.Count); 40 | Assert.Equal("World", listObject[1]); 41 | } 42 | 43 | [Fact(DisplayName = "PosTest3: The item to be added is a custom type")] 44 | public void PosTest3() 45 | { 46 | MyClass myClass = new MyClass(); 47 | TreeList listObject = new TreeList(); 48 | listObject.Add(myClass); 49 | Assert.Equal(myClass, listObject[0]); 50 | } 51 | 52 | [Fact(DisplayName = "PosTest4: Add null object to the list")] 53 | public void PosTest4() 54 | { 55 | TreeList listObject = new TreeList(); 56 | listObject.Add(null); 57 | Assert.Null(listObject[0]); 58 | } 59 | 60 | public class MyClass 61 | { 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableSortedTreeList.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections.Generic; 7 | 8 | internal static class ImmutableSortedTreeList 9 | { 10 | public static ImmutableSortedTreeList Create() 11 | => ImmutableSortedTreeList.Empty; 12 | 13 | public static ImmutableSortedTreeList Create(T item) 14 | => ImmutableSortedTreeList.Empty.Add(item); 15 | 16 | public static ImmutableSortedTreeList Create(params T[] items) 17 | => ImmutableSortedTreeList.Empty.AddRange(items); 18 | 19 | public static ImmutableSortedTreeList Create(IComparer? comparer) 20 | => ImmutableSortedTreeList.Empty.WithComparer(comparer); 21 | 22 | public static ImmutableSortedTreeList Create(IComparer? comparer, T item) 23 | => ImmutableSortedTreeList.Empty.WithComparer(comparer).Add(item); 24 | 25 | public static ImmutableSortedTreeList Create(IComparer? comparer, params T[] items) 26 | => ImmutableSortedTreeList.Empty.WithComparer(comparer).AddRange(items); 27 | 28 | public static ImmutableSortedTreeList.Builder CreateBuilder() 29 | => Create().ToBuilder(); 30 | 31 | public static ImmutableSortedTreeList.Builder CreateBuilder(IComparer? comparer) 32 | => Create(comparer).ToBuilder(); 33 | 34 | public static ImmutableSortedTreeList CreateRange(IEnumerable items) 35 | => ImmutableSortedTreeList.Empty.AddRange(items); 36 | 37 | public static ImmutableSortedTreeList CreateRange(IComparer? comparer, IEnumerable items) 38 | => ImmutableSortedTreeList.Empty.WithComparer(comparer).AddRange(items); 39 | 40 | public static ImmutableSortedTreeList ToImmutableSortedTreeList(this IEnumerable source) 41 | => ToImmutableSortedTreeList(source, comparer: null); 42 | 43 | public static ImmutableSortedTreeList ToImmutableSortedTreeList(this IEnumerable source, IComparer? comparer) 44 | => ImmutableSortedTreeList.Empty.WithComparer(comparer).AddRange(source); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeStack`1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | using System.Collections.Immutable; 10 | 11 | public sealed partial class ImmutableTreeStack : IImmutableStack 12 | { 13 | public static readonly ImmutableTreeStack Empty = new ImmutableTreeStack(ImmutableTreeList.Empty); 14 | 15 | private readonly ImmutableTreeList _treeList; 16 | 17 | private ImmutableTreeStack(ImmutableTreeList treeList) 18 | { 19 | _treeList = treeList; 20 | } 21 | 22 | public bool IsEmpty 23 | => _treeList.IsEmpty; 24 | 25 | public ImmutableTreeStack Clear() 26 | => Empty; 27 | 28 | public Enumerator GetEnumerator() 29 | => new Enumerator(_treeList.GetEnumerator()); 30 | 31 | public T Peek() 32 | { 33 | if (IsEmpty) 34 | throw new InvalidOperationException(); 35 | 36 | return _treeList[0]; 37 | } 38 | 39 | public ImmutableTreeStack Pop() 40 | => Pop(out _); 41 | 42 | public ImmutableTreeStack Pop(out T value) 43 | { 44 | if (IsEmpty) 45 | throw new InvalidOperationException(); 46 | 47 | value = _treeList[0]; 48 | return new ImmutableTreeStack(_treeList.RemoveAt(0)); 49 | } 50 | 51 | public ImmutableTreeStack Push(T value) 52 | => new ImmutableTreeStack(_treeList.Insert(0, value)); 53 | 54 | IImmutableStack IImmutableStack.Clear() 55 | => Clear(); 56 | 57 | IImmutableStack IImmutableStack.Pop() 58 | => Pop(); 59 | 60 | IImmutableStack IImmutableStack.Push(T value) 61 | => Push(value); 62 | 63 | IEnumerator IEnumerable.GetEnumerator() 64 | => GetEnumerator(); 65 | 66 | IEnumerator IEnumerable.GetEnumerator() 67 | => GetEnumerator(); 68 | 69 | internal void Validate(ValidationRules validationRules) 70 | { 71 | _treeList.Validate(validationRules); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeQueue`1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | using System.Collections.Immutable; 10 | 11 | public sealed partial class ImmutableTreeQueue : IImmutableQueue 12 | { 13 | public static readonly ImmutableTreeQueue Empty = new ImmutableTreeQueue(ImmutableTreeList.Empty); 14 | 15 | private readonly ImmutableTreeList _treeList; 16 | 17 | private ImmutableTreeQueue(ImmutableTreeList treeList) 18 | { 19 | _treeList = treeList; 20 | } 21 | 22 | public bool IsEmpty 23 | => _treeList.IsEmpty; 24 | 25 | public ImmutableTreeQueue Clear() 26 | => Empty; 27 | 28 | public ImmutableTreeQueue Dequeue() 29 | => Dequeue(out _); 30 | 31 | public ImmutableTreeQueue Dequeue(out T value) 32 | { 33 | if (IsEmpty) 34 | throw new InvalidOperationException(); 35 | 36 | value = _treeList[0]; 37 | return new ImmutableTreeQueue(_treeList.RemoveAt(0)); 38 | } 39 | 40 | public ImmutableTreeQueue Enqueue(T value) 41 | => new ImmutableTreeQueue(_treeList.Add(value)); 42 | 43 | public Enumerator GetEnumerator() 44 | => new Enumerator(_treeList.GetEnumerator()); 45 | 46 | public T Peek() 47 | { 48 | if (IsEmpty) 49 | throw new InvalidOperationException(); 50 | 51 | return _treeList[0]; 52 | } 53 | 54 | IImmutableQueue IImmutableQueue.Clear() 55 | => Clear(); 56 | 57 | IImmutableQueue IImmutableQueue.Dequeue() 58 | => Dequeue(); 59 | 60 | IImmutableQueue IImmutableQueue.Enqueue(T value) 61 | => Enqueue(value); 62 | 63 | IEnumerator IEnumerable.GetEnumerator() 64 | => GetEnumerator(); 65 | 66 | IEnumerator IEnumerable.GetEnumerator() 67 | => GetEnumerator(); 68 | 69 | internal void Validate(ValidationRules validationRules) 70 | { 71 | _treeList.Validate(validationRules); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIEnumerableGetEnumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for the implementation of , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListIEnumerableGetEnumerator 14 | { 15 | [Fact(DisplayName = "PosTest1: Calling GetEnumerator method of IEnumerable,T is Value type.")] 16 | public void PosTest1() 17 | { 18 | TreeList myList = new TreeList(); 19 | int count = 10; 20 | int[] expectValue = new int[10]; 21 | 22 | for (int i = 1; i <= count; i++) 23 | { 24 | myList.Add(i * count); 25 | expectValue[i - 1] = i * count; 26 | } 27 | 28 | IEnumerator returnValue = ((IEnumerable)myList).GetEnumerator(); 29 | int j = 0; 30 | for (IEnumerator itr = returnValue; itr.MoveNext();) 31 | { 32 | int current = itr.Current; 33 | Assert.Equal(expectValue[j], current); 34 | 35 | j++; 36 | } 37 | } 38 | 39 | [Fact(DisplayName = "PosTest2: Calling GetEnumerator method of IEnumerable,T is reference type.")] 40 | public void PosTest2() 41 | { 42 | TreeList myList = new TreeList(); 43 | int count = 10; 44 | string[] expectValue = new string[10]; 45 | string element = string.Empty; 46 | for (int i = 1; i <= count; i++) 47 | { 48 | element = i.ToString(); 49 | myList.Add(element); 50 | expectValue[i - 1] = element; 51 | } 52 | 53 | IEnumerator returnValue = ((IEnumerable)myList).GetEnumerator(); 54 | int j = 0; 55 | for (IEnumerator itr = returnValue; itr.MoveNext();) 56 | { 57 | string current = itr.Current; 58 | Assert.Equal(expectValue[j], current); 59 | 60 | j++; 61 | } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIEnumerableGetEnumerator2.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for the implementation of , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListIEnumerableGetEnumerator2 15 | { 16 | [Fact(DisplayName = "PosTest1: Calling GetEnumerator method of IEnumerable,T is Value type.")] 17 | public void PosTest1() 18 | { 19 | TreeList myList = new TreeList(); 20 | int count = 10; 21 | int[] expectValue = new int[10]; 22 | 23 | for (int i = 1; i <= count; i++) 24 | { 25 | myList.Add(i * count); 26 | expectValue[i - 1] = i * count; 27 | } 28 | 29 | IEnumerator returnValue = ((IEnumerable)myList).GetEnumerator(); 30 | int j = 0; 31 | for (IEnumerator itr = returnValue; itr.MoveNext();) 32 | { 33 | int current = (int)itr.Current!; 34 | Assert.Equal(expectValue[j], current); 35 | 36 | j++; 37 | } 38 | } 39 | 40 | [Fact(DisplayName = "PosTest2: Calling GetEnumerator method of IEnumerable,T is reference type.")] 41 | public void PosTest2() 42 | { 43 | TreeList myList = new TreeList(); 44 | int count = 10; 45 | string[] expectValue = new string[10]; 46 | string element = string.Empty; 47 | for (int i = 1; i <= count; i++) 48 | { 49 | element = i.ToString(); 50 | myList.Add(element); 51 | expectValue[i - 1] = element; 52 | } 53 | 54 | IEnumerator returnValue = ((IEnumerable)myList).GetEnumerator(); 55 | int j = 0; 56 | for (IEnumerator itr = returnValue; itr.MoveNext();) 57 | { 58 | string? current = (string?)itr.Current; 59 | Assert.Equal(expectValue[j], current); 60 | 61 | j++; 62 | } 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeSet.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | 9 | public static class ImmutableTreeSet 10 | { 11 | public static ImmutableTreeSet Create() 12 | => ImmutableTreeSet.Empty; 13 | 14 | public static ImmutableTreeSet Create(T item) 15 | => ImmutableTreeSet.Empty.Add(item); 16 | 17 | public static ImmutableTreeSet Create(params T[] items) 18 | => ImmutableTreeSet.Empty.Union(items); 19 | 20 | public static ImmutableTreeSet Create(IEqualityComparer? equalityComparer) 21 | => ImmutableTreeSet.Empty.WithComparer(equalityComparer); 22 | 23 | public static ImmutableTreeSet Create(IEqualityComparer? equalityComparer, T item) 24 | => ImmutableTreeSet.Empty.WithComparer(equalityComparer).Add(item); 25 | 26 | public static ImmutableTreeSet Create(IEqualityComparer? equalityComparer, params T[] items) 27 | => ImmutableTreeSet.Empty.WithComparer(equalityComparer).Union(items); 28 | 29 | public static ImmutableTreeSet.Builder CreateBuilder() 30 | => Create().ToBuilder(); 31 | 32 | public static ImmutableTreeSet.Builder CreateBuilder(IEqualityComparer? equalityComparer) 33 | => Create(equalityComparer).ToBuilder(); 34 | 35 | public static ImmutableTreeSet CreateRange(IEnumerable items) 36 | => ImmutableTreeSet.Empty.Union(items); 37 | 38 | public static ImmutableTreeSet CreateRange(IEqualityComparer? equalityComparer, IEnumerable items) 39 | => ImmutableTreeSet.Empty.WithComparer(equalityComparer).Union(items); 40 | 41 | public static ImmutableTreeSet ToImmutableTreeSet(this IEnumerable source) 42 | => ToImmutableTreeSet(source, equalityComparer: null); 43 | 44 | public static ImmutableTreeSet ToImmutableTreeSet(this IEnumerable source, IEqualityComparer? equalityComparer) 45 | { 46 | if (source is null) 47 | throw new ArgumentNullException(nameof(source)); 48 | 49 | return ImmutableTreeSet.Empty.WithComparer(equalityComparer).Union(source); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableSortedTreeSet.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | 9 | public static class ImmutableSortedTreeSet 10 | { 11 | public static ImmutableSortedTreeSet Create() 12 | => ImmutableSortedTreeSet.Empty; 13 | 14 | public static ImmutableSortedTreeSet Create(T item) 15 | => ImmutableSortedTreeSet.Empty.Add(item); 16 | 17 | public static ImmutableSortedTreeSet Create(params T[] items) 18 | => ImmutableSortedTreeSet.Empty.Union(items); 19 | 20 | public static ImmutableSortedTreeSet Create(IComparer? comparer) 21 | => ImmutableSortedTreeSet.Empty.WithComparer(comparer); 22 | 23 | public static ImmutableSortedTreeSet Create(IComparer? comparer, T item) 24 | => ImmutableSortedTreeSet.Empty.WithComparer(comparer).Add(item); 25 | 26 | public static ImmutableSortedTreeSet Create(IComparer? comparer, params T[] items) 27 | => ImmutableSortedTreeSet.Empty.WithComparer(comparer).Union(items); 28 | 29 | public static ImmutableSortedTreeSet.Builder CreateBuilder() 30 | => Create().ToBuilder(); 31 | 32 | public static ImmutableSortedTreeSet.Builder CreateBuilder(IComparer? comparer) 33 | => Create(comparer).ToBuilder(); 34 | 35 | public static ImmutableSortedTreeSet CreateRange(IEnumerable items) 36 | => ImmutableSortedTreeSet.Empty.Union(items); 37 | 38 | public static ImmutableSortedTreeSet CreateRange(IComparer? comparer, IEnumerable items) 39 | => ImmutableSortedTreeSet.Empty.WithComparer(comparer).Union(items); 40 | 41 | public static ImmutableSortedTreeSet ToImmutableSortedTreeSet(this IEnumerable source) 42 | => ToImmutableSortedTreeSet(source, comparer: null); 43 | 44 | public static ImmutableSortedTreeSet ToImmutableSortedTreeSet(this IEnumerable source, IComparer? comparer) 45 | { 46 | if (source is null) 47 | throw new ArgumentNullException(nameof(source)); 48 | 49 | return ImmutableSortedTreeSet.Empty.WithComparer(comparer).Union(source); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListAddRange.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListAddRange 15 | { 16 | [Fact(DisplayName = "PosTest1: The item to be added is type of byte")] 17 | public void PosTest1() 18 | { 19 | byte[] byteObject = new byte[1000]; 20 | Generator.GetBytes(-55, byteObject); 21 | TreeList listObject = new TreeList(); 22 | listObject.AddRange(byteObject); 23 | for (int i = 0; i < 1000; i++) 24 | { 25 | Assert.Equal(byteObject[i], listObject[i]); 26 | } 27 | } 28 | 29 | [Fact(DisplayName = "PosTest2: The item to be added is type of string")] 30 | public void PosTest2() 31 | { 32 | string[] strArray = { "Hello", "world", "Tom", "school" }; 33 | TreeList listObject = new TreeList(); 34 | listObject.AddRange(strArray); 35 | Assert.Equal(4, listObject.Count); 36 | 37 | for (int i = 0; i < 4; i++) 38 | { 39 | Assert.Equal(strArray[i], listObject[i]); 40 | } 41 | } 42 | 43 | [Fact(DisplayName = "PosTest3: The item to be added is a custom type")] 44 | public void PosTest3() 45 | { 46 | MyClass myClass1 = new MyClass(); 47 | MyClass myClass2 = new MyClass(); 48 | MyClass[] mc = { myClass1, myClass2 }; 49 | TreeList listObject = new TreeList(); 50 | listObject.AddRange(mc); 51 | Assert.Equal(myClass1, listObject[0]); 52 | Assert.Equal(myClass2, listObject[1]); 53 | } 54 | 55 | [Fact(DisplayName = "NegTest1: The argument is a null reference")] 56 | public void NegTest1() 57 | { 58 | IEnumerable? i = null; 59 | TreeList listObject = new TreeList(); 60 | Assert.Throws(() => listObject.AddRange(i!)); 61 | } 62 | 63 | public class MyClass 64 | { 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor2.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListCtor2 15 | { 16 | [Fact(DisplayName = "PosTest1: The genaric type is a value type")] 17 | public void PosTest1() 18 | { 19 | int[] intArray = new int[5] { 1, 2, 3, 4, 5 }; 20 | TreeList listObject = new TreeList(intArray); 21 | Assert.NotNull(listObject); 22 | Assert.Equal(5, listObject.Count); 23 | } 24 | 25 | [Fact(DisplayName = "PosTest2: The generic type is a reference type")] 26 | public void PosTest2() 27 | { 28 | string[] stringArray = { "Hello", "world", "thanks", "school" }; 29 | TreeList listObject = new TreeList(stringArray); 30 | Assert.NotNull(listObject); 31 | Assert.Equal(4, listObject.Count); 32 | } 33 | 34 | [Fact(DisplayName = "PosTest3: The generic type is a custom type")] 35 | public void PosTest3() 36 | { 37 | int length = Generator.GetByte(-55); 38 | MyClass[] myClass = new MyClass[length]; 39 | TreeList listObject = new TreeList(myClass); 40 | Assert.NotNull(listObject); 41 | Assert.Equal(length, listObject.Count); 42 | } 43 | 44 | [Fact(DisplayName = "PosTest4: Using a list to construct another list")] 45 | public void PosTest4() 46 | { 47 | int[] iArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; 48 | TreeList listObject1 = new TreeList(iArray); 49 | TreeList listObject2 = new TreeList(listObject1); 50 | Assert.NotNull(listObject2); 51 | Assert.Equal(10, listObject2.Count); 52 | } 53 | 54 | [Fact(DisplayName = "NegTest1: The argument is a null reference")] 55 | public void NegTest1() 56 | { 57 | IEnumerable? i = null; 58 | Assert.Throws(() => new TreeList(i!)); 59 | } 60 | 61 | public class MyClass 62 | { 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableTreeDictionary`2+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public sealed partial class ImmutableTreeDictionary 10 | { 11 | public struct Enumerator : IEnumerator>, IDictionaryEnumerator 12 | { 13 | private readonly ReturnType _returnType; 14 | private ImmutableTreeSet>.Enumerator _enumerator; 15 | 16 | internal Enumerator(ImmutableTreeSet>.Enumerator enumerator, ReturnType returnType) 17 | { 18 | _returnType = returnType; 19 | _enumerator = enumerator; 20 | } 21 | 22 | internal enum ReturnType 23 | { 24 | /// 25 | /// The return value from the implementation of is 26 | /// . This is the return value for most instances of this 27 | /// enumerator. 28 | /// 29 | KeyValuePair, 30 | 31 | /// 32 | /// The return value from the implementation of is 33 | /// . This is the return value for instances of this enumerator created by 34 | /// the implementation in 35 | /// . 36 | /// 37 | DictionaryEntry, 38 | } 39 | 40 | public KeyValuePair Current => _enumerator.Current; 41 | 42 | object IEnumerator.Current => _returnType == ReturnType.DictionaryEntry ? (object)((IDictionaryEnumerator)this).Entry : Current; 43 | 44 | DictionaryEntry IDictionaryEnumerator.Entry => new DictionaryEntry(Current.Key, Current.Value); 45 | 46 | object IDictionaryEnumerator.Key => Current.Key; 47 | 48 | object? IDictionaryEnumerator.Value => Current.Value; 49 | 50 | public void Dispose() => _enumerator.Dispose(); 51 | 52 | public bool MoveNext() => _enumerator.MoveNext(); 53 | 54 | public void Reset() => _enumerator.Reset(); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/Immutable/ImmutableSortedTreeDictionary`2+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Immutable 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | 9 | public sealed partial class ImmutableSortedTreeDictionary 10 | { 11 | public struct Enumerator : IEnumerator>, IDictionaryEnumerator 12 | { 13 | private readonly ReturnType _returnType; 14 | private ImmutableSortedTreeSet>.Enumerator _enumerator; 15 | 16 | internal Enumerator(ImmutableSortedTreeSet>.Enumerator enumerator, ReturnType returnType) 17 | { 18 | _returnType = returnType; 19 | _enumerator = enumerator; 20 | } 21 | 22 | internal enum ReturnType 23 | { 24 | /// 25 | /// The return value from the implementation of is 26 | /// . This is the return value for most instances of this 27 | /// enumerator. 28 | /// 29 | KeyValuePair, 30 | 31 | /// 32 | /// The return value from the implementation of is 33 | /// . This is the return value for instances of this enumerator created by 34 | /// the implementation in 35 | /// . 36 | /// 37 | DictionaryEntry, 38 | } 39 | 40 | public KeyValuePair Current => _enumerator.Current; 41 | 42 | object IEnumerator.Current => _returnType == ReturnType.DictionaryEntry ? (object)((IDictionaryEnumerator)this).Entry : Current; 43 | 44 | DictionaryEntry IDictionaryEnumerator.Entry => new DictionaryEntry(Current.Key, Current.Value); 45 | 46 | object IDictionaryEnumerator.Key => Current.Key; 47 | 48 | object? IDictionaryEnumerator.Value => Current.Value; 49 | 50 | public void Dispose() => _enumerator.Dispose(); 51 | 52 | public bool MoveNext() => _enumerator.MoveNext(); 53 | 54 | public void Reset() => _enumerator.Reset(); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeDictionary`2+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | using IDictionaryEnumerator = System.Collections.IDictionaryEnumerator; 9 | 10 | public partial class TreeDictionary 11 | { 12 | public struct Enumerator : IEnumerator>, IDictionaryEnumerator 13 | { 14 | private readonly ReturnType _returnType; 15 | private TreeSet>.Enumerator _enumerator; 16 | 17 | internal Enumerator(TreeSet>.Enumerator enumerator, ReturnType returnType) 18 | { 19 | _returnType = returnType; 20 | _enumerator = enumerator; 21 | } 22 | 23 | internal enum ReturnType 24 | { 25 | /// 26 | /// The return value from the implementation of is 27 | /// . This is the return value for most instances of this 28 | /// enumerator. 29 | /// 30 | KeyValuePair, 31 | 32 | /// 33 | /// The return value from the implementation of is 34 | /// . This is the return value for instances of this enumerator created by 35 | /// the implementation in 36 | /// . 37 | /// 38 | DictionaryEntry, 39 | } 40 | 41 | public KeyValuePair Current => _enumerator.Current; 42 | 43 | object IEnumerator.Current => _returnType == ReturnType.DictionaryEntry ? (object)((IDictionaryEnumerator)this).Entry : Current; 44 | 45 | DictionaryEntry IDictionaryEnumerator.Entry => new DictionaryEntry(Current.Key, Current.Value); 46 | 47 | object IDictionaryEnumerator.Key => Current.Key; 48 | 49 | object? IDictionaryEnumerator.Value => Current.Value; 50 | 51 | public void Dispose() => _enumerator.Dispose(); 52 | 53 | public bool MoveNext() => _enumerator.MoveNext(); 54 | 55 | void IEnumerator.Reset() => InternalReset(); 56 | 57 | internal void InternalReset() => _enumerator.InternalReset(); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/SortedTreeDictionary`2+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | using IDictionaryEnumerator = System.Collections.IDictionaryEnumerator; 9 | 10 | public partial class SortedTreeDictionary 11 | { 12 | public struct Enumerator : IEnumerator>, IDictionaryEnumerator 13 | { 14 | private readonly ReturnType _returnType; 15 | private SortedTreeSet>.Enumerator _enumerator; 16 | 17 | internal Enumerator(SortedTreeSet>.Enumerator enumerator, ReturnType returnType) 18 | { 19 | _returnType = returnType; 20 | _enumerator = enumerator; 21 | } 22 | 23 | internal enum ReturnType 24 | { 25 | /// 26 | /// The return value from the implementation of is 27 | /// . This is the return value for most instances of this 28 | /// enumerator. 29 | /// 30 | KeyValuePair, 31 | 32 | /// 33 | /// The return value from the implementation of is 34 | /// . This is the return value for instances of this enumerator created by 35 | /// the implementation in 36 | /// . 37 | /// 38 | DictionaryEntry, 39 | } 40 | 41 | public KeyValuePair Current => _enumerator.Current; 42 | 43 | object IEnumerator.Current => _returnType == ReturnType.DictionaryEntry ? (object)((IDictionaryEnumerator)this).Entry : Current; 44 | 45 | DictionaryEntry IDictionaryEnumerator.Entry => new DictionaryEntry(Current.Key, Current.Value); 46 | 47 | object IDictionaryEnumerator.Key => Current.Key; 48 | 49 | object? IDictionaryEnumerator.Value => Current.Value; 50 | 51 | public void Dispose() => _enumerator.Dispose(); 52 | 53 | public bool MoveNext() => _enumerator.MoveNext(); 54 | 55 | void IEnumerator.Reset() => InternalReset(); 56 | 57 | internal void InternalReset() => _enumerator.InternalReset(); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListContains.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | // This file contains tests for Contains... 5 | #pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection 6 | 7 | namespace TunnelVisionLabs.Collections.Trees.Test.List 8 | { 9 | using System.Collections.Generic; 10 | using Xunit; 11 | 12 | /// 13 | /// Tests for , derived from tests for 14 | /// in dotnet/coreclr. 15 | /// 16 | public class TreeListContains 17 | { 18 | [Fact(DisplayName = "PosTest1: The generic type is int")] 19 | public void PosTest1() 20 | { 21 | int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; 22 | TreeList listObject = new TreeList(iArray); 23 | int i = Generator.GetInt32(0, 10); 24 | Assert.True(listObject.Contains(i)); 25 | } 26 | 27 | [Fact(DisplayName = "PosTest2: The generic type is a referece type of string")] 28 | public void PosTest2() 29 | { 30 | string[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; 31 | TreeList listObject = new TreeList(strArray); 32 | Assert.True(listObject.Contains("dog")); 33 | } 34 | 35 | [Fact(DisplayName = "PosTest3: The generic type is custom type")] 36 | public void PosTest3() 37 | { 38 | MyClass myclass1 = new MyClass(); 39 | MyClass myclass2 = new MyClass(); 40 | MyClass myclass3 = new MyClass(); 41 | MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; 42 | TreeList listObject = new TreeList(mc); 43 | Assert.True(listObject.Contains(myclass1)); 44 | } 45 | 46 | [Fact(DisplayName = "PosTest4: The list does not contain the element")] 47 | public void PosTest4() 48 | { 49 | char[] chArray = { '1', '9', '3', '6', '5', '8', '7', '2', '4' }; 50 | TreeList listObject = new TreeList(chArray); 51 | Assert.False(listObject.Contains('t')); 52 | } 53 | 54 | [Fact(DisplayName = "PosTest5: The argument is a null reference")] 55 | public void PosTest5() 56 | { 57 | string?[] strArray = { "apple", "banana", "chocolate", null, "food" }; 58 | TreeList listObject = new TreeList(strArray); 59 | Assert.True(listObject.Contains(null)); 60 | } 61 | 62 | public class MyClass 63 | { 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListGetEnumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListGetEnumerator 14 | { 15 | [Fact(DisplayName = "PosTest1: The generic type is int")] 16 | public void PosTest1() 17 | { 18 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; 19 | TreeList listObject = new TreeList(iArray); 20 | TreeList.Enumerator enumerator = listObject.GetEnumerator(); 21 | for (int i = 0; i < 10; i++) 22 | { 23 | enumerator.MoveNext(); 24 | Assert.Equal(iArray[i], enumerator.Current); 25 | } 26 | } 27 | 28 | [Fact(DisplayName = "PosTest2: The generic type is type of string")] 29 | public void PosTest2() 30 | { 31 | string[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; 32 | TreeList listObject = new TreeList(strArray); 33 | TreeList.Enumerator enumerator = listObject.GetEnumerator(); 34 | int i = 0; 35 | while (enumerator.MoveNext()) 36 | { 37 | Assert.Equal(strArray[i], enumerator.Current); 38 | i++; 39 | } 40 | } 41 | 42 | [Fact(DisplayName = "PosTest3: The generic type is a custom type")] 43 | public void PosTest3() 44 | { 45 | MyClass myclass1 = new MyClass(); 46 | MyClass myclass2 = new MyClass(); 47 | MyClass myclass3 = new MyClass(); 48 | MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; 49 | TreeList listObject = new TreeList(mc); 50 | TreeList.Enumerator enumerator = listObject.GetEnumerator(); 51 | int i = 0; 52 | while (enumerator.MoveNext()) 53 | { 54 | Assert.Equal(mc[i], enumerator.Current); 55 | i++; 56 | } 57 | } 58 | 59 | [Fact(DisplayName = "PosTest4: The List is empty")] 60 | public void PosTest4() 61 | { 62 | TreeList listObject = new TreeList(); 63 | TreeList.Enumerator enumerator = listObject.GetEnumerator(); 64 | Assert.False(enumerator.MoveNext()); 65 | } 66 | 67 | public class MyClass 68 | { 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListIndexOf1 14 | { 15 | [Fact(DisplayName = "PosTest1: The generic type is int")] 16 | public void PosTest1() 17 | { 18 | int[] iArray = new int[1000]; 19 | for (int i = 0; i < 1000; i++) 20 | { 21 | iArray[i] = i; 22 | } 23 | 24 | TreeList listObject = new TreeList(iArray); 25 | int ob = Generator.GetInt32(0, 1000); 26 | int result = listObject.IndexOf(ob); 27 | Assert.Equal(ob, result); 28 | } 29 | 30 | [Fact(DisplayName = "PosTest2: The generic type is type of string")] 31 | public void PosTest2() 32 | { 33 | string[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; 34 | TreeList listObject = new TreeList(strArray); 35 | int result = listObject.IndexOf("dog"); 36 | Assert.Equal(3, result); 37 | } 38 | 39 | [Fact(DisplayName = "PosTest3: The generic type is a custom type")] 40 | public void PosTest3() 41 | { 42 | MyClass myclass1 = new MyClass(); 43 | MyClass myclass2 = new MyClass(); 44 | MyClass myclass3 = new MyClass(); 45 | MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; 46 | TreeList listObject = new TreeList(mc); 47 | int result = listObject.IndexOf(myclass3); 48 | Assert.Equal(2, result); 49 | } 50 | 51 | [Fact(DisplayName = "PosTest4: There are many element in the list with the same value")] 52 | public void PosTest4() 53 | { 54 | string[] strArray = { "apple", "banana", "chocolate", "banana", "banana", "dog", "banana", "food" }; 55 | TreeList listObject = new TreeList(strArray); 56 | int result = listObject.IndexOf("banana"); 57 | Assert.Equal(1, result); 58 | } 59 | 60 | [Fact(DisplayName = "PosTest5: Do not find the element")] 61 | public void PosTest5() 62 | { 63 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; 64 | TreeList listObject = new TreeList(iArray); 65 | int result = listObject.IndexOf(-10000); 66 | Assert.Equal(-1, result); 67 | } 68 | 69 | public class MyClass 70 | { 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeSpan.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System; 7 | using System.Diagnostics; 8 | 9 | internal struct TreeSpan : IEquatable 10 | { 11 | public static readonly TreeSpan Invalid = new TreeSpan(-1, 0); 12 | 13 | public TreeSpan(int start, int count) 14 | { 15 | Debug.Assert(count >= 0, $"Assertion failed: {nameof(count)} >= 0"); 16 | 17 | Start = start; 18 | Count = count; 19 | } 20 | 21 | public int Start 22 | { 23 | get; 24 | } 25 | 26 | public int Count 27 | { 28 | get; 29 | } 30 | 31 | public int EndExclusive => Start + Count; 32 | 33 | public int EndInclusive => Start + Count - 1; 34 | 35 | public bool IsEmpty => Count == 0; 36 | 37 | public static bool operator ==(TreeSpan span1, TreeSpan span2) => span1.Equals(span2); 38 | 39 | public static bool operator !=(TreeSpan span1, TreeSpan span2) => !span1.Equals(span2); 40 | 41 | public static TreeSpan FromBounds(int start, int endExclusive) 42 | { 43 | Debug.Assert(endExclusive >= start, $"Assertion failed: {nameof(endExclusive)} >= {nameof(start)}"); 44 | 45 | return new TreeSpan(start, endExclusive - start); 46 | } 47 | 48 | public static TreeSpan FromReverseSpan(int start, int count) 49 | { 50 | return new TreeSpan(start - count + 1, count); 51 | } 52 | 53 | public static TreeSpan Intersect(TreeSpan left, TreeSpan right) 54 | { 55 | int start = Math.Max(left.Start, right.Start); 56 | int endExclusive = Math.Min(left.EndExclusive, right.EndExclusive); 57 | if (endExclusive < start) 58 | return Invalid; 59 | 60 | return FromBounds(start, endExclusive); 61 | } 62 | 63 | public TreeSpan Offset(int distance) => new TreeSpan(Start + distance, Count); 64 | 65 | public bool IsSubspanOf(TreeSpan other) => Start >= other.Start && EndExclusive <= other.EndExclusive; 66 | 67 | public bool IsProperSubspanOf(TreeSpan other) => Count < other.Count && IsSubspanOf(other); 68 | 69 | public override bool Equals(object? obj) 70 | { 71 | if (!(obj is TreeSpan other)) 72 | return false; 73 | 74 | return Equals(other); 75 | } 76 | 77 | public override int GetHashCode() => (Start * -1369216789) + Count; 78 | 79 | public bool Equals(TreeSpan other) 80 | { 81 | return Start == other.Start 82 | && Count == other.Count; 83 | } 84 | 85 | public override string ToString() => $"[{Start}, {EndExclusive})"; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListAdd.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | using Xunit; 10 | 11 | /// 12 | /// Tests for the implementation of , derived from tests for 13 | /// in dotnet/coreclr. 14 | /// 15 | public class TreeListIListAdd 16 | { 17 | [Fact(DisplayName = "PosTest1: Calling Add method of IList,T is Value type.")] 18 | public void PosTest1() 19 | { 20 | TreeList myList = new TreeList(); 21 | int count = 10; 22 | int[] expectValue = new int[10]; 23 | IList myIList = myList; 24 | object? element = null; 25 | for (int i = 1; i <= count; i++) 26 | { 27 | element = i * count; 28 | myIList.Add(element); 29 | expectValue[i - 1] = (int)element; 30 | } 31 | 32 | IEnumerator returnValue = myIList.GetEnumerator(); 33 | int j = 0; 34 | for (IEnumerator itr = returnValue; itr.MoveNext();) 35 | { 36 | int current = (int)itr.Current!; 37 | Assert.Equal(expectValue[j], current); 38 | 39 | j++; 40 | } 41 | } 42 | 43 | [Fact(DisplayName = "PosTest2: Calling Add method of IList,T is reference type.")] 44 | public void PosTest2() 45 | { 46 | TreeList myList = new TreeList(); 47 | int count = 10; 48 | string?[] expectValue = new string?[10]; 49 | object? element = null; 50 | IList myIList = myList; 51 | for (int i = 1; i <= count; i++) 52 | { 53 | element = i.ToString(); 54 | myIList.Add(element); 55 | expectValue[i - 1] = element.ToString(); 56 | } 57 | 58 | IEnumerator returnValue = myIList.GetEnumerator(); 59 | int j = 0; 60 | for (IEnumerator itr = returnValue; itr.MoveNext();) 61 | { 62 | string? current = (string?)itr.Current; 63 | Assert.Equal(expectValue[j], current); 64 | 65 | j++; 66 | } 67 | } 68 | 69 | [Fact(DisplayName = "NegTest1: item is of a type that is not assignable to the IList.")] 70 | public void NegTest1() 71 | { 72 | TreeList myList = new TreeList(); 73 | IList myIList = myList; 74 | 75 | // int type should be add. but add null ArgumentNullException should be caught. 76 | Assert.Throws(() => myIList.Add(null)); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeStack`1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | using System.Diagnostics.CodeAnalysis; 10 | using ICollection = System.Collections.ICollection; 11 | 12 | public partial class TreeStack : IReadOnlyCollection, ICollection 13 | { 14 | private readonly TreeList _treeList; 15 | 16 | public TreeStack() 17 | { 18 | _treeList = new TreeList(); 19 | } 20 | 21 | public TreeStack(int branchingFactor) 22 | { 23 | _treeList = new TreeList(branchingFactor); 24 | } 25 | 26 | public int Count => _treeList.Count; 27 | 28 | bool ICollection.IsSynchronized => false; 29 | 30 | object ICollection.SyncRoot => this; 31 | 32 | public void Clear() => _treeList.Clear(); 33 | 34 | public bool Contains(T item) => _treeList.Contains(item); 35 | 36 | public void CopyTo(T[] array, int arrayIndex) => _treeList.CopyTo(array, arrayIndex); 37 | 38 | public Enumerator GetEnumerator() => new Enumerator(_treeList.GetEnumerator()); 39 | 40 | public T Peek() 41 | { 42 | if (!TryPeek(out T result)) 43 | throw new InvalidOperationException(); 44 | 45 | return result; 46 | } 47 | 48 | public T Pop() 49 | { 50 | if (!TryPop(out T result)) 51 | throw new InvalidOperationException(); 52 | 53 | return result; 54 | } 55 | 56 | public void Push(T item) => _treeList.Insert(0, item); 57 | 58 | public T[] ToArray() => _treeList.ToArray(); 59 | 60 | public void TrimExcess() => _treeList.TrimExcess(); 61 | 62 | public bool TryPeek([MaybeNullWhen(false)] out T result) 63 | { 64 | if (_treeList.Count == 0) 65 | { 66 | result = default; 67 | return false; 68 | } 69 | 70 | result = _treeList[0]; 71 | return true; 72 | } 73 | 74 | public bool TryPop([MaybeNullWhen(false)] out T result) 75 | { 76 | if (_treeList.Count == 0) 77 | { 78 | result = default; 79 | return false; 80 | } 81 | 82 | result = _treeList[0]; 83 | _treeList.RemoveAt(0); 84 | return true; 85 | } 86 | 87 | void ICollection.CopyTo(Array array, int index) => ((ICollection)_treeList).CopyTo(array, index); 88 | 89 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 90 | 91 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 92 | 93 | internal void Validate(ValidationRules validationRules) 94 | { 95 | _treeList.Validate(validationRules); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeQueue`1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | using System.Diagnostics.CodeAnalysis; 10 | using ICollection = System.Collections.ICollection; 11 | 12 | public partial class TreeQueue : IReadOnlyCollection, ICollection 13 | { 14 | private readonly TreeList _treeList; 15 | 16 | public TreeQueue() 17 | { 18 | _treeList = new TreeList(); 19 | } 20 | 21 | public TreeQueue(int branchingFactor) 22 | { 23 | _treeList = new TreeList(branchingFactor); 24 | } 25 | 26 | public int Count => _treeList.Count; 27 | 28 | bool ICollection.IsSynchronized => false; 29 | 30 | object ICollection.SyncRoot => this; 31 | 32 | public void Clear() => _treeList.Clear(); 33 | 34 | public bool Contains(T item) => _treeList.Contains(item); 35 | 36 | public void CopyTo(T[] array, int arrayIndex) => _treeList.CopyTo(array, arrayIndex); 37 | 38 | public Enumerator GetEnumerator() => new Enumerator(_treeList.GetEnumerator()); 39 | 40 | public T Peek() 41 | { 42 | if (!TryPeek(out T result)) 43 | throw new InvalidOperationException(); 44 | 45 | return result; 46 | } 47 | 48 | public T Dequeue() 49 | { 50 | if (!TryDequeue(out T result)) 51 | throw new InvalidOperationException(); 52 | 53 | return result; 54 | } 55 | 56 | public void Enqueue(T item) => _treeList.Add(item); 57 | 58 | public T[] ToArray() => _treeList.ToArray(); 59 | 60 | public void TrimExcess() => _treeList.TrimExcess(); 61 | 62 | public bool TryPeek([MaybeNullWhen(false)] out T result) 63 | { 64 | if (_treeList.Count == 0) 65 | { 66 | result = default; 67 | return false; 68 | } 69 | 70 | result = _treeList[0]; 71 | return true; 72 | } 73 | 74 | public bool TryDequeue([MaybeNullWhen(false)] out T result) 75 | { 76 | if (_treeList.Count == 0) 77 | { 78 | result = default; 79 | return false; 80 | } 81 | 82 | result = _treeList[0]; 83 | _treeList.RemoveAt(0); 84 | return true; 85 | } 86 | 87 | void ICollection.CopyTo(Array array, int index) => ((ICollection)_treeList).CopyTo(array, index); 88 | 89 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 90 | 91 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 92 | 93 | internal void Validate(ValidationRules validationRules) 94 | { 95 | _treeList.Validate(validationRules); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListRemoveAt.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListRemoveAt 15 | { 16 | [Fact(DisplayName = "PosTest1: The generic type is int")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 10, 2, 4 }; 20 | TreeList listObject = new TreeList(iArray); 21 | int index = Generator.GetInt32(0, 10); 22 | listObject.RemoveAt(index); 23 | Assert.DoesNotContain(iArray[index], listObject); 24 | } 25 | 26 | [Fact(DisplayName = "PosTest2: The generic type is type of string and the element at the beginning would be removed")] 27 | public void PosTest2() 28 | { 29 | string[] strArray = { "dog", "apple", "joke", "banana", "chocolate", "dog", "food" }; 30 | TreeList listObject = new TreeList(strArray); 31 | listObject.RemoveAt(0); 32 | Assert.Equal(6, listObject.Count); 33 | 34 | for (int i = 0; i < 6; i++) 35 | { 36 | Assert.Equal(strArray[i + 1], listObject[i]); 37 | } 38 | } 39 | 40 | [Fact(DisplayName = "PosTest3: The generic type is a custom type and the element to be removed is at the end of the list")] 41 | public void PosTest3() 42 | { 43 | MyClass myclass1 = new MyClass(); 44 | MyClass myclass2 = new MyClass(); 45 | MyClass myclass3 = new MyClass(); 46 | MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; 47 | TreeList listObject = new TreeList(mc); 48 | listObject.RemoveAt(2); 49 | Assert.Equal(2, listObject.Count); 50 | Assert.DoesNotContain(myclass3, listObject); 51 | } 52 | 53 | [Fact(DisplayName = "NegTest1: The index is negative")] 54 | public void NegTest1() 55 | { 56 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 10, 2, 4 }; 57 | TreeList listObject = new TreeList(iArray); 58 | Assert.Throws(() => listObject.RemoveAt(-1)); 59 | } 60 | 61 | [Fact(DisplayName = "NegTest2: The index is greater than the range of the list")] 62 | public void NegTest2() 63 | { 64 | char?[] chArray = { 'a', 'b', ' ', 'c', null }; 65 | TreeList listObject = new TreeList(chArray); 66 | Assert.Throws(() => listObject.RemoveAt(10)); 67 | } 68 | 69 | public class MyClass 70 | { 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListIndexOf.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for the implementation of , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListIListIndexOf 15 | { 16 | [Fact(DisplayName = "PosTest1: Calling IndexOf method of IList,T is Value type.")] 17 | public void PosTest1() 18 | { 19 | TreeList myList = new TreeList(); 20 | int count = 10; 21 | int[] expectValue = new int[10]; 22 | IList myIList = myList; 23 | object? element = null; 24 | for (int i = 1; i <= count; i++) 25 | { 26 | element = i * count; 27 | myIList.Add(element); 28 | expectValue[i - 1] = (int)element; 29 | } 30 | 31 | for (int j = 0; j < myIList.Count; j++) 32 | { 33 | int current = myIList.IndexOf(myIList[j]); 34 | Assert.NotEqual(-1, current); 35 | } 36 | } 37 | 38 | [Fact(DisplayName = "PosTest2: Calling IndexOf method of IList,T is reference type.")] 39 | public void PosTest2() 40 | { 41 | TreeList myList = new TreeList(); 42 | int count = 10; 43 | string?[] expectValue = new string?[10]; 44 | object? element = null; 45 | IList myIList = myList; 46 | for (int i = 1; i <= count; i++) 47 | { 48 | element = i.ToString(); 49 | myIList.Add(element); 50 | expectValue[i - 1] = element.ToString(); 51 | } 52 | 53 | for (int j = 0; j < myIList.Count; j++) 54 | { 55 | int current = myIList.IndexOf(myIList[j]); 56 | Assert.NotEqual(-1, current); 57 | } 58 | } 59 | 60 | [Fact(DisplayName = "PosTest3: Calling IndexOf method of IList,T is reference type and the item is not exist in the List.")] 61 | public void PosTest3() 62 | { 63 | TreeList myList = new TreeList(); 64 | int count = 10; 65 | string?[] expectValue = new string?[10]; 66 | object? element = null; 67 | IList myIList = myList; 68 | for (int i = 1; i <= count; i++) 69 | { 70 | element = i.ToString(); 71 | myIList.Add(element); 72 | expectValue[i - 1] = element.ToString(); 73 | } 74 | 75 | for (int j = 0; j < myIList.Count; j++) 76 | { 77 | int current = myIList.IndexOf(null); 78 | Assert.Equal(-1, current); 79 | } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIListItem.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | using Xunit; 10 | 11 | /// 12 | /// Tests for the implementation of , derived from tests for 13 | /// in dotnet/coreclr. 14 | /// 15 | public class TreeListIListItem 16 | { 17 | [Fact(DisplayName = "PosTest1: Calling Add method of IList,T is Value type.")] 18 | public void PosTest1() 19 | { 20 | TreeList myList = new TreeList(); 21 | int count = 10; 22 | int[] expectValue = new int[10]; 23 | IList myIList = myList; 24 | object? element = null; 25 | for (int i = 1; i <= count; i++) 26 | { 27 | element = i * count; 28 | myIList.Add(element); 29 | expectValue[i - 1] = (int)element; 30 | } 31 | 32 | for (int j = 0; j < myIList.Count; j++) 33 | { 34 | int current = (int)myIList[j]!; 35 | Assert.Equal(expectValue[j], current); 36 | } 37 | } 38 | 39 | [Fact(DisplayName = "PosTest2: Calling Add method of IList,T is reference type.")] 40 | public void PosTest2() 41 | { 42 | TreeList myList = new TreeList(); 43 | int count = 10; 44 | string?[] expectValue = new string?[10]; 45 | object? element = null; 46 | IList myIList = myList; 47 | for (int i = 1; i <= count; i++) 48 | { 49 | element = i.ToString(); 50 | myIList.Add(element); 51 | expectValue[i - 1] = element.ToString(); 52 | } 53 | 54 | for (int j = 0; j < myIList.Count; j++) 55 | { 56 | string current = (string)myIList[j]!; 57 | Assert.Equal(expectValue[j], current); 58 | } 59 | } 60 | 61 | [Fact(DisplayName = "NegTest1: item is of a type that is not assignable to the IList.")] 62 | public void NegTest1() 63 | { 64 | TreeList myList = new TreeList(); 65 | IList myIList = myList; 66 | 67 | // int type should be add. but add null ArgumentNullException should be caught. 68 | Assert.Throws(() => myIList[0] = null); 69 | } 70 | 71 | [Fact(DisplayName = "NegTest2: index is not a valid index in the IList.")] 72 | public void NegTest2() 73 | { 74 | TreeList myList = new TreeList(); 75 | IList myIList = myList; 76 | 77 | // int type should be add. but add null ArgumentException should be caught. 78 | Assert.Throws(() => myIList[int.MaxValue] = 1); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/CopyTo1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class CopyTo1 15 | { 16 | [Fact(DisplayName = "PosTest1: The list is type of int")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; 20 | TreeList listObject = new TreeList(iArray); 21 | int[] result = new int[10]; 22 | listObject.CopyTo(result); 23 | for (int i = 0; i < 10; i++) 24 | { 25 | Assert.Equal(listObject[i], result[i]); 26 | } 27 | } 28 | 29 | [Fact(DisplayName = "PosTest2: The list is type of string")] 30 | public void PosTest2() 31 | { 32 | string[] strArray = { "Tom", "Jack", "Mike" }; 33 | TreeList listObject = new TreeList(strArray); 34 | string[] result = new string[3]; 35 | listObject.CopyTo(result); 36 | Assert.Equal("Tom", result[0]); 37 | Assert.Equal("Jack", result[1]); 38 | Assert.Equal("Mike", result[2]); 39 | } 40 | 41 | [Fact(DisplayName = "PosTest3: The generic type is a custom type")] 42 | public void PosTest3() 43 | { 44 | MyClass myclass1 = new MyClass(); 45 | MyClass myclass2 = new MyClass(); 46 | MyClass myclass3 = new MyClass(); 47 | TreeList listObject = new TreeList(); 48 | listObject.Add(myclass1); 49 | listObject.Add(myclass2); 50 | listObject.Add(myclass3); 51 | MyClass[] mc = new MyClass[3]; 52 | listObject.CopyTo(mc); 53 | Assert.Equal(myclass1, mc[0]); 54 | Assert.Equal(myclass2, mc[1]); 55 | Assert.Equal(myclass3, mc[2]); 56 | } 57 | 58 | [Fact(DisplayName = "NegTest1: The array is a null reference")] 59 | public void NegTest1() 60 | { 61 | int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; 62 | TreeList listObject = new TreeList(iArray); 63 | Assert.Throws(() => listObject.CopyTo(null!)); 64 | } 65 | 66 | [Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")] 67 | public void NegTest2() 68 | { 69 | int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; 70 | TreeList listObject = new TreeList(iArray); 71 | int[] result = new int[1]; 72 | Assert.Throws(() => listObject.CopyTo(result)); 73 | } 74 | 75 | public class MyClass 76 | { 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListReverse.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListReverse 14 | { 15 | [Fact(DisplayName = "PosTest1: The generic type is byte")] 16 | public void PosTest1() 17 | { 18 | byte[] byArray = new byte[1000]; 19 | Generator.GetBytes(-55, byArray); 20 | TreeList listObject = new TreeList(byArray); 21 | byte[] expected = Reverse(byArray); 22 | listObject.Reverse(); 23 | for (int i = 0; i < 1000; i++) 24 | { 25 | Assert.Equal(expected[i], listObject[i]); 26 | } 27 | } 28 | 29 | [Fact(DisplayName = "PosTest2: The generic type is type of string")] 30 | public void PosTest2() 31 | { 32 | string[] strArray = { "dog", "apple", "joke", "banana", "chocolate", "dog", "food", "Microsoft" }; 33 | TreeList listObject = new TreeList(strArray); 34 | listObject.Reverse(); 35 | string[] expected = Reverse(strArray); 36 | for (int i = 0; i < 8; i++) 37 | { 38 | Assert.Equal(expected[i], listObject[i]); 39 | } 40 | } 41 | 42 | [Fact(DisplayName = "PosTest3: The generic type is a custom type")] 43 | public void PosTest3() 44 | { 45 | MyClass myclass1 = new MyClass(); 46 | MyClass myclass2 = new MyClass(); 47 | MyClass myclass3 = new MyClass(); 48 | MyClass myclass4 = new MyClass(); 49 | MyClass[] mc = new MyClass[4] { myclass1, myclass2, myclass3, myclass4 }; 50 | TreeList listObject = new TreeList(mc); 51 | listObject.Reverse(); 52 | MyClass[] expected = new MyClass[4] { myclass4, myclass3, myclass2, myclass1 }; 53 | for (int i = 0; i < 4; i++) 54 | { 55 | Assert.Equal(expected[i], listObject[i]); 56 | } 57 | } 58 | 59 | [Fact(DisplayName = "PosTest4: The list has no element")] 60 | public void PosTest4() 61 | { 62 | TreeList listObject = new TreeList(); 63 | listObject.Reverse(); 64 | Assert.Empty(listObject); 65 | } 66 | 67 | private T[] Reverse(T[] arrayT) 68 | { 69 | T temp; 70 | int times = arrayT.Length / 2; 71 | for (int i = 0; i < times; i++) 72 | { 73 | temp = arrayT[i]; 74 | arrayT[i] = arrayT[arrayT.Length - 1 - i]; 75 | arrayT[arrayT.Length - 1 - i] = temp; 76 | } 77 | 78 | return arrayT; 79 | } 80 | 81 | public class MyClass 82 | { 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/Immutable/ImmutableTreeListFactoryTest.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.Immutable 5 | { 6 | using System.Collections.Generic; 7 | using TunnelVisionLabs.Collections.Trees.Immutable; 8 | using Xunit; 9 | 10 | /// 11 | /// Contains tests for the factory class. 12 | /// 13 | public class ImmutableTreeListFactoryTest 14 | { 15 | [Fact] 16 | public void TestCreateEmpty() 17 | { 18 | Assert.NotNull(ImmutableTreeList.Create()); 19 | Assert.Empty(ImmutableTreeList.Create()); 20 | Assert.Same(ImmutableTreeList.Create(), ImmutableTreeList.Create()); 21 | Assert.NotSame(ImmutableTreeList.Create(), ImmutableTreeList.Create()); 22 | } 23 | 24 | [Fact] 25 | public void TestCreateSingle() 26 | { 27 | Assert.NotNull(ImmutableTreeList.Create(1)); 28 | Assert.Single(ImmutableTreeList.Create(1)); 29 | Assert.NotSame(ImmutableTreeList.Create(1), ImmutableTreeList.Create(2)); 30 | Assert.Equal(1, ImmutableTreeList.Create(1)[0]); 31 | } 32 | 33 | [Fact] 34 | public void TestCreateMany() 35 | { 36 | Assert.NotNull(ImmutableTreeList.Create(1, 5, 4)); 37 | Assert.Equal(3, ImmutableTreeList.Create(1, 5, 4).Count); 38 | Assert.Equal(new[] { 1, 5, 4 }, ImmutableTreeList.Create(1, 5, 4)); 39 | } 40 | 41 | [Fact] 42 | public void TestCreateBuilder() 43 | { 44 | Assert.NotNull(ImmutableTreeList.CreateBuilder()); 45 | Assert.Empty(ImmutableTreeList.CreateBuilder()); 46 | Assert.NotSame(ImmutableTreeList.CreateBuilder(), ImmutableTreeList.CreateBuilder()); 47 | 48 | var builder1 = ImmutableTreeList.CreateBuilder(); 49 | var builder2 = ImmutableTreeList.CreateBuilder(); 50 | Assert.Empty(builder1); 51 | builder1.Add(1); 52 | Assert.Single(builder1); 53 | Assert.Empty(builder2); 54 | } 55 | 56 | [Fact] 57 | public void TestCreateRange() 58 | { 59 | Assert.NotNull(ImmutableTreeList.CreateRange(new[] { 1, 5, 4 })); 60 | Assert.Equal(3, ImmutableTreeList.CreateRange(new[] { 1, 5, 4 }).Count); 61 | Assert.Equal(new[] { 1, 5, 4 }, ImmutableTreeList.CreateRange(new[] { 1, 5, 4 })); 62 | } 63 | 64 | [Fact] 65 | public void TestToImmutableTreeList() 66 | { 67 | Assert.NotNull(new[] { 1, 5, 4 }.ToImmutableTreeList()); 68 | Assert.Equal(3, new[] { 1, 5, 4 }.ToImmutableTreeList().Count); 69 | Assert.Equal(new[] { 1, 5, 4 }, new[] { 1, 5, 4 }.ToImmutableTreeList()); 70 | 71 | // If the source is already an immutable tree list, the method simply returns the same instance 72 | IEnumerable source = ImmutableTreeList.Create(1, 5, 4); 73 | Assert.Same(source, source.ToImmutableTreeList()); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System.Collections.Generic; 7 | using Xunit; 8 | 9 | /// 10 | /// Tests for , derived from tests for 11 | /// in dotnet/coreclr. 12 | /// 13 | public class TreeListLastIndexOf1 14 | { 15 | [Fact(DisplayName = "PosTest1: The generic type is int")] 16 | public void PosTest1() 17 | { 18 | int[] iArray = new int[1000]; 19 | for (int i = 0; i < 1000; i++) 20 | { 21 | iArray[i] = i; 22 | } 23 | 24 | TreeList listObject = new TreeList(iArray); 25 | int ob = Generator.GetInt32(0, 1000); 26 | int result = listObject.LastIndexOf(ob); 27 | Assert.Equal(ob, result); 28 | } 29 | 30 | [Fact(DisplayName = "PosTest2: The generic type is type of string")] 31 | public void PosTest2() 32 | { 33 | string[] strArray = { "apple", "banana", "dog", "chocolate", "dog", "food" }; 34 | TreeList listObject = new TreeList(strArray); 35 | int result = listObject.LastIndexOf("dog"); 36 | Assert.Equal(4, result); 37 | } 38 | 39 | [Fact(DisplayName = "PosTest3: The generic type is a custom type")] 40 | public void PosTest3() 41 | { 42 | MyClass myclass1 = new MyClass(); 43 | MyClass myclass2 = new MyClass(); 44 | MyClass myclass3 = new MyClass(); 45 | MyClass[] mc = new MyClass[5] { myclass1, myclass2, myclass3, myclass3, myclass2 }; 46 | TreeList listObject = new TreeList(mc); 47 | int result = listObject.LastIndexOf(myclass3); 48 | Assert.Equal(3, result); 49 | } 50 | 51 | [Fact(DisplayName = "PosTest4: There are many element in the list with the same value")] 52 | public void PosTest4() 53 | { 54 | string[] strArray = { "apple", "banana", "chocolate", "banana", "banana", "dog", "banana", "food" }; 55 | TreeList listObject = new TreeList(strArray); 56 | int result = listObject.LastIndexOf("banana"); 57 | Assert.Equal(6, result); 58 | } 59 | 60 | [Fact(DisplayName = "PosTest5: Do not find the element")] 61 | public void PosTest5() 62 | { 63 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; 64 | TreeList listObject = new TreeList(iArray); 65 | int result = listObject.LastIndexOf(-10000); 66 | Assert.Equal(-1, result); 67 | } 68 | 69 | [Fact(DisplayName = "PosTest6: The argument is a null reference")] 70 | public void PosTest6() 71 | { 72 | string[] strArray = { "apple", "banana", "chocolate" }; 73 | TreeList listObject = new TreeList(strArray); 74 | int result = listObject.LastIndexOf(null); 75 | Assert.Equal(-1, result); 76 | } 77 | 78 | public class MyClass 79 | { 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListRemoveRange.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListRemoveRange 15 | { 16 | [Fact(DisplayName = "PosTest1: Remove all the elements in the int type list")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 10, 2, 4 }; 20 | TreeList listObject = new TreeList(iArray); 21 | listObject.RemoveRange(0, 10); 22 | Assert.Empty(listObject); 23 | } 24 | 25 | [Fact(DisplayName = "PosTest2: The generic type is type of string")] 26 | public void PosTest2() 27 | { 28 | string[] strArray = { "dog", "apple", "joke", "banana", "chocolate", "dog", "food" }; 29 | TreeList listObject = new TreeList(strArray); 30 | listObject.RemoveRange(3, 3); 31 | string[] expected = { "dog", "apple", "joke", "food" }; 32 | for (int i = 0; i < 4; i++) 33 | { 34 | Assert.Equal(expected[i], listObject[i]); 35 | } 36 | } 37 | 38 | [Fact(DisplayName = "PosTest3: The count argument is zero")] 39 | public void PosTest3() 40 | { 41 | MyClass myclass1 = new MyClass(); 42 | MyClass myclass2 = new MyClass(); 43 | MyClass myclass3 = new MyClass(); 44 | MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; 45 | TreeList listObject = new TreeList(mc); 46 | listObject.RemoveRange(1, 0); 47 | for (int i = 0; i < 3; i++) 48 | { 49 | Assert.Equal(mc[i], listObject[i]); 50 | } 51 | } 52 | 53 | [Fact(DisplayName = "NegTest1: The index is a negative number")] 54 | public void NegTest1() 55 | { 56 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 10, 2, 4 }; 57 | TreeList listObject = new TreeList(iArray); 58 | Assert.Throws(() => listObject.RemoveRange(-1, 3)); 59 | } 60 | 61 | [Fact(DisplayName = "NegTest2: The count is a negative number")] 62 | public void NegTest2() 63 | { 64 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 10, 2, 4 }; 65 | TreeList listObject = new TreeList(iArray); 66 | Assert.Throws(() => listObject.RemoveRange(3, -2)); 67 | } 68 | 69 | [Fact(DisplayName = "NegTest3: index and count do not denote a valid range of elements in the List")] 70 | public void NegTest3() 71 | { 72 | string[] strArray = { "dog", "apple", "joke", "banana", "chocolate", "dog", "food" }; 73 | TreeList listObject = new TreeList(strArray); 74 | Assert.Throws(() => listObject.RemoveRange(3, 10)); 75 | } 76 | 77 | public class MyClass 78 | { 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeList`1+Enumerator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | 10 | public partial class TreeList 11 | { 12 | public struct Enumerator : IEnumerator 13 | { 14 | private readonly TreeList _list; 15 | private readonly TreeSpan _span; 16 | private readonly int _version; 17 | 18 | private int _index; 19 | private LeafNode? _leafNode; 20 | private int _leafIndex; 21 | private T _current; 22 | 23 | internal Enumerator(TreeList list) 24 | : this(list, list._root.Span) 25 | { 26 | } 27 | 28 | internal Enumerator(TreeList list, TreeSpan span) 29 | { 30 | _list = list; 31 | _span = span; 32 | _version = list._version; 33 | _index = -1; 34 | _leafNode = null; 35 | _leafIndex = -1; 36 | _current = default!; 37 | } 38 | 39 | public T Current => _current; 40 | 41 | object? IEnumerator.Current => Current; 42 | 43 | public void Dispose() 44 | { 45 | } 46 | 47 | public bool MoveNext() 48 | { 49 | if (_list._version != _version) 50 | throw new InvalidOperationException(); 51 | 52 | if (_index < -1) 53 | { 54 | // Past the end of the list. 55 | return false; 56 | } 57 | 58 | if (_index == -1) 59 | { 60 | if (_span.IsEmpty) 61 | { 62 | _index = int.MinValue; 63 | return false; 64 | } 65 | 66 | // Need to get the first leaf node 67 | (_leafNode, _leafIndex) = _list._root.GetLeafNode(_span.Start); 68 | 69 | // The index and leaf index will be incremented below; want the correct final result 70 | _index = _span.Start - 1; 71 | _leafIndex--; 72 | } 73 | else if (_leafIndex == _leafNode!.Count - 1) 74 | { 75 | // Need to move to the next leaf 76 | _leafNode = _leafNode.Next; 77 | _leafIndex = -1; 78 | } 79 | 80 | _index++; 81 | if (_index == _span.EndExclusive) 82 | { 83 | _index = int.MinValue; 84 | return false; 85 | } 86 | 87 | _leafIndex++; 88 | _current = _leafNode![_leafIndex]; 89 | return true; 90 | } 91 | 92 | void IEnumerator.Reset() => InternalReset(); 93 | 94 | internal void InternalReset() 95 | { 96 | if (_list._version != _version) 97 | throw new InvalidOperationException(); 98 | 99 | _leafNode = null; 100 | _index = -1; 101 | _leafIndex = -1; 102 | _current = default!; 103 | } 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListForEach.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListForEach 15 | { 16 | [Fact(DisplayName = "PosTest1: The generic type is int")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; 20 | TreeList listObject = new TreeList(iArray); 21 | MyClass myClass = new MyClass(); 22 | Action action = new Action(myClass.SumCalc); 23 | listObject.ForEach(action); 24 | Assert.Equal(40, myClass.Sum); 25 | } 26 | 27 | [Fact(DisplayName = "PosTest2: The generic type is type of string")] 28 | public void PosTest2() 29 | { 30 | string[] strArray = { "Hello", "wor", "l", "d" }; 31 | TreeList listObject = new TreeList(strArray); 32 | MyClass myClass = new MyClass(); 33 | Action action = new Action(myClass.JoinStr); 34 | listObject.ForEach(action); 35 | Assert.Equal("Helloworld", myClass.Result); 36 | } 37 | 38 | [Fact(DisplayName = "PosTest3: The generic type is custom type")] 39 | public void PosTest3() 40 | { 41 | MyClass2 myclass1 = new MyClass2('h'); 42 | MyClass2 myclass2 = new MyClass2('='); 43 | MyClass2 myclass3 = new MyClass2('&'); 44 | MyClass2[] mc = new MyClass2[3] { myclass1, myclass2, myclass3 }; 45 | TreeList listObject = new TreeList(mc); 46 | MyClass myClass = new MyClass(); 47 | Action action = new Action(myClass.DeleteValue); 48 | listObject.ForEach(action); 49 | for (int i = 0; i < 3; i++) 50 | { 51 | Assert.Null(mc[i].Value); 52 | } 53 | } 54 | 55 | [Fact(DisplayName = "NegTest1: The action is a null reference")] 56 | public void NegTest1() 57 | { 58 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; 59 | TreeList listObject = new TreeList(iArray); 60 | Action? action = null; 61 | Assert.Throws(() => listObject.ForEach(action!)); 62 | } 63 | 64 | public class MyClass 65 | { 66 | public int Sum { get; set; } = 0; 67 | 68 | public string? Result { get; set; } 69 | 70 | public void SumCalc(int a) 71 | { 72 | Sum = Sum + a; 73 | } 74 | 75 | public void JoinStr(string a) 76 | { 77 | Result = Result + a; 78 | } 79 | 80 | public void DeleteValue(MyClass2 mc) 81 | { 82 | mc.Value = null; 83 | } 84 | } 85 | 86 | public class MyClass2 87 | { 88 | public MyClass2(char c) 89 | { 90 | Value = c; 91 | } 92 | 93 | public char? Value 94 | { 95 | get; 96 | set; 97 | } 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListIndexOf2.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListIndexOf2 15 | { 16 | [Fact(DisplayName = "PosTest1: The generic type is int")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = new int[1000]; 20 | for (int i = 0; i < 1000; i++) 21 | { 22 | iArray[i] = i; 23 | } 24 | 25 | TreeList listObject = new TreeList(iArray); 26 | int ob = Generator.GetInt32(0, 1000); 27 | int result = listObject.IndexOf(ob, 0); 28 | Assert.Equal(ob, result); 29 | } 30 | 31 | [Fact(DisplayName = "PosTest2: The generic type is type of string")] 32 | public void PosTest2() 33 | { 34 | string[] strArray = { "apple", "dog", "banana", "chocolate", "dog", "food" }; 35 | TreeList listObject = new TreeList(strArray); 36 | int result = listObject.IndexOf("dog", 2); 37 | Assert.Equal(4, result); 38 | } 39 | 40 | [Fact(DisplayName = "PosTest3: The generic type is a custom type")] 41 | public void PosTest3() 42 | { 43 | MyClass myclass1 = new MyClass(); 44 | MyClass myclass2 = new MyClass(); 45 | MyClass myclass3 = new MyClass(); 46 | MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; 47 | TreeList listObject = new TreeList(mc); 48 | int result = listObject.IndexOf(myclass3, 2); 49 | Assert.Equal(2, result); 50 | } 51 | 52 | [Fact(DisplayName = "PosTest4: There are many element in the list with the same value")] 53 | public void PosTest4() 54 | { 55 | string[] strArray = { "apple", "banana", "chocolate", "banana", "banana", "dog", "banana", "food" }; 56 | TreeList listObject = new TreeList(strArray); 57 | int result = listObject.IndexOf("banana", 2); 58 | Assert.Equal(3, result); 59 | } 60 | 61 | [Fact(DisplayName = "PosTest5: Do not find the element")] 62 | public void PosTest5() 63 | { 64 | int[] iArray = { 1, 9, -11, 3, 6, -1, 8, 7, 1, 2, 4 }; 65 | TreeList listObject = new TreeList(iArray); 66 | int result = listObject.IndexOf(-11, 4); 67 | Assert.Equal(-1, result); 68 | } 69 | 70 | [Fact(DisplayName = "NegTest1: The index is negative")] 71 | public void NegTest1() 72 | { 73 | int[] iArray = { 1, 9, -11, 3, 6, -1, 8, 7, 1, 2, 4 }; 74 | TreeList listObject = new TreeList(iArray); 75 | Assert.Throws(() => listObject.IndexOf(-11, -4)); 76 | } 77 | 78 | [Fact(DisplayName = "NegTest2: The index is greater than the last index of the list")] 79 | public void NegTest2() 80 | { 81 | int[] iArray = { 1, 9, -11, 3, 6, -1, 8, 7, 1, 2, 4 }; 82 | TreeList listObject = new TreeList(iArray); 83 | Assert.Throws(() => listObject.IndexOf(-11, 12)); 84 | } 85 | 86 | public class MyClass 87 | { 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/TreeListLastIndexOf2.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class TreeListLastIndexOf2 15 | { 16 | [Fact(DisplayName = "PosTest1: The generic type is int")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = new int[1000]; 20 | for (int i = 0; i < 1000; i++) 21 | { 22 | iArray[i] = i; 23 | } 24 | 25 | TreeList listObject = new TreeList(iArray); 26 | int ob = Generator.GetInt32(0, 1000); 27 | int result = listObject.LastIndexOf(ob, 999); 28 | Assert.Equal(ob, result); 29 | } 30 | 31 | [Fact(DisplayName = "PosTest2: The generic type is type of string")] 32 | public void PosTest2() 33 | { 34 | string[] strArray = { "apple", "dog", "banana", "chocolate", "dog", "food" }; 35 | TreeList listObject = new TreeList(strArray); 36 | int result = listObject.LastIndexOf("dog", 3); 37 | Assert.Equal(1, result); 38 | } 39 | 40 | [Fact(DisplayName = "PosTest3: The generic type is a custom type")] 41 | public void PosTest3() 42 | { 43 | MyClass myclass1 = new MyClass(); 44 | MyClass myclass2 = new MyClass(); 45 | MyClass myclass3 = new MyClass(); 46 | MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; 47 | TreeList listObject = new TreeList(mc); 48 | int result = listObject.LastIndexOf(myclass3, 2); 49 | Assert.Equal(2, result); 50 | } 51 | 52 | [Fact(DisplayName = "PosTest4: There are many element in the list with the same value")] 53 | public void PosTest4() 54 | { 55 | string[] strArray = { "apple", "banana", "chocolate", "banana", "banana", "dog", "banana", "food" }; 56 | TreeList listObject = new TreeList(strArray); 57 | int result = listObject.LastIndexOf("banana", 5); 58 | Assert.Equal(4, result); 59 | } 60 | 61 | [Fact(DisplayName = "PosTest5: Do not find the element")] 62 | public void PosTest5() 63 | { 64 | int[] iArray = { 1, 9, -15, 3, 6, -1, 8, 7, -11, 2, 4 }; 65 | TreeList listObject = new TreeList(iArray); 66 | int result = listObject.LastIndexOf(-11, 6); 67 | Assert.Equal(-1, result); 68 | } 69 | 70 | [Fact(DisplayName = "NegTest1: The index is negative")] 71 | public void NegTest1() 72 | { 73 | int[] iArray = { 1, 9, -11, 3, 6, -1, 8, 7, 1, 2, 4 }; 74 | TreeList listObject = new TreeList(iArray); 75 | Assert.Throws(() => listObject.LastIndexOf(-11, -4)); 76 | } 77 | 78 | [Fact(DisplayName = "NegTest2: The index is greater than the last index of the list")] 79 | public void NegTest2() 80 | { 81 | int[] iArray = { 1, 9, -11, 3, 6, -1, 8, 7, 1, 2, 4 }; 82 | TreeList listObject = new TreeList(iArray); 83 | Assert.Throws(() => listObject.LastIndexOf(-11, 12)); 84 | } 85 | 86 | public class MyClass 87 | { 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/Generator.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System; 7 | using System.Diagnostics; 8 | 9 | internal static class Generator 10 | { 11 | private static Random _rand = new Random(); 12 | private static int? _seed = null; 13 | 14 | public static int? Seed 15 | { 16 | get 17 | { 18 | if (_seed.HasValue) 19 | { 20 | return _seed.Value; 21 | } 22 | else 23 | { 24 | return null; 25 | } 26 | } 27 | 28 | set 29 | { 30 | if (!_seed.HasValue) 31 | { 32 | _seed = value; 33 | if (_seed.HasValue) 34 | { 35 | Debug.WriteLine("Seeding Random with: " + _seed.Value.ToString()); 36 | _rand = new Random(_seed.Value); 37 | } 38 | } 39 | else 40 | { 41 | Debug.WriteLine("Attempt to seed Random to " + value.ToString() + " rejected it was already seeded to: " + _seed.Value.ToString()); 42 | } 43 | } 44 | } 45 | 46 | // returns a byte array of random data 47 | public static void GetBytes(int new_seed, byte[] buffer) 48 | { 49 | Seed = new_seed; 50 | GetBytes(buffer); 51 | } 52 | 53 | public static void GetBytes(byte[] buffer) 54 | { 55 | _rand.NextBytes(buffer); 56 | Debug.WriteLine("Random Byte[] produced: " + Convert.ToBase64String(buffer)); 57 | } 58 | 59 | /// 60 | /// Gets an integer value between 0 and . 61 | /// 62 | /// An integer value between 0 and . 63 | public static int GetInt32() 64 | { 65 | int i = _rand.Next(); 66 | Debug.WriteLine("Random Int32 produced: " + i.ToString()); 67 | return i; 68 | } 69 | 70 | /// 71 | /// Gets a non-negative integer value less than . 72 | /// 73 | /// The exclusive maximum for the generated random value. 74 | /// A non-negative integer value less than . 75 | public static int GetInt32(int maxValue) 76 | { 77 | int i = _rand.Next(maxValue); 78 | Debug.WriteLine("Random Int32 produced: " + i.ToString()); 79 | return i; 80 | } 81 | 82 | public static int GetInt32(int minValue, int maxValue) 83 | { 84 | if (minValue == maxValue) 85 | { 86 | return minValue; 87 | } 88 | 89 | if (minValue < maxValue) 90 | { 91 | Seed = -55; 92 | return minValue + (GetInt32() % (maxValue - minValue)); 93 | } 94 | 95 | return minValue; 96 | } 97 | 98 | // returns a non-negative Byte between 0 and Byte.MaxValue 99 | public static byte GetByte(int new_seed) 100 | { 101 | Seed = new_seed; 102 | return GetByte(); 103 | } 104 | 105 | public static byte GetByte() 106 | { 107 | byte i = Convert.ToByte(_rand.Next() % (1 + byte.MaxValue)); 108 | Debug.WriteLine("Random Byte produced: " + i.ToString()); 109 | return i; 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/ThrowingTraceListener.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test 5 | { 6 | using System; 7 | using System.Diagnostics; 8 | using System.Diagnostics.CodeAnalysis; 9 | using System.Runtime.Serialization; 10 | 11 | // To enable this for a process, add the following to the app.config for the project: 12 | // 13 | // 14 | // 15 | // 16 | // 17 | // 18 | // 19 | // 20 | // 21 | // 22 | // 23 | [ExcludeFromCodeCoverage] 24 | public sealed class ThrowingTraceListener : TraceListener 25 | { 26 | public override void Fail(string message, string detailMessage) 27 | { 28 | throw new DebugAssertFailureException(message + Environment.NewLine + detailMessage); 29 | } 30 | 31 | public override void Write(object o) 32 | { 33 | if (Debugger.IsLogging()) 34 | { 35 | Debugger.Log(0, null, o?.ToString()); 36 | } 37 | } 38 | 39 | public override void Write(object o, string category) 40 | { 41 | if (Debugger.IsLogging()) 42 | { 43 | Debugger.Log(0, category, o?.ToString()); 44 | } 45 | } 46 | 47 | public override void Write(string message) 48 | { 49 | if (Debugger.IsLogging()) 50 | { 51 | Debugger.Log(0, null, message); 52 | } 53 | } 54 | 55 | public override void Write(string message, string category) 56 | { 57 | if (Debugger.IsLogging()) 58 | { 59 | Debugger.Log(0, category, message); 60 | } 61 | } 62 | 63 | public override void WriteLine(object o) 64 | { 65 | if (Debugger.IsLogging()) 66 | { 67 | Debugger.Log(0, null, o?.ToString() + Environment.NewLine); 68 | } 69 | } 70 | 71 | public override void WriteLine(object o, string category) 72 | { 73 | if (Debugger.IsLogging()) 74 | { 75 | Debugger.Log(0, category, o?.ToString() + Environment.NewLine); 76 | } 77 | } 78 | 79 | public override void WriteLine(string message) 80 | { 81 | if (Debugger.IsLogging()) 82 | { 83 | Debugger.Log(0, null, message + Environment.NewLine); 84 | } 85 | } 86 | 87 | public override void WriteLine(string message, string category) 88 | { 89 | if (Debugger.IsLogging()) 90 | { 91 | Debugger.Log(0, category, message + Environment.NewLine); 92 | } 93 | } 94 | 95 | [Serializable] 96 | public class DebugAssertFailureException : Exception 97 | { 98 | public DebugAssertFailureException() 99 | { 100 | } 101 | 102 | public DebugAssertFailureException(string message) 103 | : base(message) 104 | { 105 | } 106 | 107 | public DebugAssertFailureException(string message, Exception inner) 108 | : base(message, inner) 109 | { 110 | } 111 | 112 | protected DebugAssertFailureException( 113 | SerializationInfo info, 114 | StreamingContext context) 115 | : base(info, context) 116 | { 117 | } 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/Immutable/ImmutableTreeListTest+ForEach.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.Immutable 5 | { 6 | using System; 7 | using TunnelVisionLabs.Collections.Trees.Immutable; 8 | using Xunit; 9 | 10 | public partial class ImmutableTreeListTest 11 | { 12 | /// 13 | /// Tests for . 14 | /// 15 | public class ForEach 16 | { 17 | [Fact(DisplayName = "PosTest1: The generic type is int")] 18 | public void PosTest1() 19 | { 20 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; 21 | var listObject = ImmutableTreeList.Create(iArray); 22 | var myClass = new MyClass(); 23 | var action = new Action(myClass.SumCalc); 24 | listObject.ForEach(action); 25 | Assert.Equal(40, myClass.Sum); 26 | } 27 | 28 | [Fact(DisplayName = "PosTest2: The generic type is type of string")] 29 | public void PosTest2() 30 | { 31 | string[] strArray = { "Hello", "wor", "l", "d" }; 32 | var listObject = ImmutableTreeList.Create(strArray); 33 | var myClass = new MyClass(); 34 | var action = new Action(myClass.JoinStr); 35 | listObject.ForEach(action); 36 | Assert.Equal("Helloworld", myClass.Result); 37 | } 38 | 39 | [Fact(DisplayName = "PosTest3: The generic type is custom type")] 40 | public void PosTest3() 41 | { 42 | var myclass1 = new MyClass2('h'); 43 | var myclass2 = new MyClass2('='); 44 | var myclass3 = new MyClass2('&'); 45 | var mc = new MyClass2[3] { myclass1, myclass2, myclass3 }; 46 | var listObject = ImmutableTreeList.Create(mc); 47 | var myClass = new MyClass(); 48 | var action = new Action(myClass.DeleteValue); 49 | listObject.ForEach(action); 50 | for (int i = 0; i < 3; i++) 51 | { 52 | Assert.Null(mc[i].Value); 53 | } 54 | } 55 | 56 | [Fact(DisplayName = "NegTest1: The action is a null reference")] 57 | public void NegTest1() 58 | { 59 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; 60 | var listObject = ImmutableTreeList.Create(iArray); 61 | Action? action = null; 62 | Assert.Throws(() => listObject.ForEach(action!)); 63 | } 64 | 65 | public class MyClass 66 | { 67 | public int Sum { get; set; } = 0; 68 | 69 | public string? Result 70 | { 71 | get; set; 72 | } 73 | 74 | public void SumCalc(int a) 75 | { 76 | Sum = Sum + a; 77 | } 78 | 79 | public void JoinStr(string a) 80 | { 81 | Result = Result + a; 82 | } 83 | 84 | public void DeleteValue(MyClass2 mc) 85 | { 86 | mc.Value = null; 87 | } 88 | } 89 | 90 | public class MyClass2 91 | { 92 | public MyClass2(char c) 93 | { 94 | Value = c; 95 | } 96 | 97 | public char? Value 98 | { 99 | get; 100 | set; 101 | } 102 | } 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.ruleset: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/Immutable/ImmutableTreeListBuilderTest+ForEach.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.Immutable 5 | { 6 | using System; 7 | using TunnelVisionLabs.Collections.Trees.Immutable; 8 | using Xunit; 9 | 10 | public partial class ImmutableTreeListBuilderTest 11 | { 12 | /// 13 | /// Tests for . 14 | /// 15 | public class ForEach 16 | { 17 | [Fact(DisplayName = "PosTest1: The generic type is int")] 18 | public void PosTest1() 19 | { 20 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; 21 | var listObject = ImmutableTreeList.Create(iArray).ToBuilder(); 22 | var myClass = new MyClass(); 23 | var action = new Action(myClass.SumCalc); 24 | listObject.ForEach(action); 25 | Assert.Equal(40, myClass.Sum); 26 | } 27 | 28 | [Fact(DisplayName = "PosTest2: The generic type is type of string")] 29 | public void PosTest2() 30 | { 31 | string[] strArray = { "Hello", "wor", "l", "d" }; 32 | var listObject = ImmutableTreeList.Create(strArray).ToBuilder(); 33 | var myClass = new MyClass(); 34 | var action = new Action(myClass.JoinStr); 35 | listObject.ForEach(action); 36 | Assert.Equal("Helloworld", myClass.Result); 37 | } 38 | 39 | [Fact(DisplayName = "PosTest3: The generic type is custom type")] 40 | public void PosTest3() 41 | { 42 | var myclass1 = new MyClass2('h'); 43 | var myclass2 = new MyClass2('='); 44 | var myclass3 = new MyClass2('&'); 45 | var mc = new MyClass2[3] { myclass1, myclass2, myclass3 }; 46 | var listObject = ImmutableTreeList.Create(mc).ToBuilder(); 47 | var myClass = new MyClass(); 48 | var action = new Action(myClass.DeleteValue); 49 | listObject.ForEach(action); 50 | for (int i = 0; i < 3; i++) 51 | { 52 | Assert.Null(mc[i].Value); 53 | } 54 | } 55 | 56 | [Fact(DisplayName = "NegTest1: The action is a null reference")] 57 | public void NegTest1() 58 | { 59 | int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; 60 | var listObject = ImmutableTreeList.Create(iArray).ToBuilder(); 61 | Action? action = null; 62 | Assert.Throws(() => listObject.ForEach(action!)); 63 | } 64 | 65 | public class MyClass 66 | { 67 | public int Sum { get; set; } = 0; 68 | 69 | public string? Result 70 | { 71 | get; set; 72 | } 73 | 74 | public void SumCalc(int a) 75 | { 76 | Sum = Sum + a; 77 | } 78 | 79 | public void JoinStr(string a) 80 | { 81 | Result = Result + a; 82 | } 83 | 84 | public void DeleteValue(MyClass2 mc) 85 | { 86 | mc.Value = null; 87 | } 88 | } 89 | 90 | public class MyClass2 91 | { 92 | public MyClass2(char c) 93 | { 94 | Value = c; 95 | } 96 | 97 | public char? Value 98 | { 99 | get; 100 | set; 101 | } 102 | } 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees/TreeDictionary`2+KeyCollection.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees 5 | { 6 | using System; 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | using System.Diagnostics; 10 | 11 | public partial class TreeDictionary 12 | { 13 | public partial struct KeyCollection : ICollection, IReadOnlyCollection, ICollection 14 | { 15 | private readonly TreeDictionary _dictionary; 16 | 17 | internal KeyCollection(TreeDictionary dictionary) 18 | { 19 | Debug.Assert(dictionary != null, $"Assertion failed: {nameof(dictionary)} != null"); 20 | _dictionary = dictionary; 21 | } 22 | 23 | public int Count => _dictionary.Count; 24 | 25 | bool ICollection.IsReadOnly => false; 26 | 27 | bool ICollection.IsSynchronized => false; 28 | 29 | object ICollection.SyncRoot => ((ICollection)_dictionary).SyncRoot; 30 | 31 | void ICollection.Add(TKey item) => throw new NotSupportedException(); 32 | 33 | public void Clear() => _dictionary.Clear(); 34 | 35 | public bool Contains(TKey item) => _dictionary.ContainsKey(item); 36 | 37 | public void CopyTo(TKey[] array, int arrayIndex) 38 | { 39 | if (array == null) 40 | throw new ArgumentNullException(nameof(array)); 41 | if (arrayIndex < 0 || arrayIndex > array.Length) 42 | throw new ArgumentOutOfRangeException(nameof(arrayIndex)); 43 | if (array.Length - arrayIndex < _dictionary.Count) 44 | throw new ArgumentException(); 45 | 46 | int i = arrayIndex; 47 | foreach (TKey key in this) 48 | { 49 | array[i] = key; 50 | i++; 51 | } 52 | } 53 | 54 | public Enumerator GetEnumerator() => new Enumerator(_dictionary.GetEnumerator()); 55 | 56 | public bool Remove(TKey item) => _dictionary.Remove(item); 57 | 58 | void ICollection.CopyTo(Array array, int index) 59 | { 60 | if (array == null) 61 | throw new ArgumentNullException(nameof(array)); 62 | if (array.Rank != 1) 63 | throw new ArgumentException(); 64 | if (array.GetLowerBound(0) != 0) 65 | throw new ArgumentException(); 66 | if (index < 0 || index > array.Length) 67 | throw new ArgumentOutOfRangeException(nameof(index)); 68 | if (array.Length - index < _dictionary.Count) 69 | throw new ArgumentException(); 70 | 71 | if (array is TKey[] keys) 72 | { 73 | CopyTo(keys, index); 74 | } 75 | else if (array is object[] objects) 76 | { 77 | try 78 | { 79 | int i = index; 80 | foreach (TKey key in this) 81 | { 82 | objects[i] = key; 83 | i++; 84 | } 85 | } 86 | catch (ArrayTypeMismatchException) 87 | { 88 | throw new ArgumentException(); 89 | } 90 | } 91 | else 92 | { 93 | throw new ArgumentException(); 94 | } 95 | } 96 | 97 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 98 | 99 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch1.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. 3 | 4 | namespace TunnelVisionLabs.Collections.Trees.Test.List 5 | { 6 | using System; 7 | using System.Collections.Generic; 8 | using Xunit; 9 | 10 | /// 11 | /// Tests for , derived from tests for 12 | /// in dotnet/coreclr. 13 | /// 14 | public class BinarySearch1 15 | { 16 | [Fact(DisplayName = "PosTest1: The generic type is int")] 17 | public void PosTest1() 18 | { 19 | int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 }; 20 | TreeList listObject = new TreeList(iArray); 21 | listObject.Sort(); 22 | int i = Generator.GetInt32(0, 10); 23 | Assert.Equal(i, listObject.BinarySearch(i)); 24 | } 25 | 26 | [Fact(DisplayName = "PosTest2: The generic type is a referece type of string")] 27 | public void PosTest2() 28 | { 29 | string[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; 30 | TreeList listObject = new TreeList(strArray); 31 | Assert.Equal(-5, listObject.BinarySearch("egg")); 32 | } 33 | 34 | [Fact(DisplayName = "PosTest3: There are many elements with the same value")] 35 | public void PosTest3() 36 | { 37 | string[] strArray = { "key", "keys", "key", "key", "sky", "key" }; 38 | TreeList listObject = new TreeList(strArray); 39 | Assert.True(listObject.BinarySearch("key") >= 0); 40 | } 41 | 42 | [Fact(DisplayName = "PosTest4: The generic type is custom type")] 43 | public void PosTest4() 44 | { 45 | MyClass myclass1 = new MyClass(10); 46 | MyClass myclass2 = new MyClass(20); 47 | MyClass myclass3 = new MyClass(30); 48 | MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 }; 49 | TreeList listObject = new TreeList(mc); 50 | listObject.Sort(); 51 | Assert.Equal(1, listObject.BinarySearch(new MyClass(20))); 52 | } 53 | 54 | [Fact] 55 | public void TestComparable() 56 | { 57 | MyClass first = new MyClass(10); 58 | MyClass second = new MyClass(20); 59 | Assert.Equal(-1, first.CompareTo(second)); 60 | Assert.Equal(1, second.CompareTo(first)); 61 | Assert.Equal(1, first.CompareTo(null)); 62 | } 63 | 64 | [Fact(DisplayName = "PosTest5: The item to be search is a null reference")] 65 | public void PosTest5() 66 | { 67 | string[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; 68 | TreeList listObject = new TreeList(strArray); 69 | Assert.Equal(-1, listObject.BinarySearch(null)); 70 | } 71 | 72 | [Fact(DisplayName = "NegTest1: IComparable generic interface was not implemented")] 73 | public void NegTest1() 74 | { 75 | TestClass[] tc = new TestClass[2] { new TestClass(), new TestClass() }; 76 | TreeList listObject = new TreeList(tc); 77 | Assert.Throws(() => listObject.BinarySearch(new TestClass())); 78 | } 79 | 80 | public class MyClass : IComparable 81 | { 82 | private int _value; 83 | 84 | public MyClass(int a) 85 | { 86 | _value = a; 87 | } 88 | 89 | public int CompareTo(object? obj) 90 | { 91 | if (obj is null) 92 | return 1; 93 | 94 | return _value.CompareTo(((MyClass)obj)._value); 95 | } 96 | } 97 | 98 | public class TestClass 99 | { 100 | } 101 | } 102 | } 103 | --------------------------------------------------------------------------------