.NET console mathematics Library extensions Algorithm Data structures Framework C#
Need help with Towel?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.
ZacharyPatten

Description

Throw in the towel.

252 Stars 21 Forks MIT License 841 Commits 23 Opened issues

Services available

Need anything else?

Towel

A .NET library intended to add core functionality and make advanced topics as clean and simple as possible: data structures, algorithms, mathematics, metadata, extensions, console, and more. :)

"It's a tough galaxy. If you want to survive, you've gotta know... where your towel is." - Ford Prefect

github repo Language C# .NET target Nuget Package Docfx Documentation Towel Continuous Integration Discord License

Note This project has a goal of keeping as up-to-date on modern coding practices rather than maintaining backwards compatibility.

Getting Started

:page_facing_up: Run The Included Examples (Click To Expand)

> Towel has Examples included in this repository. > > Download this repository and unzip the contents. > > There are no custom build processes. Towel should build with any standard .NET build process, but one of the following is recommended: > > ### Visual Studio 2019 > > Install Visual Studio 2019 if not already installed. > > Open the :page_facing_up: Towel.sln file in Visual Studio. > > > Note This is optional, but here are some recommended settings you change in Visual Studio. > > ### Visual Studio Code > > Install the .NET Core SDK if not already installed. > > Install Visual Studio Code if not already installed. > > Open the :file_folder: root folder of the repository in Visual Studio Code. > > > Note The following files are included in the repository: > > - .vscode/extensions.jsonrecommends Vistual Studio Code extension dependencies > > - .vscode/launch.jsonincludes the configurations for debugging the examples > > - .vscode/settings.jsonautomatically applies settings to the workspace > > - .vscode/tasks.jsonincludes the commands to build the projects > > > Note Visual Studio Code Extensions (will be prompted to install these when you open the folder): > > - ms-vscode.csharp C# support > > - aisoftware.tt-processor (optional) T4 Template support > > - zbecknell.t4-support (optional) T4 Template syntax highlighting > > - formulahendry.dotnet-test-explorer (optional) MSTest unit testing support

:page_facing_up: Use Towel In Your .NET Projects (Click To Expand)

> Towel has a nuget package:
> Nuget Package > > Your project must target the same or newer version of .NET as Towel. See this documentation on how to check the current target of your project. Towel targets the following version of .NET:
> .NET target > > You can install the Towel nuget package with the dotnet add package Towel --version XXXXX command, or you can > manually add a reference to it in your .csproj files <packagereference include="Towel" version="XXXXX"></packagereference> > (where XXXXX is the version to install). > > See the releases page for change logs.

:page_facing_up: View Documentation (Click To Expand)

> Towel has an API documentation reference that is generated with docfx. You can view the documentation here: > https://zacharypatten.github.io/Towel/api/index.html > Docfx Documentation > > Here is Towel's benchmarking documentation: > https://zacharypatten.github.io/Towel/articles/benchmarks.html > > Here are some other documentation references: > - MSDN Accessing XML Documentation Via Reflection > - Beginner's Guide To Console Input In C# > - Generating Unique Random Data > - Omnitree > - C# Generic Math

:page_facing_up: Get Involved (Click To Expand)

> The easiest way to support Towel is to star the github repository. > > If you notice anything in Towel that may be improved, please create a new issue. > > You can chat with the developer(s) on discord: > Discord > > Share your work. If you use Towel in one of your projects we want to hear about it. > > If you want to contribute to Towel: > 1. Fork this repository > 2. Make some changes > 3. Open a pull request

:page_facing_up: Repository Structure (Click To Expand)

