Skip to content

Commit ef7e6d2

Browse files
xperiandriclaude
andcommitted
fixup! feat: HashSet and ISet modules with the vocabulary of the FSharp.Core Set module
Follows the reworked ADR 0001 (D2, D5, D12) and the review of #29: - `HashSet`, `ISet` and their `Option` and `Tuple` modules are the root modules of their files. The `HashSet<'T>` abbreviation moves to `HashSet/HashSetTypes.fs`, which needs no signature file. The files that open `System.Collections.Generic` open `FSharp.Collections.Immutable` after it, so that `HashSet` names the abbreviation, not the mutable set of the BCL. - `CustomComparerHashSet` (D12) creates hash sets with an equality comparer: its members `empty`, `ofArray`, `ofList`, `ofSeq`, `singleton` and `builder` have the names and the compiled names of the module functions. `HashSet.forComparer` creates it. The struct has an explicit private field and constructor, because a primary constructor cannot check its argument: the constructor throws `ArgumentNullException` for a `null` comparer, and so does `forComparer`. The signature reveals the field, as F# requires of a struct (FS0314). The default value of the struct compares with `EqualityComparer<'T>.Default`. - `emptyWithComparer`, `ofSeqWithComparer`, `builderWithComparer` and `withComparer` are removed: `ofSeq` of the struct gives an existing hash set its comparer, and returns it when it has the comparer already. The creation functions of the module are the members of the struct with the default comparer, so that each has one implementation. - `HashSetCE` checks its comparer for `null` when it is created. - `HashSet.exists` and `forall` loop over the struct enumerator of the hash set and allocate nothing per call, where LINQ `Any` and `All` allocated 232 bytes, a delegate and the boxed enumerator. `ISet.exists` and `forall` call `Seq.exists` and `Seq.forall`, which allocate only the enumerator: 144 bytes instead of 232 for a hash set and 64 instead of 152 for a sorted set. `ISet.filter` keeps LINQ `Where`: `Seq.filter` allocates as many bytes and runs slower, and a list of the accepted elements allocates more for large sets (measured in Release, D5). - The complexity notes of `ISet` say that the complexity depends on the implementation of the set, and give it for `ImmutableHashSet` and, where it is the same, for `ImmutableSortedSet`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2ceb3e8 commit ef7e6d2

17 files changed

Lines changed: 1783 additions & 1556 deletions

