Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<PackageVersion Include="xunit" Version="2.8.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
37 changes: 37 additions & 0 deletions src/Csdsa/Algorithms/Search/Linear/LinearSSearch.IndexOf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Returns the zero-based index of the first element in the sequence
/// that satisfies the specified predicate, or -1 if no such element exists.
/// </summary>
/// <typeparam name="T">The element type of the sequence.</typeparam>
/// <param name="collection">The sequence to search.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>
/// The zero-based index of the first matching element, or -1 if no match is found.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="collection"/> or <paramref name="match"/> is <see langword="null"/>.
/// </exception>
public static int IndexOf<T>(this IEnumerable<T> collection, Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(match);

int index = 0;

foreach (T item in collection)
{
if (match(item))
{
return index;
}

index++;
}

return -1;
}
}
36 changes: 36 additions & 0 deletions src/Csdsa/Algorithms/Search/Linear/LinearSearch.AllMatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;

namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Determines whether all elements in the sequence satisfy the specified predicate.
/// </summary>
/// <typeparam name="T">The element type of the sequence.</typeparam>
/// <param name="collection">The sequence to test.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>
/// <see langword="true"/> if all elements satisfy <paramref name="match"/>;
/// otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="collection"/> or <paramref name="match"/> is <see langword="null"/>.
/// </exception>
public static bool AllMatch<T>(this IEnumerable<T> collection, Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(match);

foreach (T item in collection)
{
if (!match(item))
{
return false;
}
}

return true;
}
}
33 changes: 33 additions & 0 deletions src/Csdsa/Algorithms/Search/Linear/LinearSearch.AnyMatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Determines whether any element in the sequence satisfies the specified predicate.
/// </summary>
/// <typeparam name="T">The element type of the sequence.</typeparam>
/// <param name="collection">The sequence to test.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>
/// <see langword="true"/> if any element satisfies <paramref name="match"/>;
/// otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="collection"/> or <paramref name="match"/> is <see langword="null"/>.
/// </exception>
public static bool AnyMatch<T>(this IEnumerable<T> collection, Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(match);

foreach (T item in collection)
{
if (match(item))
{
return true;
}
}

return false;
}
}
25 changes: 25 additions & 0 deletions src/Csdsa/Algorithms/Search/Linear/LinearSearch.ContainsMatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Determines whether any element in the sequence satisfies the specified predicate.
/// </summary>
/// <typeparam name="T">The element type of the sequence.</typeparam>
/// <param name="collection">The sequence to search.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>
/// <see langword="true"/> if any element satisfies <paramref name="match"/>;
/// otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="collection"/> or <paramref name="match"/> is <see langword="null"/>.
/// </exception>
public static bool ContainsMatch<T>(this IEnumerable<T> collection, Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(match);

return collection.IndexOf(match) != -1;
}
}
32 changes: 32 additions & 0 deletions src/Csdsa/Algorithms/Search/Linear/LinearSearch.CountMatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Counts the number of elements in the sequence that satisfy the specified predicate.
/// </summary>
/// <typeparam name="T">The element type of the sequence.</typeparam>
/// <param name="collection">The sequence to search.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>The number of elements satisfying <paramref name="match"/>.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="collection"/> or <paramref name="match"/> is <see langword="null"/>.
/// </exception>
public static int CountMatches<T>(this IEnumerable<T> collection, Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(match);

int count = 0;

foreach (T item in collection)
{
if (match(item))
{
count++;
}
}

return count;
}
}
19 changes: 19 additions & 0 deletions src/Csdsa/Algorithms/Search/Linear/LinearSearch.FindAllAlias.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Returns a list of all elements in the sequence that satisfy the specified predicate.
/// This is an alias for <see cref="FindAllMatches{T}(IEnumerable{T}, Func{T, bool})"/>.
/// </summary>
/// <typeparam name="T">The element type of the sequence.</typeparam>
/// <param name="collection">The sequence to search.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>
/// A read-only list containing all elements that satisfy <paramref name="match"/>.
/// </returns>
public static IReadOnlyList<T> FindAll<T>(this IEnumerable<T> collection, Func<T, bool> match)
{
return FindAllMatches(collection, match);
}
}
34 changes: 34 additions & 0 deletions src/Csdsa/Algorithms/Search/Linear/LinearSearch.FindAllMatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Returns a list of all elements in the sequence that satisfy the specified predicate.
/// </summary>
/// <typeparam name="T">The element type of the sequence.</typeparam>
/// <param name="collection">The sequence to search.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>
/// A read-only list containing all elements that satisfy <paramref name="match"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="collection"/> or <paramref name="match"/> is <see langword="null"/>.
/// </exception>
public static IReadOnlyList<T> FindAllMatches<T>(this IEnumerable<T> collection, Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(match);

List<T> result = new List<T>();

foreach (T item in collection)
{
if (match(item))
{
result.Add(item);
}
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;

namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Returns the first element in the sequence that satisfies the specified predicate,
/// or the default value of <typeparamref name="T"/> if no such element is found.
/// </summary>
/// <typeparam name="T">The element type of the sequence.</typeparam>
/// <param name="collection">The sequence to search.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>
/// The first matching element, or <c>default(T)</c> if no match is found.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="collection"/> or <paramref name="match"/> is <see langword="null"/>.
/// </exception>
public static T FirstOrDefaultMatch<T>(this IEnumerable<T> collection, Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(match);

foreach (T item in collection)
{
if (match(item))
{
return item;
}
}

return default;
}
}
38 changes: 38 additions & 0 deletions src/Csdsa/Algorithms/Search/Linear/LinearSearch.LastIndexOf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Returns the zero-based index of the last element in the sequence that
/// satisfies the specified predicate, or -1 if no such element exists.
/// </summary>
/// <typeparam name="T">The element type of the sequence.</typeparam>
/// <param name="collection">The sequence to search.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>
/// The zero-based index of the last matching element, or -1 if no match is found.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="collection"/> or <paramref name="match"/> is <see langword="null"/>.
/// </exception>
public static int LastIndexOf<T>(this IEnumerable<T> collection, Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(match);

int lastIndex = -1;
int index = 0;

foreach (T item in collection)
{
if (match(item))
{
lastIndex = index;
}

index++;
}

return lastIndex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Removes all elements from the list that satisfy the specified predicate
/// and returns the number of removed elements.
/// </summary>
/// <typeparam name="T">The element type of the list.</typeparam>
/// <param name="collection">The list to modify.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>The number of elements removed from <paramref name="collection"/>.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="collection"/> or <paramref name="match"/> is <see langword="null"/>.
/// </exception>
public static int RemoveAllMatches<T>(this List<T> collection, Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(match);

int removed = 0;

for (int i = collection.Count - 1; i >= 0; i--)
{
if (match(collection[i]))
{
collection.RemoveAt(i);
removed++;
}
}

return removed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Csdsa.Algorithms.Search.Linear;

public static partial class LinearSearch
{
/// <summary>
/// Returns the single element in the sequence that satisfies the specified
/// predicate, or the default value of <typeparamref name="T"/> if no such
/// element is found or if more than one matching element exists.
/// </summary>
/// <typeparam name="T">The element type of the sequence.</typeparam>
/// <param name="collection">The sequence to search.</param>
/// <param name="match">The predicate used to test each element.</param>
/// <returns>
/// The single matching element, or <c>default(T)</c> if zero or multiple matches are found.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="collection"/> or <paramref name="match"/> is <see langword="null"/>.
/// </exception>
public static T SingleOrDefaultMatch<T>(this IEnumerable<T> collection, Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(match);

T matchItem = default;
int count = 0;

foreach (T item in collection)
{
if (!match(item))
{
continue;
}

matchItem = item;
count++;

if (count > 1)
{
return default;
}
}

return matchItem;
}
}
Loading