> Here is a map of this repository's file structure: > > - :file_folder: .github content regarding the GitHub repoistory. > - :file_folder: ISSUE_TEMPLATE templates for issue submissions to the GitHub repository > - :file_folder: Resources resources such as image files > - :file_folder: workflows GitHub Actions workflows > - :page_facing_up: Towel Continuous Integration.yml workflow for checking that code compiles and unit tests pass > - :page_facing_up: Towel Deployment.yml workflow to manage releases and deploy nuget packages > - :page_facing_up: Towel Docfx.yml workflow that runs docfx to generate the GitHub pages in the gh-pages branch > - pull_request_template.md template for when pull requests are created > - :file_folder: .vscode confirguration files for if the code is opened in Visual Studio Code > - :file_folder: Examples root folder for all the example projects > - :file_folder: Sources root folder for the source code of released nuget packages > - :file_folder: Towel the root folder for all source code in the Towel nuget package > - :file_folder: Tools root folder for all support projects > - :file_folder: docfx_project root folder for docfx project (used in the Towel Docfx.yml workflow) > - :file_folder: articles root folder for all articless of the docfx generated GitHub pages website > - :page_facing_up: docfx.json configuration file that controls docfx > - :page_facing_up: index.md home page of the docfx generated GitHub pages website > - :page_facing_up: toc.yml primary navigation for the docfx generated GitHub pages website > - :file_folder: Towel_Benchmarking project with all the benchmarking for the Towel project > - :file_folder: Towel_Documenting not currently used; custom documentation generation for the Towel Project > - :file_folder: Towel_Generating not currently used; custom source code generation for the Towel Project > - :file_folder: Towel_Testing project with all unit tests for the Towel project (runs in the Towel Continuous Integration.yml workflow)

Generic Mathematics & Logic

:page_facing_up: How It Works (Click To Expand)

> cs &gt; public static T Addition<t>(T a, T b) &gt; { &gt; return AdditionImplementation<t>.Function(a, b); &gt; } &gt; &gt; internal static class AdditionImplementation<t> &gt; { &gt; internal static Func<t t> Function = (T a, T b) =&gt; &gt; { &gt; ParameterExpression A = Expression.Parameter(typeof(T)); &gt; ParameterExpression B = Expression.Parameter(typeof(T)); &gt; Expression BODY = Expression.Add(A, B); &gt; Function = Expression.Lambda<func t>&gt;(BODY, A, B).Compile(); &gt; return Function(a, b); &gt; }; &gt; } &gt; > > You can break type safe-ness using generic types and runtime compilation, and you can store the runtime compilation in a delegate so the only overhead is the invocation of the delegate.

// Logic Fundamentals
bool Equate(T a , T b);
bool LessThan(T a, T b);
bool GreaterThan(T a, T b);
CompareResult Compare(T a, T b);

// Mathematics Fundamentals T Negation(T a); T Addition(T a, T b); T Subtraction(T a, T b); T Multiplication(T a, T b); T Division(T a, T b); T Remainder(T a, T b);

// More Logic bool IsPrime(T a); bool IsEven(T a); bool IsOdd(T a); T Minimum(T a, T b); T Maximum(T a, T b); T Clamp(T value, T floor, T ceiling); T AbsoluteValue(T a); bool EqualityLeniency(T a, T b, T leniency);

// More Numerics void FactorPrimes(T a, ...); T Factorial(T a); T LinearInterpolation(T x, T x0, T x1, T y0, T y1); T LeastCommonMultiple(T a, T b, params T[] c); T GreatestCommonFactor(T a, T b, params T[] c); LinearRegression2D(..., out T slope, out T y_intercept);

// Statistics T Mean(T a, params T[] b); T Median(params T[] values); Heap> Mode(T a, params T[] b); void Range(out T minimum, out T maximum, ...); T[] Quantiles(int quantiles, ...); T GeometricMean(...); T Variance(...); T StandardDeviation(...); T MeanDeviation(...);

// Vectors Vector V1 = new Vector(params T[] vector); Vector V2 = new Vector(params T[] vector); Vector V3; T scalar; V3 = -V1; // Negate V3 = V1 + V2; // Add V3 = V1 - V2; // Subtract V3 = V1 * scalar; // Multiply V3 = V1 / scalar; // Divide scalar = V1.DotProduct(V2); // Dot Product V3 = V1.CrossProduct(V2); // Cross Product V1.Magnitude; // Magnitude V3 = V1.Normalize(); // Normalize bool equal = V1 == V2; // Equal

