Skip to content

Commit

Permalink
feat: hash set extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa committed Sep 17, 2024
1 parent 27cb28f commit 8408554
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
25 changes: 25 additions & 0 deletions Chickensoft.Collections.Tests/src/extensions/HashSetExTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Chickensoft.Collections.Tests;

using System.Collections.Generic;
using Shouldly;
using Xunit;

public class HashSetExTest {
[Fact]
public void WithIncludesItem() {
var set = new HashSet<int> { 1, 2, 3 };
var result = set.With(4);

result.ShouldContain(4);
result.Count.ShouldBe(4);
}

[Fact]
public void WithoutExcludesItem() {
var set = new HashSet<int> { 1, 2, 3 };
var result = set.Without(2);

result.ShouldNotContain(2);
result.Count.ShouldBe(2);
}
}
33 changes: 33 additions & 0 deletions Chickensoft.Collections/src/extensions/HashSetEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Chickensoft.Collections;

using System.Collections.Generic;

/// <summary>
/// <see cref="HashSet{T}" /> extensions.
/// </summary>
public static class HashSetEx {
/// <summary>
/// Creates a copy of the hash set with the specified item included.
/// </summary>
/// <typeparam name="T">Type of the item.</typeparam>
/// <param name="set">Hash set.</param>
/// <param name="item">Item to include.</param>
/// <returns>A new hash set.</returns>
public static HashSet<T> With<T>(this HashSet<T> set, T item) {
var copy = new HashSet<T>(set) { item };
return copy;
}

/// <summary>
/// Creates a copy of the hash set with the specified item excluded.
/// </summary>
/// <typeparam name="T">Type of the item.</typeparam>
/// <param name="set">Hash set.</param>
/// <param name="item">Item to exclude.</param>
/// <returns>A new hash set.</returns>
public static HashSet<T> Without<T>(this HashSet<T> set, T item) {
var copy = new HashSet<T>(set);
copy.Remove(item);
return copy;
}
}
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
},
"sdk": {
"rollForward": "major",
"version": "9.0.100-rc.1.24452.12"
"version": "8.0.401"
}
}
3 changes: 2 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
],
"groupName": "all dependencies",
"groupSlug": "all-deps",
"automerge": true
"automerge": true,
"allowedVersions": "!/preview/"
},
{
"matchPackagePrefixes": [
Expand Down

0 comments on commit 8408554

Please sign in to comment.