Skip to content

Repository files navigation

Codacy Badge CodeFactor CodeQL

Actions Status NuGet Version and Downloads count C#
Actions Status NuGet Version and Downloads count C++

LinksPlatform's Platform.Collections.Methods Class Library.

Namespaces: Platform.Collections.Methods.Lists, Platform.Collections.Methods.Trees

Forked from: Konard/LinksPlatform/Platform/Platform.Data.Core/Collections

NuGet package: Platform.Collections.Methods

Use version 1.0.1 or later on .NET 8 and later. It supports the Platform.Collections.Methods.Trees and Platform.Collections.Methods.Lists namespaces used in 0.7.3. The old 1.0.0 package is unlisted. Exact version references can still restore it, so update those references explicitly. Pin 0.7.3 while waiting for 1.0.1 to appear on NuGet or if you target .NET 7.

PDF file with code for e-readers.

The C# workflow builds the DocFX site on every pull request and publishes it from main, including the API documentation PDF.

Depend on

Dependent libraries

Trees

  • SizeBalancedTreeMethods runs Chen's recursive Maintain on the way back from every attach and detach. Its height is at most the AVL bound 1.44 log₂(n + 2).
  • RecursionlessSizeBalancedTreeMethods makes the same rotations as the recursive tree, without recursion: the attach path is remembered while descending and only its nodes are maintained. It builds identical trees to SizeBalancedTreeMethods.
  • SizedAndThreadedAVLBalancedTreeMethods keeps AVL balance factors and threaded leaves. Its height is at most the AVL bound.

Every node t of a size balanced tree satisfies Chen's property: size(t.left) >= size(t.right.left), size(t.right.right) and size(t.right) >= size(t.left.left), size(t.left.right). Trees that an older version left unbalanced are rebuilt once, in linear time, when an attach or detach passes more levels than any size balanced tree of their size has.

Lists

The four doubly linked lists keep previous and next links in every element:

  • Absolute* lists keep one list: its first and last element and its size are stored once, for example in fields.
  • Relative* lists keep a list per head element, so one storage can hold many lists, for example the lists of all links that share a source.
  • *Circular* lists link the last element back to the first one; *Open* lists end with 0 on both sides.

Attach and detach are O(1) for all of them.

Choosing a structure

The benchmarks below measure these trade-offs:

  • Use SizedAndThreadedAVLBalancedTreeMethods when nodes are attached and detached more often than searched: it attaches and detaches in 0.4–0.75 of the time of the size balanced tree. Its threads also give the inheritor the next and previous node (GetNext, GetPrevious) without a stack. The price is 16 instead of 12 bytes per uint node (a balance factor and two thread flags) and searches that take 1.06–2.1 times as long, because every step checks a thread flag.
  • Use SizeBalancedTreeMethods when searches dominate or memory matters: it searches as fast as the recursionless tree and faster than the AVL tree, stores only left, right and size, and attaches and detaches in 0.68–0.91 of the time of the recursionless tree.
  • Use RecursionlessSizeBalancedTreeMethods when the stack is limited or the tree comes from data that other code may have linked badly: it builds the same trees as SizeBalancedTreeMethods and searches as fast, but its stack use does not depend on the tree, for 1.1–1.46 times the attach and detach time.
  • Use a list when the order of the elements is the order they were added in and no search by key is needed: its attach and detach are O(1), 24 to 145 times faster than those of a tree with 1000 elements. The *Open* lists are about 0.8 of the time of the *Circular* ones.

Memory

The methods store nothing per node: the inheritor decides where the fields of a node live (an array, memory mapped file and so on) and the methods read and write them through abstract accessors. The bytes per node are those of the test and benchmark storages, which keep the fields in an array of structs; they double for ulong nodes.

Structure Fields of a node Bytes per node (uint) Memory besides the nodes
SizeBalancedTreeMethods left, right, size 12 one stack frame per level of the path, at most 1.44 log₂(n + 2) levels
RecursionlessSizeBalancedTreeMethods left, right, size 12 a reused path of steps (parent and two flags, 8 bytes for uint): C# allocates 64 + 8 steps with the tree and doubles them when a path is longer; C++ grows a std::vector on the first attach
SizedAndThreadedAVLBalancedTreeMethods left, right, size, balance, two thread flags 16 a path of 11 · sizeof(TElement) + 4 nodes (192 bytes for uint): a field array allocated with the tree in C#, a std::array on the stack in C++
Doubly linked lists previous, next 8 first, last and size once (Absolute*) or per head element (Relative*)