// Matrices Matrix M1 = new Matrix(int rows, int columns); Matrix M2 = new Matrix(int rows, int columns); Matrix M3; Vector V2 = new Vector(params T[] vector); Vector V3; T scalar; M3 = -M1; // Negate M3 = M1 + M2; // Add M3 = M1 - M2; // Subtract M3 = M1 * M2; // Multiply V3 = M1 * V2; // Multiply (vector) M3 = M1 * scalar; // Multiply (scalar) M3 = M1 / scalar; // Divide M3 = M1 ^ 3; // Power scalar = M1.Determinent(); // Determinent M3 = M1.Minor(int row, int column); // Minor M3 = M1.Echelon(); // Echelon Form (REF) M3 = M1.ReducedEchelon(); // Reduced Echelon Form (RREF) M3 = M1.Inverse(); // Inverse M1.DecomposeLowerUpper(ref M2, ref M3); // Lower Upper Decomposition bool equal = M1 == M2; // Equal

Symbolic Mathematics

// Parsing From Linq Expression
Expression> exp1 = (x) => 2 * (x / 7);
Symbolics.Expression symExp1 = Symbolics.Parse(exp1);

// Parsing From String Symbolics.Expression symExp2 = Symbolics.Parse("2 * ([x] / 7)");

// Mathematical Simplification Symbolics.Expression simplified = symExp1.Simplify();

// Variable Substitution symExp1.Substitute("x", 5);

Measurement Mathematics

:page_facing_up: Supported Measurements (Click To Expand)

> Here are the currently supported measurement types: > > cs &gt; // Acceleration: Length/Time/Time &gt; // AngularAcceleration: Angle/Time/Time &gt; // Angle: Angle &gt; // AngularSpeed: Angle/Time &gt; // Area: Length*Length &gt; // AreaDensity: Mass/Length/Length &gt; // Density: Mass/Length/Length/Length &gt; // ElectricCharge: ElectricCharge &gt; // ElectricCurrent: ElectricCharge/Time &gt; // Energy: Mass*Length*Length/Time/Time &gt; // Force: Mass*Length/Time/Time &gt; // Length: Length &gt; // LinearDensity: Mass/Length &gt; // LinearMass: Mass*Length &gt; // LinearMassFlow: Mass*Length/Time &gt; // Mass: Mass &gt; // MassRate: Mass/Time &gt; // Power: Mass*Length*Length/Time/Time/Time &gt; // Pressure: Mass/Length/Time/Time &gt; // Speed: Length/Time &gt; // Tempurature: Tempurature &gt; // Time: Time &gt; // TimeArea: Time*Time &gt; // Volume: Length*Length*Length &gt; // VolumeRate: Length*Length*Length/Time &gt; > > The measurement types are generated in the Towel/Measurements/MeasurementTypes.tt T4 text template file. The unit (enum) definitions are in the Towel/Measurements/MeasurementUnitDefinitions.cs file. Both measurment types and unit definitions can be easily added. If you think a measurement type or unit type should be added, please submit an enhancement issue.

// Towel has measurement types to help write scientific code: Acceleration, Angle, Area, 
// Density, Length, Mass, Speed, Time, Volume, etc.

// Automatic Unit Conversion // When you perform mathematical operations on measurements, any necessary unit conversions will // be automatically performed by the relative measurement type (in this case "Angle"). Angle angle1 = (90d, Degrees); Angle angle2 = (.5d, Turns); Angle result1 = angle1 + angle2; // 270°

// Type Safeness // The type safe-ness of the measurement types prevents the miss-use of the measurements. You cannot // add "Length" to "Angle" because that is mathematically invalid (no operator exists). Length length1 = (2d, Yards); object result2 = angle1 + length1; // WILL NOT COMPILE!!!

// Simplify The Syntax Even Further // You can use alias to remove the generic type if you want to simplify the syntax even further. using Speedf = Towel.Measurements.Speed; // at top of file Speedf speed1 = (5, Meters / Seconds);

// Vector + Measurements // You can use the measurement types inside Towel Vectors. Vector> velocity1 = new Vector>( (1f, Meters / Seconds), (2f, Meters / Seconds), (3f, Meters / Seconds)); Vector velocity2 = new Vector( (1f, Centimeters / Seconds), (2f, Centimeters / Seconds), (3f, Centimeters / Seconds)); Vector> velocity3 = velocity1 + velocity2;