‎src/FSharp.Collections.Immutable/FSharp.Collections.Immutable.fsproj‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<Compile Include="FlatList\FlatListBuilder.fs" />
3333
<Compile Include="FlatList\FlatListCE.fsi" />
3434
<Compile Include="FlatList\FlatListCE.fs" />
35+
<Compile Include="HashSet\HashSetTypes.fs" />
36+
<Compile Include="HashSet\CustomComparerHashSet.fsi" />
37+
<Compile Include="HashSet\CustomComparerHashSet.fs" />
3538
<Compile Include="HashSet\HashSet.fsi" />
3639
<Compile Include="HashSet\HashSet.fs" />
3740
<Compile Include="HashSet\HashSetOption.fsi" />
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace FSharp.Collections.Immutable
2+
3+
open System.Collections.Generic
4+
open System.Collections.Immutable
5+
// Opened after System.Collections.Generic, so that HashSet names the abbreviation of this library, not the mutable set
6+
open FSharp.Collections.Immutable
7+
8+
// A struct with a primary constructor cannot check its argument, so the field and the constructor are explicit (ADR 0001,
9+
// D12). The field is null only in the default value of the struct, which no constructor creates; the BCL treats a null
10+
// comparer as the default comparer, and the Comparer property says so.
11+
[<Struct; NoComparison; NoEquality>]
12+
type CustomComparerHashSet<'T> =
13+
14+
val private comparer : IEqualityComparer<'T> | null
15+
16+
new (comparer : IEqualityComparer<'T>)
17+
=
18+
checkNotNull (nameof comparer) comparer
19+
{ comparer = comparer }
20+
21+
// ---- Creation ----
22+
23+
[<CompiledName "Empty">]
24+
member this.empty : HashSet<'T> = ImmutableHashSet.Create<'T> this.Comparer
25+
26+
[<CompiledName "OfArray">]
27+
member this.ofArray (array : 'T array) : HashSet<'T> =
28+
checkNotNull (nameof array) array
29+
ImmutableHashSet.CreateRange<'T>(this.Comparer, array)
30+
31+
[<CompiledName "OfList">]
32+
member this.ofList (elements : 'T list) : HashSet<'T> =
33+
// Set.ofList dereferences a null list without a check; a null input throws ArgumentNullException here (D6)
34+
checkNotNull (nameof elements) elements
35+
let builder = ImmutableHashSet.CreateBuilder<'T> this.Comparer
36+
37+
for item in elements do
38+
builder.Add item |> ignore
39+
40+
builder.ToImmutable ()
41+
42+
[<CompiledName "OfSeq">]
43+
member this.ofSeq (elements : seq<'T>) : HashSet<'T> =
44+
checkNotNull (nameof elements) elements
45+
// A hash set gets the comparer through WithComparer, which returns the hash set itself for the same comparer
46+
ImmutableHashSet.CreateRange<'T>(this.Comparer, elements)
47+
48+
[<CompiledName "Singleton">]
49+
member this.singleton (value : 'T) : HashSet<'T> = ImmutableHashSet.Create<'T>(this.Comparer, value)
50+
51+
// ---- Builder ----
52+
53+
[<CompiledName "CreateBuilder">]
54+
member this.builder () : HashSet<'T>.Builder = ImmutableHashSet.CreateBuilder<'T> this.Comparer
55+
56+
// ---- Properties ----
57+
58+
member this.Comparer : IEqualityComparer<'T> =
59+
match this.comparer with
60+
| null -> EqualityComparer<'T>.Default :> IEqualityComparer<'T>
61+
| comparer -> comparer
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
namespace FSharp.Collections.Immutable
2+
3+
open System.Collections.Generic
4+
// Opened after System.Collections.Generic, so that HashSet names the abbreviation of this library, not the mutable set
5+
open FSharp.Collections.Immutable
6+
7+
/// <summary>Creates hash sets that compare their elements with an equality comparer (ADR 0001, D12).</summary>
8+
/// <remarks>
9+
/// <para>
10+
/// <see cref="M:FSharp.Collections.Immutable.HashSet.ForComparer``1(System.Collections.Generic.IEqualityComparer{``0})"/>
11+
/// creates the struct. Its members are the creation functions of <see cref="T:FSharp.Collections.Immutable.HashSet"/>
12+
/// under the same names, with the comparer applied, so that a call reads as one phrase.
13+
/// </para>
14+
/// <para>
15+
/// The struct holds only the comparer, so creating it allocates nothing. Its default value, which no constructor
16+
/// creates, compares with <see cref="P:System.Collections.Generic.EqualityComparer`1.Default"/>.
17+
/// </para>
18+
/// </remarks>
19+
/// <example id="customComparerHashSet-1">
20+
/// <code lang="fsharp">
21+
/// let names = (HashSet.forComparer StringComparer.OrdinalIgnoreCase).ofList [ "Ann"; "ANN"; "Bob" ]
22+
/// </code>
23+
/// Evaluates to a hash set of "Ann" and "Bob" that compares without case.
24+
/// </example>
25+
[<Struct; NoComparison; NoEquality>]
26+
type CustomComparerHashSet<'T> =
27+
28+
// A signature reveals the fields of a struct, private ones included (FS0314); null only in the default value
29+
val private comparer : IEqualityComparer<'T> | null
30+
31+
/// <summary>Creates the struct for an equality comparer.</summary>
32+
/// <param name="comparer">The equality comparer of the elements of the hash sets that the struct creates.</param>
33+
/// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="comparer"/> is <see langword="null"/>.</exception>
34+
new : comparer : IEqualityComparer<'T> -> CustomComparerHashSet<'T>
35+
36+
// ---- Creation ----
37+
38+
/// <summary>The empty hash set that compares with the comparer.</summary>
39+
/// <remarks>
40+
/// O(1). The member wraps
41+
/// <see cref="M:System.Collections.Immutable.ImmutableHashSet.Create``1(System.Collections.Generic.IEqualityComparer{``0})"/>.
42+
/// </remarks>
43+
[<CompiledName "Empty">]
44+
member empty : HashSet<'T>
45+
46+
/// <summary>Creates a hash set from the elements of an array, compared with the comparer.</summary>
47+
/// <param name="array">The input array.</param>
48+
/// <returns>
49+
/// The hash set of the elements that are distinct in the comparer; of the elements that the comparer considers equal,
50+
/// it holds the first one.
51+
/// </returns>
52+
/// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="array"/> is <see langword="null"/>.</exception>
53+
/// <remarks>
54+
/// O(n log n). Later changes to the array do not change the hash set. The member wraps
55+
/// <see cref="M:System.Collections.Immutable.ImmutableHashSet.CreateRange``1(System.Collections.Generic.IEqualityComparer{``0},System.Collections.Generic.IEnumerable{``0})"/>.
56+
/// </remarks>
57+
[<CompiledName "OfArray">]
58+
member ofArray : array : 'T array -> HashSet<'T>
59+
60+
/// <summary>Creates a hash set from the elements of a list, compared with the comparer.</summary>
61+
/// <param name="elements">The input list.</param>
62+
/// <returns>
63+
/// The hash set of the elements that are distinct in the comparer; of the elements that the comparer considers equal,
64+
/// it holds the first one.
65+
/// </returns>
66+
/// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="elements"/> is <see langword="null"/>.</exception>
67+
/// <remarks>O(n log n).</remarks>
68+
[<CompiledName "OfList">]
69+
member ofList : elements : 'T list -> HashSet<'T>
70+
71+
/// <summary>Creates a hash set from the elements of a sequence, compared with the comparer.</summary>
72+
/// <param name="elements">The input sequence, which can be a hash set with another comparer.</param>
73+
/// <returns>
74+
/// The hash set of the elements that are distinct in the comparer; of the elements that the comparer considers equal,
75+
/// it holds the first one.
76+
/// </returns>
77+
/// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="elements"/> is <see langword="null"/>.</exception>
78+
/// <remarks>
79+
/// <para>
80+
/// O(n log n). The member wraps
81+
/// <see cref="M:System.Collections.Immutable.ImmutableHashSet.CreateRange``1(System.Collections.Generic.IEqualityComparer{``0},System.Collections.Generic.IEnumerable{``0})"/>.
82+
/// </para>
83+
/// <para>
84+
/// A hash set gets the comparer, as
85+
/// <see cref="M:System.Collections.Immutable.ImmutableHashSet`1.WithComparer(System.Collections.Generic.IEqualityComparer{`0})"/>
86+
/// gives it: the member returns the hash set itself in O(1) when it compares with the comparer already, and otherwise a
87+
/// new hash set of its elements in the comparer.
88+
/// </para>
89+
/// </remarks>
90+
[<CompiledName "OfSeq">]
91+
member ofSeq : elements : seq<'T> -> HashSet<'T>
92+
93+
/// <summary>Creates a hash set of one element that compares with the comparer.</summary>
94+
/// <param name="value">The element.</param>
95+
/// <returns>The hash set of one element.</returns>
96+
/// <remarks>
97+
/// O(1). The member wraps
98+
/// <see cref="M:System.Collections.Immutable.ImmutableHashSet.Create``1(System.Collections.Generic.IEqualityComparer{``0},``0)"/>.
99+
/// </remarks>
100+
[<CompiledName "Singleton">]
101+
member singleton : value : 'T -> HashSet<'T>
102+
103+
// ---- Builder ----
104+
105+
/// <summary>Creates an empty builder that compares with the comparer.</summary>
106+
/// <returns>The builder.</returns>
107+
/// <remarks>
108+
/// O(1). The member wraps
109+
/// <see cref="M:System.Collections.Immutable.ImmutableHashSet.CreateBuilder``1(System.Collections.Generic.IEqualityComparer{``0})"/>.
110+
/// </remarks>
111+
[<CompiledName "CreateBuilder">]
112+
member builder : unit -> HashSet<'T>.Builder
113+
114+
// ---- Properties ----
115+
116+
/// <summary>The equality comparer of the elements of the hash sets that the struct creates.</summary>
117+
/// <remarks>
118+
/// O(1). The default value of the struct returns <see cref="P:System.Collections.Generic.EqualityComparer`1.Default"/>.
119+
/// </remarks>
120+
member Comparer : IEqualityComparer<'T>

0 commit comments

Comments
 (0)