Skip to content

Commit

Permalink
Added xml docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Timmoth committed Jun 16, 2024
1 parent e75a40c commit 4035724
Show file tree
Hide file tree
Showing 26 changed files with 240 additions and 8 deletions.
6 changes: 6 additions & 0 deletions DsaDotnet/Arithmetic/GCD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

public static partial class Arithmetic
{
/// <summary>
/// Calculates the greatest common divisor (GCD) of two integers.
/// </summary>
/// <param name="a">The first integer.</param>
/// <param name="b">The second integer.</param>
/// <returns>The GCD of the two integers.</returns>
public static int GCD(int a, int b)
{
while (b != 0)
Expand Down
11 changes: 11 additions & 0 deletions DsaDotnet/Arithmetic/LCM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

public static partial class Arithmetic
{
/// <summary>
/// Calculates the least common multiple (LCM) of two integers.
/// </summary>
/// <param name="a">The first integer.</param>
/// <param name="b">The second integer.</param>
/// <returns>The LCM of the two integers.</returns>
public static int LCM(int a, int b)
{
if (a == 0 || b == 0)
Expand All @@ -12,6 +18,11 @@ public static int LCM(int a, int b)
return Math.Abs(a * b / GCD(a, b));
}

/// <summary>
/// Calculates the least common multiple (LCM) of multiple integers.
/// </summary>
/// <param name="arr">The array of integers.</param>
/// <returns>The LCM of the multiple integers.</returns>
public static int LCM(params int[] arr)
{
if (arr.Length == 0)
Expand Down
2 changes: 1 addition & 1 deletion DsaDotnet/DsaDotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<LangVersion>preview</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>DsaDotnet</PackageId>
<PackageVersion>8.0.0</PackageVersion>
<PackageVersion>8.0.1</PackageVersion>
<Authors>Tim Jones</Authors>
<Company>Aptacode</Company>
<Description>A collection of optimised algorithms and datastructures</Description>
Expand Down
13 changes: 13 additions & 0 deletions DsaDotnet/Graphs/Graph.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
namespace DsaDotnet.Graphs;

/// <summary>
/// Represents an abstract graph data structure.
/// </summary>
/// <typeparam name="T">The type of the graph nodes.</typeparam>
/// <typeparam name="U">The type of the node keys.</typeparam>
public abstract class Graph<T, U> where U : IEquatable<U> where T : IGraphNode<T, U>
{
internal readonly Dictionary<U, T> Nodes = new();

/// <summary>
/// Adds the specified nodes to the graph.
/// </summary>
/// <param name="nodes">The nodes to add.</param>
public void AddNodes(params T[] nodes)
{
for (var i = 0; i < nodes.Length; i++)
Expand All @@ -12,6 +21,10 @@ public void AddNodes(params T[] nodes)
}
}

/// <summary>
/// Returns all the nodes in the graph.
/// </summary>
/// <returns>A read-only collection of all the nodes in the graph.</returns>
public IReadOnlyCollection<T> AllNodes()
{
return Nodes.Values;
Expand Down
8 changes: 8 additions & 0 deletions DsaDotnet/Graphs/GraphNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ public class GraphNode<T> : IGraphNode<GraphNode<T>, T> where T : IEquatable<T>
private readonly List<GraphNode<T>> _neighbors = new();
public required T Key { get; set; }

/// <summary>
/// Gets the neighbors of the graph node.
/// </summary>
/// <returns>An <see cref="IReadOnlyList{T}"/> of <see cref="GraphNode{T}"/>.</returns>
public IReadOnlyList<GraphNode<T>> GetNeighbors()
{
return _neighbors;
}

/// <summary>
/// Adds a neighbor to the graph node.
/// </summary>
/// <param name="neighbor">The neighbor to add.</param>
public void AddNeighbor(GraphNode<T> neighbor)
{
if (_neighbors.Any(n => n.Key.Equals(neighbor.Key)))
Expand Down
8 changes: 8 additions & 0 deletions DsaDotnet/Graphs/IGraphNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

public interface IGraphNode<out TSelf, T> where T : IEquatable<T>
{
/// <summary>
/// Gets or sets the key of the graph node.
/// </summary>
public T Key { get; set; }

/// <summary>
/// Gets the neighbors of the graph node.
/// </summary>
/// <returns>An <see cref="IReadOnlyList{TSelf}"/> containing the neighbors of the graph node.</returns>
public IReadOnlyList<TSelf> GetNeighbors();
}
4 changes: 4 additions & 0 deletions DsaDotnet/Graphs/UnWeightedGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

public class UnWeightedGraph<U> : Graph<GraphNode<U>, U> where U : IEquatable<U>
{
/// <summary>
/// Adds edges to the graph.
/// </summary>
/// <param name="edges">The edges to add.</param>
public void AddEdges(params (U source, U destination)[] edges)
{
for (var i = 0; i < edges.Length; i++)
Expand Down
9 changes: 9 additions & 0 deletions DsaDotnet/Graphs/WeightedGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

public class WeightedGraph<U> : Graph<WeightedGraphNode<U>, U> where U : IEquatable<U>
{
/// <summary>
/// Adds weighted edges to the graph.
/// </summary>
/// <param name="edges">The array of edges to add.</param>
/// <param name="weight">The weight of the edges.</param>
public void AddEdges((U source, U destination)[] edges, int weight)
{
for (var i = 0; i < edges.Length; i++)
Expand All @@ -21,6 +26,10 @@ public void AddEdges((U source, U destination)[] edges, int weight)
}
}

/// <summary>
/// Adds weighted edges to the graph.
/// </summary>
/// <param name="edges">The array of edges to add.</param>
public void AddEdges((U source, U destination, int weight)[] edges)
{
for (var i = 0; i < edges.Length; i++)
Expand Down
17 changes: 17 additions & 0 deletions DsaDotnet/Graphs/WeightedGraphNode.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
namespace DsaDotnet.Graphs;

/// <summary>
/// Represents a weighted graph node.
/// </summary>
/// <typeparam name="T">The type of the node's key.</typeparam>
public class WeightedGraphNode<T> : IGraphNode<WeightedGraphNode<T>, T> where T : IEquatable<T>
{
private readonly List<WeightedGraphNode<T>> _neighbors = new();
private readonly List<(int, WeightedGraphNode<T>)> _weightedNeighbors = new();
public required T Key { get; set; }

/// <summary>
/// Gets the list of neighbors of the node.
/// </summary>
/// <returns>The list of neighbors.</returns>
public IReadOnlyList<WeightedGraphNode<T>> GetNeighbors()
{
return _neighbors;
}

/// <summary>
/// Gets the collection of weighted neighbors of the node.
/// </summary>
/// <returns>The collection of weighted neighbors.</returns>
public IReadOnlyCollection<(int, WeightedGraphNode<T>)> GetWeightedNeighbors()
{
return _weightedNeighbors;
}

/// <summary>
/// Adds a neighbor to the node with the specified weight.
/// </summary>
/// <param name="neighbor">The neighbor node.</param>
/// <param name="weight">The weight of the neighbor.</param>
public void AddNeighbor(WeightedGraphNode<T> neighbor, int weight)
{
_neighbors.Add(neighbor);
Expand Down
10 changes: 10 additions & 0 deletions DsaDotnet/Search/Bfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ namespace DsaDotnet;

public static partial class Search
{
/// <summary>
/// Performs a breadth-first search on the specified graph starting from the given node and using the provided predicate.
/// </summary>
/// <typeparam name="T">The type of the graph.</typeparam>
/// <typeparam name="U">The type of the graph nodes.</typeparam>
/// <param name="graph">The graph to perform the search on.</param>
/// <param name="start">The starting node.</param>
/// <param name="predicate">The predicate used to determine if a node matches the search criteria.</param>
/// <returns>The first node that matches the search criteria, or null if no such node is found.</returns>
/// <exception cref="ArgumentException">Thrown when the start node does not exist in the graph.</exception>
public static T? BreadthFirstSearch<T, U>(this Graph<T, U> graph, U start, Predicate<T> predicate)
where U : IEquatable<U> where T : class?, IGraphNode<T, U>
{
Expand Down
8 changes: 8 additions & 0 deletions DsaDotnet/Search/BinarySearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

public static partial class Search
{
/// <summary>
/// Performs a binary search on a sorted list and returns the index of the specified value.
/// </summary>
/// <typeparam name="T">The type of elements in the list.</typeparam>
/// <param name="source">The sorted list to search.</param>
/// <param name="value">The value to search for.</param>
/// <param name="comparer">The comparer used to compare elements. If null, the default comparer for the type is used.</param>
/// <returns>The index of the specified value in the list, or -1 if the value is not found.</returns>
public static int BinarySearch<T>(this IList<T> source, T value, IComparer<T>? comparer = null)
{
comparer ??= Comparer<T>.Default;
Expand Down
10 changes: 10 additions & 0 deletions DsaDotnet/Search/Dfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ namespace DsaDotnet;

public static partial class Search
{
/// <summary>
/// Performs a depth-first search on the graph starting from the specified node and using the given predicate.
/// </summary>
/// <typeparam name="T">The type of the graph nodes.</typeparam>
/// <typeparam name="U">The type of the node identifier.</typeparam>
/// <param name="graph">The graph to perform the search on.</param>
/// <param name="start">The identifier of the starting node.</param>
/// <param name="predicate">The predicate used to determine if a node matches the search criteria.</param>
/// <returns>The first node that matches the search criteria, or null if no such node is found.</returns>
/// <exception cref="ArgumentException">Thrown when the start node does not exist in the graph.</exception>
public static T? DepthFirstSearch<T, U>(this Graph<T, U> graph, U start, Predicate<T> predicate)
where U : IEquatable<U> where T : class?, IGraphNode<T, U>
{
Expand Down
9 changes: 9 additions & 0 deletions DsaDotnet/Search/Dijkstra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ namespace DsaDotnet;

public static partial class Search
{
/// <summary>
/// Finds the shortest path in a weighted graph using Dijkstra's algorithm.
/// </summary>
/// <typeparam name="U">The type of the node values in the graph.</typeparam>
/// <param name="graph">The weighted graph.</param>
/// <param name="start">The start node value.</param>
/// <param name="predicate">The predicate used to determine the end node.</param>
/// <returns>The collection of nodes representing the shortest path, or null if no path is found.</returns>
/// <exception cref="ArgumentException">Thrown when the start node does not exist in the graph.</exception>
public static ICollection<WeightedGraphNode<U>>? Dijkstra<U>(this WeightedGraph<U> graph, U start,
Predicate<WeightedGraphNode<U>> predicate)
where U : IEquatable<U>
Expand Down
7 changes: 7 additions & 0 deletions DsaDotnet/Search/LinearSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

public static partial class Search
{
/// <summary>
/// Performs a linear search on the given source list to find the index of the target element.
/// </summary>
/// <typeparam name="T">The type of elements in the list.</typeparam>
/// <param name="source">The source list to search.</param>
/// <param name="target">The target element to search for.</param>
/// <returns>The index of the target element if found; otherwise, -1.</returns>
public static int LinearSearch<T>(this IList<T> source, T target) where T : IEquatable<T>
{
for (var i = 0; i < source.Count; i++)
Expand Down
9 changes: 9 additions & 0 deletions DsaDotnet/Series/Factorial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

namespace DsaDotnet;

/// <summary>
/// Provides a collection of mathematical series functions.
/// </summary>
public static partial class Series
{
/// <summary>
/// Computes the factorial of a given number.
/// </summary>
/// <param name="n">The number to compute the factorial for.</param>
/// <returns>The factorial of the given number.</returns>
/// <exception cref="ArgumentException">Thrown when the input number is negative.</exception>
public static BigInteger Factorial(this int n)
{
switch (n)
Expand Down
6 changes: 6 additions & 0 deletions DsaDotnet/Series/Fibonacci.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ namespace DsaDotnet;

public static partial class Series
{
/// <summary>
/// Calculates the Fibonacci number at the specified index.
/// </summary>
/// <param name="input">The index of the Fibonacci number to calculate.</param>
/// <returns>The Fibonacci number at the specified index.</returns>
/// <exception cref="ArgumentException">Thrown when the input is a negative number.</exception>
public static ulong Fibonacci(this int input)
{
if (input <= 1)
Expand Down
5 changes: 5 additions & 0 deletions DsaDotnet/Series/Primes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

public static partial class Series
{
/// <summary>
/// Generates a list of prime numbers up to the specified number.
/// </summary>
/// <param name="n">The upper limit for generating prime numbers.</param>
/// <returns>A list of prime numbers up to the specified number.</returns>
public static List<int> PrimesUpTo(this int n)
{
var primes = new List<int>();
Expand Down
13 changes: 13 additions & 0 deletions DsaDotnet/Sorting/Bubble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

public static partial class Sorting
{
/// <summary>
/// Sorts the elements of the sequence using the Bubble Sort algorithm.
/// </summary>
/// <typeparam name="T">The type of the elements in the sequence.</typeparam>
/// <param name="source">The sequence to sort.</param>
/// <param name="comparer">The comparer to use for comparing elements. If null, the default comparer for the type is used.</param>
/// <returns>A new array containing the sorted elements.</returns>
public static T[] BubbleSort<T>(this IEnumerable<T> source, IComparer<T>? comparer = null)
{
var elementArray = source.ToArray();
elementArray.BubbleSortInPlace(comparer);
return elementArray;
}

/// <summary>
/// Sorts the elements of the array using the Bubble Sort algorithm.
/// </summary>
/// <typeparam name="T">The type of the elements in the array.</typeparam>
/// <param name="source">The array to sort.</param>
/// <param name="comparer">The comparer to use for comparing elements. If null, the default comparer for the type is used.</param>
public static void BubbleSortInPlace<T>(this T[] source, IComparer<T>? comparer = null)
{
if (source.Length < 2)
Expand Down
13 changes: 13 additions & 0 deletions DsaDotnet/Sorting/Insertion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

public static partial class Sorting
{
/// <summary>
/// Sorts the elements of the sequence using the Insertion Sort algorithm.
/// </summary>
/// <typeparam name="T">The type of the elements in the sequence.</typeparam>
/// <param name="source">The sequence to sort.</param>
/// <param name="comparer">The comparer to use for comparing elements. If null, the default comparer for the type is used.</param>
/// <returns>A new array containing the sorted elements.</returns>
public static T[] InsertionSort<T>(this IEnumerable<T> source, IComparer<T>? comparer = null)
{
var elementArray = source.ToArray();
elementArray.InsertionSortInPlace(comparer);
return elementArray;
}

/// <summary>
/// Sorts the elements of the array using the Insertion Sort algorithm.
/// </summary>
/// <typeparam name="T">The type of the elements in the array.</typeparam>
/// <param name="source">The array to sort.</param>
/// <param name="comparer">The comparer to use for comparing elements. If null, the default comparer for the type is used.</param>
public static void InsertionSortInPlace<T>(this T[] source, IComparer<T>? comparer = null)
{
if (source.Length < 2)
Expand Down
Loading

0 comments on commit 4035724

Please sign in to comment.