// Manual Unit Conversions // 1. Index Operator On Measurement Type double angle1_inRadians = angle1[Radians]; float speed1_inMilesPerHour = speed1[Miles / Hours]; // 2. Static Conversion Methods double angle3 = Angle.Convert(7d, Radians, // from Degrees); // to double speed2 = Speed.Convert(8d, Meters / Seconds, // from Miles / Hours); // to double force1 = Force.Convert(9d, Kilograms * Meters / Seconds / Seconds, // from Grams * Miles / Hours / Hours); // to double angle4 = Measurement.Convert(10d, Radians, // from Degrees); // to // Note: The unit conversion on the Measurement class // is still compile-time-safe.

// Measurement Parsing Speed.TryParse("20.5 Meters / Seconds", out Speed parsedSpeed); Force.TryParse(".1234 Kilograms * Meters / Seconds / Seconds", out Force parsedForce);

Data Structures

:page_facing_up: Heap (Click To Expand)

> cs &gt; // A heap is a binary tree that is sorted vertically using comparison methods. This is different &gt; // from AVL Trees or Red-Black Trees that keep their contents stored horizontally. The rule &gt; // of a heap is that no parent can be less than either of its children. A Heap using "sifting up" &gt; // and "sifting down" algorithms to move values vertically through the tree to keep items sorted. &gt; &gt; IHeap<t> heapArray = new HeapArray<t>(); &gt; &gt; // Visualization: &gt; // &gt; // Binary Tree &gt; // &gt; // -7 &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // -4 1 &gt; // / \ / \ &gt; // / \ / \ &gt; // / \ / \ &gt; // -1 3 6 4 &gt; // / \ / \ / \ / \ &gt; // 30 10 17 51 45 22 19 7 &gt; // &gt; // Flattened into an Array &gt; // &gt; // Root = 1 &gt; // Left Child = 2 * Index &gt; // Right Child = 2* Index + 1 &gt; // __________________________________________________________________________ &gt; // |0 |-7 |-4 |1 |-1 |3 |6 |4 |30 |10 |17 |51 |45 |22 |19 |7 |0 |0 |0 ... &gt; // ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ &gt; // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 &gt;

:page_facing_up: AVL Tree (Click To Expand)

> cs &gt; // An AVL tree is a binary tree that is sorted using comparison methods and automatically balances &gt; // itself by tracking the heights of nodes and performing one of four specific algorithms: rotate &gt; // right, rotate left, double rotate right, or double rotate left. Any parent in an AVL Tree must &gt; // be greater than its left child but less than its right child (if the children exist). An AVL &gt; // tree is sorted in the same manor as a Red-Black Tree, but uses different algorithms to maintain &gt; // the balance of the tree. &gt; &gt; IAvlTree<int> avlTree = new AvlTreeLinked<int>(); &gt; &gt; // Visualization: &gt; // &gt; // Binary Tree &gt; // &gt; // Depth 0 ------------------&gt; 7 &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // Depth 1 ---------&gt; 1 22 &gt; // / \ / \ &gt; // / \ / \ &gt; // / \ / \ &gt; // Depth 2 ----&gt; -4 4 17 45 &gt; // / \ / \ / \ / \ &gt; // Depth 3 ---&gt; -7 -1 3 6 10 19 30 51 &gt; // &gt; // Flattened into an Array &gt; // &gt; // Root = 1 &gt; // Left Child = 2 * Index &gt; // Right Child = 2* Index + 1 &gt; // __________________________________________________________________________ &gt; // |0 |7 |1 |22 |-4 |4 |17 |45 |-7 |-1 |3 |6 |10 |19 |30 |51 |0 |0 |0 ... &gt; // ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ &gt; // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 &gt;

:page_facing_up: Red Black Tree (Click To Expand)

> cs &gt; // A Red-Black treeis a binary tree that is sorted using comparison methods and automatically &gt; // balances itself. Any parent in an Red-Black Tree must be greater than its left child but less &gt; // than its right child (if the children exist). A Red-Black tree is sorted in the same manor as &gt; // an AVL Tree, but uses different algorithms to maintain the balance of the tree. &gt; &gt; IRedBlackTree<int> redBlackTree = new RedBlackTreeLinked<int>(); &gt; &gt; // Visualization: &gt; // &gt; // Binary Tree &gt; // &gt; // Color Black ----------------&gt; 7 &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // / \ &gt; // Color Red ---------&gt; 1 22 &gt; // / \ / \ &gt; // / \ / \ &gt; // / \ / \ &gt; // Color Black ---&gt; -4 4 17 45 &gt; // / \ / \ / \ / \ &gt; // Color Red ---&gt; -7 -1 3 6 10 19 30 51 &gt; // &gt; // Flattened into an Array &gt; // &gt; // Root = 1 &gt; // Left Child = 2 * Index &gt; // Right Child = 2* Index + 1 &gt; // __________________________________________________________________________ &gt; // |0 |7 |1 |22 |-4 |4 |17 |45 |-7 |-1 |3 |6 |10 |19 |30 |51 |0 |0 |0 ... &gt; // ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ &gt; // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 &gt;

:page_facing_up: Omnitree (Click To Expand)

> cs &gt; // An Omnitree is a Spacial Partitioning Tree (SPT) that works on an arbitrary number of dimensions. &gt; // It stores items sorted along multiple dimensions by dividing spaces into sub-spaces. A 3D &gt; // version of an SPT is often called an "Octree" and a 2D version of an SPT is often called a &gt; // "Quadtree." There are two versions of the Omnitree: Points and Bounds. The Points version stores &gt; // vectors while the Bounds version stores spaces with a minimum and maximum vector. &gt; &gt; IOmnitreePoints<t a1 a2 a3...> omnitreePoints = &gt; new OmnitreePointsLinked<t a1 a2 a3...>( &gt; (T value, out A1 a1, out A2 a2, out A3 a3...) =&gt; { ... }); &gt; &gt; IOmnitreeBounds<t a1 a2 a3...> omnitreeBounds = &gt; new OmnitreeBoundsLinked<t a1 a2 a3...>( &gt; (T value, &gt; out A1 min1, out A1 max1, &gt; out A2 min2, out A2 max2, &gt; out A3 min3, out A3 max3...) =&gt; { ... }); &gt; &gt; // The maximum number of children any node can have is 2 ^ N where N is the number &gt; // of dimensions of the tree. &gt; // &gt; // ------------------------------- &gt; // | Dimensions | Max # Children | &gt; // |============|================| &gt; // | 1 | 2 ^ 1 = 2 | &gt; // | 2 | 2 ^ 2 = 4 | &gt; // | 3 | 2 ^ 3 = 8 | &gt; // | 4 | 2 ^ 4 = 16 | &gt; // | ... | ... | &gt; // ------------------------------- &gt; // &gt; // Visualizations &gt; // &gt; // 1 Dimensional: &gt; // &gt; // -1D |-----------|-----------| +1D Children Indexes: &gt; // -1D: 0 &gt; // +1D: 1 &gt; // &gt; // 2 Dimensional: &gt; // _____________________ &gt; // | | | +2D &gt; // | | | ^ &gt; // | 2 | 3 | | Children Indexes: &gt; // | | | | -2D -1D: 0 &gt; // |----------|----------| | -2D +1D: 1 &gt; // | | | | +2D -1D: 2 &gt; // | | | | +2D +1D: 3 &gt; // | 0 | 1 | | &gt; // | | | v &gt; // |__________|__________| -2D &gt; // &gt; // -1D +1D &gt; // &gt; // 3 Dimensional: &gt; // &gt; // +3D _____________________ &gt; // 7 / / /| &gt; // / / 6 / 7 / | &gt; // / /---------/----------/ | Children Indexes: &gt; // / / 2 / 3 /| | -3D -2D -1D: 0 &gt; // L /_________/__________/ | | -3D -2D +1D: 1 &gt; // -3D | | | | /| +2D -3D +2D -1D: 2 &gt; // | | | |/ | ^ -3D +2D +1D: 3 &gt; // | 2 | 3 | / | | +3D -2D -1D: 4 &gt; // | | |/| | // |----------|----------| | | | +3D +2D -1D: 6 &gt; // | | | | / | +3D +2D +1D: 7 &gt; // | | | | / | &gt; // | 0 | 1 | |/ | &gt; // | | | / v &gt; // |__________|__________|/ -2D &gt; // &gt; // ^ &gt; // | &gt; // 4 (behind 0) &gt; // &gt; // -1D +1D &gt; // &gt; // 4 Dimensional: &gt; // &gt; // +1D +2D +3D +4D Children Indexes: &gt; // ^ ^ ^ ^ &gt; // | | | | -4D -3D -2D -1D: 0 +4D -3D -2D -1D: 8 &gt; // | | | | -4D -3D -2D +1D: 1 +4D -3D -2D +1D: 9 &gt; // | | | | -4D -3D +2D -1D: 2 +4D -3D +2D -1D: 10 &gt; // | | | | -4D -3D +2D +1D: 3 +4D -3D +2D +1D: 11 &gt; // | | | | -4D +3D -2D -1D: 4 +4D +3D -2D -1D: 12 &gt; // --- --- --- --- -4D +3D -2D +1D: 5 +4D +3D -2D +1D: 13 &gt; // | | | | -4D +3D +2D -1D: 6 +4D +3D +2D -1D: 14 &gt; // | | | | -4D +3D +2D +1D: 7 +4D +3D +2D +1D: 15 &gt; // | | | | &gt; // | | | | &gt; // | | | | &gt; // v v v v &gt; // -1D -2D -3D -4D &gt; // &gt; // With a value that is in the (+1D, -2D, -3D, +4D)[Index 9] child: &gt; // &gt; // +1D +2D +3D +4D &gt; // ^ ^ ^ ^ &gt; // | | | | &gt; // | | | | &gt; // O--- | | ---O &gt; // | \ | | / | &gt; // | \ | | / | &gt; // --- \ --- --- / --- &gt; // | \ | | / | &gt; // | \ | | / | &gt; // | ---O-----------O--- | &gt; // | | | | &gt; // | | | | &gt; // v v v v &gt; // -1D -2D -3D -4D &gt; &gt; // By default, the omnitree will sort items along each axis and use the median algorithm to determine &gt; // the point of divisions. However, you can override the subdivision algorithm. For numerical values, &gt; // the mean algorithm can be used (and is much faster than median). If you know the data set will be &gt; // relatively evenly distributed within a sub-space, you can even set the subdivision algorithm to &gt; // calculate the subdivision from parent spaces rather than looking at the current contents of the &gt; // space. Note: In a future enhancement I will automatically detect if the mean algorithm is possible &gt; // for a given type, and then the default will depend on the type in use. &gt; &gt; // The depth of the omnitree is bounded by "ln(count)" the natural log of the current count. When adding &gt; // and item to the tree, if the number of items in the respective child is greater than ln(count) and &gt; // the depth bounding has not been reached, then the child will be subdivided. The goal is to achieve &gt; // Ω(ln(count)) runtime complexity when looking up values. &gt;