Attach and detach allocate nothing on the heap once the path buffers have grown to the height of the tree; the tests count the allocated bytes in both languages to prove it.

Limits

  • Nodes are the numbers 1..TElement.MaxValue, 0 means no node. A tree or a list holds at most 255 byte, 65 535 ushort, 4 294 967 295 uint or 2⁶⁴ − 1 ulong nodes, and the size of a tree always fits in TElement. The tests fill byte trees with all 255 nodes. In practice the storage is the limit: 4 294 967 295 uint nodes take 48 GiB in the size balanced tree storage above.
  • A tree of n nodes is at most 1.44 log₂(n + 2) levels high, which is 91 levels for 2⁶⁴ − 1 nodes, so search, attach and detach are O(log n) in the worst case.
  • Keys must be unique. The AVL tree refuses to attach a node whose key is in the tree; the size balanced trees attach it, but cannot detach nodes whose key is shared and throw InvalidOperationException.
  • Detaching a node that is not in the tree throws InvalidOperationException and leaves the tree intact.
  • A tree that other code linked deeper than any balanced tree: the size balanced trees rebuild it on the next attach or detach (the recursive tree recurses along that path once, so prefer the recursionless tree for such data); the AVL tree throws IndexOutOfRangeException in C# and std::out_of_range in C++ instead of overflowing its path.
  • An instance is not thread-safe: the recursionless and AVL trees reuse their path buffers, and all structures write the nodes. Use an instance per thread or a lock.

Tests

The C# and C++ tests run on every pull request and on main (csharp, Test cpp). They check after every attach and detach that the trees keep their sizes, order and balance property, that the height stays within the AVL bound, that the recursive and the recursionless tree build identical trees with the minimum number of rotations, and that every line of the libraries is executed (100% line coverage for C# and C++). They also run with ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION, which makes every tree validate itself after each change:

cd csharp && dotnet test -c Release -p:TreeValidation=true
TREE_VALIDATION=1 BUILD_TYPE=Debug SANITIZERS='address;undefined' cpp/build-and-test.sh

The trees pay nothing for being inspected. The library has no counters, logs or hooks for tests: the test trees inherit from the library trees and count rotations and size reads in the node accessors (GetSize, SetLeft, SetRight) that every tree implements anyway. LeftRotate and RightRotate stay non-virtual in C#, and in C++ the test trees shadow them through CRTP, which compiles to direct calls. The validation mode is a compile-time switch, so it costs nothing when it is off.

Benchmarks

The benchmarks compare the three trees with each other and the four lists with each other, in C# (BenchmarkDotNet) and C++ (Google Benchmark), with the same storages as the tests: time and allocated bytes of attach and detach, of search, and the memory of the storage itself. Shared runners are noisy, so they run only on request: Actions > Benchmarks > Run workflow. The run summary shows the tables below for that run; with commit-results the workflow also commits them to this README and to benchmarks/results.json. Locally:

cd csharp && dotnet run -c Release --project Platform.Collections.Methods.Benchmarks -- --filter '*' --exporters json
BENCHMARKS=1 cpp/build-and-test.sh && cpp/build/bin/Platform.Collections.Methods.Benchmarks --benchmark_out=cpp.json --benchmark_out_format=json
node .github/scripts/render-benchmarks.mjs normalize --csharp csharp/BenchmarkDotNet.Artifacts/results --cpp cpp.json
node .github/scripts/render-benchmarks.mjs readme

The C# allocated column comes from BenchmarkDotNet's MemoryDiagnoser, which also counts what the runtime allocates for itself (JIT, timers) during the measurement and divides it by the invocations; the long benchmarks run few invocations, so this noise shows as a few dozen bytes. The tests above prove that attach and detach allocate nothing in both languages.

