Towel .NET Utility Library

repository·main·Indexed 21 days ago

https://github.com/zacharypatten/towel

A .NET utility library providing a wide range of helper tools for C# development, including advanced data structures (AVL, Red-Black, B-Tree, Omnitree, SkipList), algorithms for sorting and graph search, generic mathematics, and type-safe scientific measurement types. It also features extensions for System.Random, reflection-based XML documentation access, and a command-line parser.

Tokens
8K
Snippets
11
Records
27
Agent score
73%

What's inside Towel

  1. Overview of Towel

    main

    Towel is a .NET library designed to simplify development by providing a collection of utility tools including data structures, algorithms, mathematics, metadata, extensions, and console utilities.

    Important Note on Versioning: The project prioritizes modern coding practices and targeting the latest non-preview versions of .NET over maintaining backwards compatibility. As a result, Semantic Versioning (SemVer) is not strictly followed, and breaking changes may occur as the library evolves.

  2. Explore available Towel benchmarks

    main

    Towel provides various benchmarks to compare performance across different algorithms, data structures, and patterns. Available benchmark categories include:

    • Algorithms & Data Structures: Sorting Algorithms, Data Structures, Map vs Dictionary (Add), Map vs Dictionary (Look Up), Span vs Array Sorting, Heap Generics Vs Delegates.
    • Randomization: Weighted Random, Random With Exclusions.
    • Lazy Patterns: Lazy Initialization, Lazy Caching, Lazy Construction.
    • Utilities: decimal To English Words, Permute.
  3. Data Structures in Towel

    main

    Towel includes several advanced data structures for specialized use cases.

    Trees

    • Heap (IHeap<T>): A binary tree sorted vertically. Uses "sifting up" and "sifting down" algorithms. Can be implemented via HeapArray.New<T>().
    • AVL Tree (IAvltree<T>): A self-balancing binary tree that uses rotations (right, left, double right, double left) to maintain balance. Use AvlTreeLinked.New<T>().
    • Red-Black Tree (IRedBlackTree<T>): A self-balancing binary tree that uses different algorithms than AVL to maintain balance. Use RedBlackTreeLinked.New<T>().
    • Omnitree (IOmnitreePoints<T, ...> or IOmnitreeBounds<T, ...>): A Spatial Partitioning Tree (SPT) that works in arbitrary dimensions (e.g., Octrees for 3D, Quadtrees for 2D). It divides spaces into sub-spaces. The depth is bounded by $\Omega(\ln(\text{count}))$.
    • B-Tree (BTree<T>): A self-balancing tree that allows nodes to have more than two children, supporting logarithmic time for search, insertion, and deletion. This implementation uses a Pre-emptive mode for Add/Remove, requiring the Maximum Degree to be an even number. Use new BTree<int>(degree).
    • Trie (ITrie<T> or ITrie<T, D>): A tree that shares partial keys to reduce memory usage. Use TrieLinkedHashLinked.New<T>().

    Other Structures

    • Graph (IGraph<T>): Models nodes and edges. GraphSetOmnitree stores nodes in a hashed set and edges in a 2D omnitree (quadtree). Use GraphSetOmnitree.New<int>().
    • SkipList (SkipList<T, ...>): A probabilistic data structure that allows $O(\log n)$ average complexity for basic operations using multiple layers. Use SkipList.New<T>(levels).
    • TreeMap (ITree<T>): General tree mapping via TreeMap.New<T>().
    // Heap
    IHeap<T> heap = HeapArray.New<T>();
    
    // AVL Tree
    IAvltree<T> avlTree = AvlTreeLinked.New<T>();
    
    // Red Black Tree
    IRedBlackTree<T> redBlackTree = RedBlackTreeLinked.New<T>();
    
    // Omnitree
    IOmnitreePoints<T, A1, A2, A3...> omnitreePoints = 
        new OmnitreePointsLinked<T, A1, A2, A3...>( 
            (T value, out A1 a1, out A2 a2, out A3 a3...) => { ... });
    
    // B-Tree
    BTree<int> tree = new BTree<int>(4); 
    
    // Graph
    IGraph<int> graph = GraphSetOmnitree.New<int>();
    
    // SkipList
    SkipList<int, SFunc<int, int, CompareResult>>? list = SkipList.New<int>(5);
    
    // Trie
    ITrie<T> trie = TrieLinkedHashLinked.New<T>();
  4. Scientific Measurement Types

    main

    Towel provides type-safe measurement types (e.g., Length<T>, Mass<T>, Speed<T>) to prevent mathematically invalid operations and automate unit conversions.

    Automatic Unit Conversion

    When performing operations on measurements of the same type, conversions are handled automatically based on the units used.

    Angle<double> angle1 = (90d, Degrees);
    Angle<double> angle2 = (.5d, Turns);
    Angle<double> result1 = angle1 + angle2; // Result is 270°

    Type Safety

    You cannot perform operations between incompatible measurement types (e.g., adding Length<T> to Angle<T>) at compile time.

    Manual Conversion and Parsing

    • Index Operator: double radians = angle1[Radians];
    • Static Convert: Angle<double>.Convert(7d, Radians, Degrees);
    • Parsing: Speed<float>.TryParse("20.5 Meters / Seconds", out var speed);

    Integration with Vectors

    Measurements can be used as the generic type within Vector<T>.

    Vector<Speed<float>> velocity1 = new Vector<Speed<float>>((1f, Meters / Seconds), ...);
    Vector<Speed<float>> velocity2 = new Vector<Speed<float>>((1f, Centimeters / Seconds), ...);
    Vector<Speed<float>> velocity3 = velocity1 + velocity2; // Automatic conversion applied
    // Automatic Unit Conversion
    Angle<double> angle1 = (90d, Degrees);
    Angle<double> angle2 = (.5d, Turns);
    Angle<double> result1 = angle1 + angle2; // 270° 
    
    // Type Safeness
    Length<double> length1 = (2d, Yards);
    // object result2 = angle1 + length1; // WILL NOT COMPILE!!!
    
    // Manual Unit Conversions
    double angle1_inRadians = angle1[Radians];
    double angle3 = Angle<double>.Convert(7d, Radians, Degrees);
    
    // Measurement Parsing
    Speed<float>.TryParse("20.5 Meters / Seconds", out Speed<float> parsedSpeed);
  5. Repository structure overview

    main

    Understanding the repository layout helps in locating source code, examples, and tools:

    • Sources/Towel: The root folder containing all source code for the Towel NuGet package.
    • Examples: Root folder for all example projects.
    • Tools/: Contains support projects not included in the NuGet packages:
      • Tools/docfx_project: Documentation project files.
      • Tools/Towel_Benchmarking: Benchmarking projects.
      • Tools/Towel_Generating: Code generation projects.
      • Tools/Towel_Testing: Unit test projects.
    • .github/: Contains GitHub repository configurations, including issue templates and GitHub Actions workflows.
    • .vscode/: Configuration files for Visual Studio Code users.
  6. Use Towel in your .NET projects

    main

    You can integrate Towel into your own .NET applications via NuGet.

    Requirements

    • Target Framework: Your project must target the same or a newer version of .NET than Towel.

    Installation

    Install the Towel NuGet package. Detailed instructions for referencing the package can be found on the NuGet gallery page.