:page_facing_up: Tree (Click To Expand)

> cs &gt; Tree<t> treeMap = new TreeMap<t>(...); &gt;

:page_facing_up: Graph (Click To Expand)

> cs &gt; // A graph is a data structure that contains nodes and edges. They are useful &gt; // when you need to model real world scenarios. They also are generally used &gt; // for particular algorithms such as path finding. The GraphSetOmnitree is a &gt; // graph that stores nodes in a hashed set and the edges in a 2D omnitree (aka &gt; // quadtree). &gt; &gt; IGraph<int> graph = new GraphSetOmnitree<int>() &gt; { &gt; // add nodes &gt; 0, &gt; 1, &gt; 2, &gt; 3, &gt; // add edges &gt; { 0, 1 }, &gt; { 1, 2 }, &gt; { 2, 3 }, &gt; { 0, 3 }, &gt; // visualization &gt; // &gt; // 0 --------&gt; 1 &gt; // | | &gt; // | | &gt; // | | &gt; // v v &gt; // 3 }; &gt;

:page_facing_up: Trie (Click To Expand)

> cs &gt; // A trie is a tree that stores values in a way that partial keys may be shared &gt; // amongst values to reduce redundant memory usage. They are generally used with &gt; // large data sets such as storing all the words in the English language. For &gt; // example, the words "farm" and "fart" both have the letters "far" in common. &gt; // A trie takes advantage of that and only stores the necessary letters for &gt; // those words ['f'-&gt;'a'-&gt;'r'-&gt;('t'||'m')]. A trie is not limited to string &gt; // values though. Any key type that can be broken into pieces (and shared), &gt; // could be used in a trie. &gt; // &gt; // There are two versions. One that only stores the values of the trie (ITrie<t>) &gt; // and one that stores the values of the trie plus an additional generic value &gt; // on the leaves (ITrie<t d>). &gt; &gt; ITrie<t> trie = new TrieLinkedHashLinked<t>(); &gt; &gt; ITrie<t d> trieWithAdditionalData = new TrieLinkedHashLinked<t d>(); &gt;