Run the Benchmarks workflow with commit-results to update these results, which come from:

  • C#: .NET 8.0.34 (8.0.3426.52217), BenchmarkDotNet 0.14.0, Ubuntu 24.04.5 LTS (Noble Numbat), Intel Xeon 6975P-C, 4 logical cores (Benchmarks workflow run of commit 47bd4ee on 2026-11-29).
  • C++: g++ (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0, Google Benchmark v1.9.1, 4 CPUs at 3241 MHz (Benchmarks workflow run of commit 47bd4ee on 2026-11-29).

Mean is the time of one invocation, which handles all N nodes; ratio compares it with the first structure of the same table, language and parameters; allocated is the heap memory one invocation allocates. Shared runners are noisy, so compare ratios rather than absolute times, and measure on your own hardware for decisions.

Trees: attach and detach

Attaches the nodes 1..N to an empty tree in the given order, then detaches all of them (in another random order for Random).

N Order Tree C# mean C# ratio C# allocated C++ mean C++ ratio C++ allocated
1000 Ascending SizeBalancedTree 363 μs 1.00 0 B 127 μs 1.00 0 B
1000 Ascending RecursionlessSizeBalancedTree 412 μs 1.13 0 B 159 μs 1.25 0 B
1000 Ascending SizedAndThreadedAVLBalancedTree 209 μs 0.58 0 B 73.4 μs 0.58 0 B
1000 Descending SizeBalancedTree 380 μs 1.00 0 B 118 μs 1.00 0 B
1000 Descending RecursionlessSizeBalancedTree 427 μs 1.12 0 B 161 μs 1.36 0 B
1000 Descending SizedAndThreadedAVLBalancedTree 209 μs 0.55 0 B 74.9 μs 0.63 0 B
1000 Random SizeBalancedTree 441 μs 1.00 0 B 170 μs 1.00 0 B
1000 Random RecursionlessSizeBalancedTree 595 μs 1.35 1 B 248 μs 1.46 0 B
1000 Random SizedAndThreadedAVLBalancedTree 323 μs 0.73 0 B 81.3 μs 0.48 0 B
100000 Ascending SizeBalancedTree 56.6 ms 1.00 82 B 26.4 ms 1.00 0 B
100000 Ascending RecursionlessSizeBalancedTree 67.8 ms 1.20 92 B 29.1 ms 1.10 0 B
100000 Ascending SizedAndThreadedAVLBalancedTree 28.8 ms 0.51 23 B 10.5 ms 0.40 0 B
100000 Descending SizeBalancedTree 59.4 ms 1.00 82 B 27.4 ms 1.00 0 B
100000 Descending RecursionlessSizeBalancedTree 68.1 ms 1.15 92 B 30.2 ms 1.10 0 B
100000 Descending SizedAndThreadedAVLBalancedTree 28.1 ms 0.47 23 B 11.2 ms 0.41 0 B
100000 Random SizeBalancedTree 75.7 ms 1.00 105 B 43.5 ms 1.00 0 B
100000 Random RecursionlessSizeBalancedTree 104 ms 1.38 147 B 54.1 ms 1.25 0 B
100000 Random SizedAndThreadedAVLBalancedTree 49.2 ms 0.65 67 B 32.1 ms 0.74 0 B

Trees: search

Looks every one of the N nodes up (Contains) in a tree built by attaching them in the given order.

N Order Tree C# mean C# ratio C# allocated C++ mean C++ ratio C++ allocated
1000 Ascending SizeBalancedTree 29.3 μs 1.00 0 B 8.43 μs 1.00 0 B
1000 Ascending RecursionlessSizeBalancedTree 28.8 μs 0.98 0 B 8.37 μs 0.99 0 B
1000 Ascending SizedAndThreadedAVLBalancedTree 48.1 μs 1.64 0 B 10.4 μs 1.24 0 B
1000 Descending SizeBalancedTree 28.2 μs 1.00 0 B 8.06 μs 1.00 0 B
1000 Descending RecursionlessSizeBalancedTree 28.2 μs 1.00 0 B 7.97 μs 0.99 0 B
1000 Descending SizedAndThreadedAVLBalancedTree 48 μs 1.70 0 B 9.77 μs 1.21 0 B
1000 Random SizeBalancedTree 25.1 μs 1.00 0 B 10.7 μs 1.00 0 B
1000 Random RecursionlessSizeBalancedTree 25.2 μs 1.00 0 B 10.8 μs 1.01 0 B
1000 Random SizedAndThreadedAVLBalancedTree 52.1 μs 2.07 0 B 12.4 μs 1.15 0 B
100000 Ascending SizeBalancedTree 4.92 ms 1.00 6 B 4.16 ms 1.00 0 B
100000 Ascending RecursionlessSizeBalancedTree 4.79 ms 0.97 6 B 4.16 ms 1.00 0 B
100000 Ascending SizedAndThreadedAVLBalancedTree 6.88 ms 1.40 6 B 5.34 ms 1.28 0 B
100000 Descending SizeBalancedTree 4.95 ms 1.00 6 B 4.22 ms 1.00 0 B
100000 Descending RecursionlessSizeBalancedTree 4.88 ms 0.99 6 B 4.23 ms 1.00 0 B
100000 Descending SizedAndThreadedAVLBalancedTree 6.52 ms 1.32 6 B 5.34 ms 1.26 0 B
100000 Random SizeBalancedTree 10.5 ms 1.00 12 B 10.7 ms 1.00 0 B
100000 Random RecursionlessSizeBalancedTree 10.6 ms 1.01 12 B 10.8 ms 1.00 0 B
100000 Random SizedAndThreadedAVLBalancedTree 12.5 ms 1.19 12 B 11.4 ms 1.06 0 B

Lists: attach and detach

Attaches the elements 1..N to an empty list as last ones, then detaches all of them from the first one.

N List C# mean C# ratio C# allocated C++ mean C++ ratio C++ allocated
1000 AbsoluteCircularDoublyLinkedList 3.59 μs 1.00 0 B 3.12 μs 1.00 0 B
1000 AbsoluteOpenDoublyLinkedList 3.05 μs 0.85 0 B 2.5 μs 0.80 0 B
1000 RelativeCircularDoublyLinkedList 5.53 μs 1.54 0 B 3.12 μs 1.00 0 B
1000 RelativeOpenDoublyLinkedList 4.38 μs 1.22 0 B 2.49 μs 0.80 0 B
100000 AbsoluteCircularDoublyLinkedList 372 μs 1.00 0 B 320 μs 1.00 0 B
100000 AbsoluteOpenDoublyLinkedList 308 μs 0.83 0 B 253 μs 0.79 0 B
100000 RelativeCircularDoublyLinkedList 548 μs 1.47 1 B 319 μs 1.00 0 B
100000 RelativeOpenDoublyLinkedList 451 μs 1.21 0 B 252 μs 0.79 0 B

Memory: storage of N nodes

Creates an empty tree or list that can hold N nodes besides the zero node; the allocated bytes are the memory it uses, the bytes per node divide them by N + 1.

N Structure C# allocated C# bytes per node C++ allocated C++ bytes per node
1000 SizeBalancedTree 11.83 KB 12.1 11.74 KB 12.0
1000 RecursionlessSizeBalancedTree 12.45 KB 12.7 11.79 KB 12.1
1000 SizedAndThreadedAVLBalancedTree 15.95 KB 16.3 15.65 KB 16.0
1000 AbsoluteCircularDoublyLinkedList 7.92 KB 8.1 7.83 KB 8.0
1000 AbsoluteOpenDoublyLinkedList 7.92 KB 8.1 7.83 KB 8.0
1000 RelativeCircularDoublyLinkedList 8.02 KB 8.2 7.83 KB 8.0
1000 RelativeOpenDoublyLinkedList 8.02 KB 8.2 7.83 KB 8.0
100000 SizeBalancedTree 1.14 MB 12.0 1.14 MB 12.0
100000 RecursionlessSizeBalancedTree 1.15 MB 12.0 1.14 MB 12.0
100000 SizedAndThreadedAVLBalancedTree 1.53 MB 16.0 1.53 MB 16.0
100000 AbsoluteCircularDoublyLinkedList 781.51 KB 8.0 781.27 KB 8.0
100000 AbsoluteOpenDoublyLinkedList 781.52 KB 8.0 781.27 KB 8.0
100000 RelativeCircularDoublyLinkedList 781.63 KB 8.0 781.27 KB 8.0
100000 RelativeOpenDoublyLinkedList 781.62 KB 8.0 781.27 KB 8.0

Releasing

A version bump in csharp/Platform.Collections.Methods/Platform.Collections.Methods.csproj or cpp/Platform.Collections.Methods/Platform.Collections.Methods.TemplateLibrary.nuspec merged to main publishes the package to nuget.org and creates a GitHub release, after the tests passed. Publishing uses trusted publishing first:

  1. On nuget.org, sign in as an individual who can publish the packages. Create policies for linksplatform/Collections.Methods. Select the package owner and grant Push only new versions for Platform.Collections.Methods in csharp.yml and Platform.Collections.Methods.TemplateLibrary in deploy-cpp.yml. Leave the optional environment field blank.
  2. Set the GitHub repository variable NUGET_USER to the NuGet username that created both policies. For organization-owned packages, use the individual policy creator's username.

To enable fallback, set NUGET_TOKEN to a NuGet API key with push scope for the packages. The workflows use it when trusted publishing is unconfigured or NuGet cannot issue or authorize its key. Pull requests verify the packed package with an isolated consumer. The release verifies the public listing, and the C# release also builds that consumer from nuget.org. If both credentials fail on main, correct the policy or replace the key in the repository settings. Then rerun the failed jobs or dispatch the workflow with Retry publication from main selected. Dependabot pull requests are merged automatically with the DEPENDABOT_AUTO_MERGE_TOKEN secret once "Allow auto-merge" is enabled.

Releases

Packages

Used by

Contributors

Languages