diff --git a/DsaDotnet/Arithmetic/GCD.cs b/DsaDotnet/Arithmetic/GCD.cs index 6e918d5..d52b5fa 100644 --- a/DsaDotnet/Arithmetic/GCD.cs +++ b/DsaDotnet/Arithmetic/GCD.cs @@ -2,6 +2,12 @@ public static partial class Arithmetic { + /// + /// Calculates the greatest common divisor (GCD) of two integers. + /// + /// The first integer. + /// The second integer. + /// The GCD of the two integers. public static int GCD(int a, int b) { while (b != 0) diff --git a/DsaDotnet/Arithmetic/LCM.cs b/DsaDotnet/Arithmetic/LCM.cs index 4c59bcf..0bcfba0 100644 --- a/DsaDotnet/Arithmetic/LCM.cs +++ b/DsaDotnet/Arithmetic/LCM.cs @@ -2,6 +2,12 @@ public static partial class Arithmetic { + /// + /// Calculates the least common multiple (LCM) of two integers. + /// + /// The first integer. + /// The second integer. + /// The LCM of the two integers. public static int LCM(int a, int b) { if (a == 0 || b == 0) @@ -12,6 +18,11 @@ public static int LCM(int a, int b) return Math.Abs(a * b / GCD(a, b)); } + /// + /// Calculates the least common multiple (LCM) of multiple integers. + /// + /// The array of integers. + /// The LCM of the multiple integers. public static int LCM(params int[] arr) { if (arr.Length == 0) diff --git a/DsaDotnet/DsaDotnet.csproj b/DsaDotnet/DsaDotnet.csproj index 31ef095..ed2bad0 100644 --- a/DsaDotnet/DsaDotnet.csproj +++ b/DsaDotnet/DsaDotnet.csproj @@ -8,7 +8,7 @@ preview true DsaDotnet - 8.0.0 + 8.0.1 Tim Jones Aptacode A collection of optimised algorithms and datastructures diff --git a/DsaDotnet/Graphs/Graph.cs b/DsaDotnet/Graphs/Graph.cs index 126d7f2..4a30fb1 100644 --- a/DsaDotnet/Graphs/Graph.cs +++ b/DsaDotnet/Graphs/Graph.cs @@ -1,9 +1,18 @@ namespace DsaDotnet.Graphs; +/// +/// Represents an abstract graph data structure. +/// +/// The type of the graph nodes. +/// The type of the node keys. public abstract class Graph where U : IEquatable where T : IGraphNode { internal readonly Dictionary Nodes = new(); + /// + /// Adds the specified nodes to the graph. + /// + /// The nodes to add. public void AddNodes(params T[] nodes) { for (var i = 0; i < nodes.Length; i++) @@ -12,6 +21,10 @@ public void AddNodes(params T[] nodes) } } + /// + /// Returns all the nodes in the graph. + /// + /// A read-only collection of all the nodes in the graph. public IReadOnlyCollection AllNodes() { return Nodes.Values; diff --git a/DsaDotnet/Graphs/GraphNode.cs b/DsaDotnet/Graphs/GraphNode.cs index b192e43..160b0f0 100644 --- a/DsaDotnet/Graphs/GraphNode.cs +++ b/DsaDotnet/Graphs/GraphNode.cs @@ -5,11 +5,19 @@ public class GraphNode : IGraphNode, T> where T : IEquatable private readonly List> _neighbors = new(); public required T Key { get; set; } + /// + /// Gets the neighbors of the graph node. + /// + /// An of . public IReadOnlyList> GetNeighbors() { return _neighbors; } + /// + /// Adds a neighbor to the graph node. + /// + /// The neighbor to add. public void AddNeighbor(GraphNode neighbor) { if (_neighbors.Any(n => n.Key.Equals(neighbor.Key))) diff --git a/DsaDotnet/Graphs/IGraphNode.cs b/DsaDotnet/Graphs/IGraphNode.cs index addd161..12239cf 100644 --- a/DsaDotnet/Graphs/IGraphNode.cs +++ b/DsaDotnet/Graphs/IGraphNode.cs @@ -2,6 +2,14 @@ public interface IGraphNode where T : IEquatable { + /// + /// Gets or sets the key of the graph node. + /// public T Key { get; set; } + + /// + /// Gets the neighbors of the graph node. + /// + /// An containing the neighbors of the graph node. public IReadOnlyList GetNeighbors(); } diff --git a/DsaDotnet/Graphs/UnWeightedGraph.cs b/DsaDotnet/Graphs/UnWeightedGraph.cs index 2fa62b3..cde52e4 100644 --- a/DsaDotnet/Graphs/UnWeightedGraph.cs +++ b/DsaDotnet/Graphs/UnWeightedGraph.cs @@ -2,6 +2,10 @@ public class UnWeightedGraph : Graph, U> where U : IEquatable { + /// + /// Adds edges to the graph. + /// + /// The edges to add. public void AddEdges(params (U source, U destination)[] edges) { for (var i = 0; i < edges.Length; i++) diff --git a/DsaDotnet/Graphs/WeightedGraph.cs b/DsaDotnet/Graphs/WeightedGraph.cs index 7938b76..75e561e 100644 --- a/DsaDotnet/Graphs/WeightedGraph.cs +++ b/DsaDotnet/Graphs/WeightedGraph.cs @@ -2,6 +2,11 @@ public class WeightedGraph : Graph, U> where U : IEquatable { + /// + /// Adds weighted edges to the graph. + /// + /// The array of edges to add. + /// The weight of the edges. public void AddEdges((U source, U destination)[] edges, int weight) { for (var i = 0; i < edges.Length; i++) @@ -21,6 +26,10 @@ public void AddEdges((U source, U destination)[] edges, int weight) } } + /// + /// Adds weighted edges to the graph. + /// + /// The array of edges to add. public void AddEdges((U source, U destination, int weight)[] edges) { for (var i = 0; i < edges.Length; i++) diff --git a/DsaDotnet/Graphs/WeightedGraphNode.cs b/DsaDotnet/Graphs/WeightedGraphNode.cs index b40a8c2..b40e410 100644 --- a/DsaDotnet/Graphs/WeightedGraphNode.cs +++ b/DsaDotnet/Graphs/WeightedGraphNode.cs @@ -1,21 +1,38 @@ namespace DsaDotnet.Graphs; +/// +/// Represents a weighted graph node. +/// +/// The type of the node's key. public class WeightedGraphNode : IGraphNode, T> where T : IEquatable { private readonly List> _neighbors = new(); private readonly List<(int, WeightedGraphNode)> _weightedNeighbors = new(); public required T Key { get; set; } + /// + /// Gets the list of neighbors of the node. + /// + /// The list of neighbors. public IReadOnlyList> GetNeighbors() { return _neighbors; } + /// + /// Gets the collection of weighted neighbors of the node. + /// + /// The collection of weighted neighbors. public IReadOnlyCollection<(int, WeightedGraphNode)> GetWeightedNeighbors() { return _weightedNeighbors; } + /// + /// Adds a neighbor to the node with the specified weight. + /// + /// The neighbor node. + /// The weight of the neighbor. public void AddNeighbor(WeightedGraphNode neighbor, int weight) { _neighbors.Add(neighbor); diff --git a/DsaDotnet/Search/Bfs.cs b/DsaDotnet/Search/Bfs.cs index 0e0a2bd..9614976 100644 --- a/DsaDotnet/Search/Bfs.cs +++ b/DsaDotnet/Search/Bfs.cs @@ -4,6 +4,16 @@ namespace DsaDotnet; public static partial class Search { + /// + /// Performs a breadth-first search on the specified graph starting from the given node and using the provided predicate. + /// + /// The type of the graph. + /// The type of the graph nodes. + /// The graph to perform the search on. + /// The starting node. + /// The predicate used to determine if a node matches the search criteria. + /// The first node that matches the search criteria, or null if no such node is found. + /// Thrown when the start node does not exist in the graph. public static T? BreadthFirstSearch(this Graph graph, U start, Predicate predicate) where U : IEquatable where T : class?, IGraphNode { diff --git a/DsaDotnet/Search/BinarySearch.cs b/DsaDotnet/Search/BinarySearch.cs index e91dc72..85da324 100644 --- a/DsaDotnet/Search/BinarySearch.cs +++ b/DsaDotnet/Search/BinarySearch.cs @@ -2,6 +2,14 @@ public static partial class Search { + /// + /// Performs a binary search on a sorted list and returns the index of the specified value. + /// + /// The type of elements in the list. + /// The sorted list to search. + /// The value to search for. + /// The comparer used to compare elements. If null, the default comparer for the type is used. + /// The index of the specified value in the list, or -1 if the value is not found. public static int BinarySearch(this IList source, T value, IComparer? comparer = null) { comparer ??= Comparer.Default; diff --git a/DsaDotnet/Search/Dfs.cs b/DsaDotnet/Search/Dfs.cs index e57f45a..22d35ab 100644 --- a/DsaDotnet/Search/Dfs.cs +++ b/DsaDotnet/Search/Dfs.cs @@ -4,6 +4,16 @@ namespace DsaDotnet; public static partial class Search { + /// + /// Performs a depth-first search on the graph starting from the specified node and using the given predicate. + /// + /// The type of the graph nodes. + /// The type of the node identifier. + /// The graph to perform the search on. + /// The identifier of the starting node. + /// The predicate used to determine if a node matches the search criteria. + /// The first node that matches the search criteria, or null if no such node is found. + /// Thrown when the start node does not exist in the graph. public static T? DepthFirstSearch(this Graph graph, U start, Predicate predicate) where U : IEquatable where T : class?, IGraphNode { diff --git a/DsaDotnet/Search/Dijkstra.cs b/DsaDotnet/Search/Dijkstra.cs index 28963fa..3b63352 100644 --- a/DsaDotnet/Search/Dijkstra.cs +++ b/DsaDotnet/Search/Dijkstra.cs @@ -4,6 +4,15 @@ namespace DsaDotnet; public static partial class Search { + /// + /// Finds the shortest path in a weighted graph using Dijkstra's algorithm. + /// + /// The type of the node values in the graph. + /// The weighted graph. + /// The start node value. + /// The predicate used to determine the end node. + /// The collection of nodes representing the shortest path, or null if no path is found. + /// Thrown when the start node does not exist in the graph. public static ICollection>? Dijkstra(this WeightedGraph graph, U start, Predicate> predicate) where U : IEquatable diff --git a/DsaDotnet/Search/LinearSearch.cs b/DsaDotnet/Search/LinearSearch.cs index 4e8bee2..b076339 100644 --- a/DsaDotnet/Search/LinearSearch.cs +++ b/DsaDotnet/Search/LinearSearch.cs @@ -2,6 +2,13 @@ public static partial class Search { + /// + /// Performs a linear search on the given source list to find the index of the target element. + /// + /// The type of elements in the list. + /// The source list to search. + /// The target element to search for. + /// The index of the target element if found; otherwise, -1. public static int LinearSearch(this IList source, T target) where T : IEquatable { for (var i = 0; i < source.Count; i++) diff --git a/DsaDotnet/Series/Factorial.cs b/DsaDotnet/Series/Factorial.cs index 9bc1484..705f3a2 100644 --- a/DsaDotnet/Series/Factorial.cs +++ b/DsaDotnet/Series/Factorial.cs @@ -2,8 +2,17 @@ namespace DsaDotnet; +/// +/// Provides a collection of mathematical series functions. +/// public static partial class Series { + /// + /// Computes the factorial of a given number. + /// + /// The number to compute the factorial for. + /// The factorial of the given number. + /// Thrown when the input number is negative. public static BigInteger Factorial(this int n) { switch (n) diff --git a/DsaDotnet/Series/Fibonacci.cs b/DsaDotnet/Series/Fibonacci.cs index 02fdf0b..846bc0b 100644 --- a/DsaDotnet/Series/Fibonacci.cs +++ b/DsaDotnet/Series/Fibonacci.cs @@ -4,6 +4,12 @@ namespace DsaDotnet; public static partial class Series { + /// + /// Calculates the Fibonacci number at the specified index. + /// + /// The index of the Fibonacci number to calculate. + /// The Fibonacci number at the specified index. + /// Thrown when the input is a negative number. public static ulong Fibonacci(this int input) { if (input <= 1) diff --git a/DsaDotnet/Series/Primes.cs b/DsaDotnet/Series/Primes.cs index edcda1e..d2c28dc 100644 --- a/DsaDotnet/Series/Primes.cs +++ b/DsaDotnet/Series/Primes.cs @@ -2,6 +2,11 @@ public static partial class Series { + /// + /// Generates a list of prime numbers up to the specified number. + /// + /// The upper limit for generating prime numbers. + /// A list of prime numbers up to the specified number. public static List PrimesUpTo(this int n) { var primes = new List(); diff --git a/DsaDotnet/Sorting/Bubble.cs b/DsaDotnet/Sorting/Bubble.cs index 64af598..0b7963d 100644 --- a/DsaDotnet/Sorting/Bubble.cs +++ b/DsaDotnet/Sorting/Bubble.cs @@ -2,6 +2,13 @@ public static partial class Sorting { + /// + /// Sorts the elements of the sequence using the Bubble Sort algorithm. + /// + /// The type of the elements in the sequence. + /// The sequence to sort. + /// The comparer to use for comparing elements. If null, the default comparer for the type is used. + /// A new array containing the sorted elements. public static T[] BubbleSort(this IEnumerable source, IComparer? comparer = null) { var elementArray = source.ToArray(); @@ -9,6 +16,12 @@ public static T[] BubbleSort(this IEnumerable source, IComparer? compar return elementArray; } + /// + /// Sorts the elements of the array using the Bubble Sort algorithm. + /// + /// The type of the elements in the array. + /// The array to sort. + /// The comparer to use for comparing elements. If null, the default comparer for the type is used. public static void BubbleSortInPlace(this T[] source, IComparer? comparer = null) { if (source.Length < 2) diff --git a/DsaDotnet/Sorting/Insertion.cs b/DsaDotnet/Sorting/Insertion.cs index c6781f7..4397a32 100644 --- a/DsaDotnet/Sorting/Insertion.cs +++ b/DsaDotnet/Sorting/Insertion.cs @@ -2,6 +2,13 @@ public static partial class Sorting { + /// + /// Sorts the elements of the sequence using the Insertion Sort algorithm. + /// + /// The type of the elements in the sequence. + /// The sequence to sort. + /// The comparer to use for comparing elements. If null, the default comparer for the type is used. + /// A new array containing the sorted elements. public static T[] InsertionSort(this IEnumerable source, IComparer? comparer = null) { var elementArray = source.ToArray(); @@ -9,6 +16,12 @@ public static T[] InsertionSort(this IEnumerable source, IComparer? com return elementArray; } + /// + /// Sorts the elements of the array using the Insertion Sort algorithm. + /// + /// The type of the elements in the array. + /// The array to sort. + /// The comparer to use for comparing elements. If null, the default comparer for the type is used. public static void InsertionSortInPlace(this T[] source, IComparer? comparer = null) { if (source.Length < 2) diff --git a/DsaDotnet/Sorting/Merge.cs b/DsaDotnet/Sorting/Merge.cs index 3175208..cafc825 100644 --- a/DsaDotnet/Sorting/Merge.cs +++ b/DsaDotnet/Sorting/Merge.cs @@ -2,6 +2,13 @@ public static partial class Sorting { + /// + /// Sorts the elements of the source collection using the Merge Sort algorithm. + /// + /// The type of the elements in the collection. + /// The source collection to be sorted. + /// The comparer used to compare elements. If null, the default comparer for the type is used. + /// A new array containing the sorted elements. public static T[] MergeSort(this IEnumerable source, IComparer? comparer = null) { var elementArray = source.ToArray(); @@ -9,6 +16,12 @@ public static T[] MergeSort(this IEnumerable source, IComparer? compare return elementArray; } + /// + /// Sorts the elements of the source array using the Merge Sort algorithm in place. + /// + /// The type of the elements in the array. + /// The source array to be sorted. + /// The comparer used to compare elements. If null, the default comparer for the type is used. public static void MergeSortInPlace(this T[] source, IComparer? comparer = null) { if (source.Length < 2) @@ -31,6 +44,15 @@ public static void MergeSortInPlace(this T[] source, IComparer? comparer = } } + /// + /// Merges two sorted subarrays of the given array. + /// + /// The type of the elements in the array. + /// The array to be merged. + /// The starting index of the left subarray. + /// The ending index of the left subarray and the starting index of the right subarray. + /// The ending index of the right subarray. + /// The comparer used to compare elements. private static void Merge(T[] array, int left, int mid, int right, IComparer comparer) { var n1 = mid - left + 1; diff --git a/DsaDotnet/Sorting/Quick.cs b/DsaDotnet/Sorting/Quick.cs index 160d9cf..821be5e 100644 --- a/DsaDotnet/Sorting/Quick.cs +++ b/DsaDotnet/Sorting/Quick.cs @@ -2,6 +2,13 @@ public static partial class Sorting { + /// + /// Sorts the elements of the sequence using the QuickSort algorithm. + /// + /// The type of the elements in the sequence. + /// The sequence to sort. + /// The comparer to use for comparing elements. If null, the default comparer for the type is used. + /// A new array containing the sorted elements. public static T[] QuickSort(this IEnumerable source, IComparer? comparer = null) { var elementArray = source.ToArray(); @@ -9,6 +16,12 @@ public static T[] QuickSort(this IEnumerable source, IComparer? compare return elementArray; } + /// + /// Sorts the elements of the list using the QuickSort algorithm. + /// + /// The type of the elements in the list. + /// The list to sort. + /// The comparer to use for comparing elements. If null, the default comparer for the type is used. public static void QuickSortInPlace(this IList source, IComparer? comparer = null) { if (source.Count < 2) diff --git a/DsaDotnet/Sorting/SortingHelpers.cs b/DsaDotnet/Sorting/SortingHelpers.cs index 1de263e..a90f114 100644 --- a/DsaDotnet/Sorting/SortingHelpers.cs +++ b/DsaDotnet/Sorting/SortingHelpers.cs @@ -4,8 +4,6 @@ public static partial class Sorting { private static void Swap(IList array, int i, int j) { - var temp = array[i]; - array[i] = array[j]; - array[j] = temp; + (array[i], array[j]) = (array[j], array[i]); } } diff --git a/DsaDotnet/Trees/BinaryTree.cs b/DsaDotnet/Trees/BinaryTree.cs index 01de053..fe7d79c 100644 --- a/DsaDotnet/Trees/BinaryTree.cs +++ b/DsaDotnet/Trees/BinaryTree.cs @@ -1,9 +1,17 @@ namespace DsaDotnet.Trees; +/// +/// Represents a binary tree data structure. +/// +/// The type of elements in the binary tree. public class BinaryTree where T : IComparable { private readonly IComparer _comparer; + /// + /// Initializes a new instance of the class. + /// + /// The comparer used to compare elements in the binary tree. public BinaryTree(IComparer? comparer = null) { if (comparer != null) @@ -16,13 +24,17 @@ public BinaryTree(IComparer? comparer = null) } } - private TreeNode? _root { get; set; } + private TreeNode? Root { get; set; } + /// + /// Inserts the specified keys into the binary tree. + /// + /// The keys to insert. public void Insert(params T[] keys) { for (var i = 0; i < keys.Length; i++) { - _root = InsertRec(_root, keys[i]); + Root = InsertRec(Root, keys[i]); } } @@ -48,9 +60,14 @@ private TreeNode InsertRec(TreeNode? node, T key) return node; } + /// + /// Searches for the specified key in the binary tree. + /// + /// The key to search for. + /// The node containing the key, or null if the key is not found. public TreeNode? Search(T key) { - return SearchRec(_root, key); + return SearchRec(Root, key); } private TreeNode? SearchRec(TreeNode? node, T key) diff --git a/DsaDotnet/Trees/TreeNode.cs b/DsaDotnet/Trees/TreeNode.cs index ebaf5e7..ce02a29 100644 --- a/DsaDotnet/Trees/TreeNode.cs +++ b/DsaDotnet/Trees/TreeNode.cs @@ -1,8 +1,23 @@ namespace DsaDotnet.Trees; +/// +/// Represents a node in a binary tree. +/// +/// The type of the node's key. public class TreeNode { + /// + /// Gets or sets the key of the node. + /// public required T Key { get; init; } + + /// + /// Gets or sets the left child of the node. + /// public TreeNode? Left { get; set; } + + /// + /// Gets or sets the right child of the node. + /// public TreeNode? Right { get; set; } } diff --git a/README.md b/README.md index d4b852f..ecf0166 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# DsaDotnet +# DsaDotnet ![Logo](DsaDotnet/logo.png) [![NuGet](https://img.shields.io/nuget/v/DsaDotnet)](https://www.nuget.org/packages/DsaDotnet) A collection of optimised algorithms and datastructures. diff --git a/docs/releases.md b/docs/releases.md index daaaa70..f5826c9 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -1,3 +1,4 @@ | Version | Description | |---------|-----------------| +| 8.0.1 | Added XML docs | | 8.0.0 | Initial release | \ No newline at end of file