Algorithms

// Note: supports System.Span and any (non ref struct) int-indexed type
IsPalindrome<...>(...);

// Note: supports System.ReadOnlySpan IsInterleavedRecursive<...>(...); IsInterleavedIterative<...>(...);

// Note: supports System.Span and any (non ref struct) int-indexed type SortShuffle(...); SortBubble(...); SortSelection(...); SortInsertion(...); SortQuick(...); SortMerge(...); SortHeap(...); SortOddEven(...); SortCocktail(...); SortComb(...); SortGnome(...); SortShell(...); SortBogo(...); SortSlow(...);

// Note: supports System.ReadOnlySpan and any (non ref struct) int-indexed type SearchBinary(...);

// Note: supports System.ReadOnlySpan and any (non ref struct) int-indexed type int HammingDistanceIterative<...>(...); int LevenshteinDistanceRecursive<...>(...); int LevenshteinDistanceIterative<...>(...);

// Permutations of sequences // Note: supports System.Span and any (non ref struct) int-indexed type void PermuteRecursive<...>(...); void PermuteIterative<...>(...);

// Path Finding (Graph Search) // Note: overloads for A*, Dijkstra, and Breadth-First-Search algorithms SearchGraph<...>(...); </...></...></...></...></...></...></...></...></...>

Note Benchmarks are included for the sorting algorithms.

Extensions

// System.Random extensions to generate more random types
// Note: there are overloads to specify possible ranges
string NextString(this Random random, int length);
char NextChar(this Random random);
decimal NextDecimal(this Random random);
DateTime DateTime(this Random random);
TimeSpan TimeSpan(this Random random);
long NextLong(this Random random);
IEnumerable NextUnique(this Random random, int count, int minValue, int maxValue); // unique values
T Next(this Random random, IEnumerable pool); // weighted values
void Shuffle(this Random random, T[] array); // randomize arrays

// Type conversion to string definition as appears in C# source code // Note: useful for runtime compilation from strings string ConvertToCSharpSourceDefinition(this Type type); // Example: typeof(List) -> "System.Collections.Generic.List"

string ToEnglishWords(this decimal @decimal); // Example: 12 -> "Twelve"

// Reflection Extensions To Access XML Documentation string GetDocumentation(this Type type); string GetDocumentation(this FieldInfo fieldInfo); string GetDocumentation(this PropertyInfo propertyInfo); string GetDocumentation(this EventInfo eventInfo); string GetDocumentation(this ConstructorInfo constructorInfo); string GetDocumentation(this MethodInfo methodInfo); string GetDocumentation(this MemberInfo memberInfo); string GetDocumentation(this ParameterInfo parameterInfo);

Console Helpers

// Just some helper methods for console applications...

// command line argument parser/handler CommandLine.HandleArguments();

// wait for keypress to continue an intercept input ConsoleHelper.PromptPressToContinue(...); // generic method for retrieving validated console input ConsoleHelper.GetInput(...); // animated ellipsis character to show processing ConsoleHelper.AnimatedEllipsis(...); // render progress bar in console ConsoleHelper.ProgressBar(...); // Console.ReadLine() with hidden input characters ConsoleHelper.HiddenReadLine(); // easily manage int-based console menus ConsoleHelper.IntMenu(...); // preventing console input ConsoleHelper.FlushInputBuffer();

TagAttribute

// With TagAttribute's you can make value-based attributes so
// you don't always have to make your own custom attribute types.
// Just "tag" a code member with constant values.

using System; using Towel;

static class Program { static void Main() { var (Found, Value) = typeof(MyClass).GetTag("My Tag"); Console.WriteLine("My Tag..."); Console.WriteLine("Found: " + Found); Console.WriteLine("Value: " + Value); } }

[Tag("My Tag", "hello world")] public class MyClass { }

Developer(s)

Zachary Patten Howdy! I'm Zachary Patten. I like making code frameworks and teaching people how to code. I only work on Towel in my free time, but feel free to contact me if you have questions and I will respond when I am able. :)

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.