-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from chickensoft-games/feat/extensions
feat: hash set extensions
- Loading branch information
Showing
4 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Chickensoft.Collections.Tests/src/extensions/HashSetExTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,6 @@ | |
}, | ||
"sdk": { | ||
"rollForward": "major", | ||
"version": "9.0.100-rc.1.24452.12" | ||
"version": "8.0.401" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters