Skip to content

Commit 474e768

Browse files
xperiandriclaude
andcommitted
feat: FrozenSet module with the vocabulary of the FSharp.Core Set module
Adds the `FrozenSet` module over `System.Collections.Frozen.FrozenSet<'T>` in `FrozenSet/FrozenSet.fsi` and `FrozenSet/FrozenSet.fs`: 37 functions, 39 on `net10.0`, in the canonical category order, each documented in the signature file, with the names, the argument order and the exceptions of the `Set` module. - A frozen set is built once, so the module offers creation, lookup, iteration, folds, conversions and the transformations of a whole set, but no `add`, `remove`, `clear`, builder or `withComparer`. Every transformation collects its result in a `HashSet` with the comparer of the result and freezes it once with `ToFrozenSet`, which takes such a hash set over without another copy. - The reading functions call the members of `FrozenSet` and pass the array behind `Items` to the FSharp.Core `Array` functions without a copy; `toFlatList` returns `Items` in O(1). - `filter`, `partition` and the set algebra keep the comparer of their input, the set algebra that of the first set; `intersect` and `union` hold the elements of the first set where both sets have equal ones. `map`, `choose` and `partitionWith` compare with the default comparer of the result type. `symmetricDifference` is an extra, because `FrozenSet` has no `SymmetricExcept`. - `intersectMany` reuses `Sets.reduce`: an intersection never grows, so its rebuilds at each step stay linear in total. `unionMany` folds every set into one hash set and freezes it once, because rebuilding a growing union at each step would take quadratic time. - On `net10.0`, `alternateLookup` and `tryAlternateLookup` wrap `GetAlternateLookup` and `TryGetAlternateLookup`. The alternate type comes first, as in `FrozenSet.alternateLookup<int, _> set`. Their documentation points span lookups to the BCL methods, because an F# generic function cannot be instantiated with `ReadOnlySpan<char>`. - The module coexists with the BCL class `FrozenSet`: `FrozenSet.ofSeq` and `FrozenSet.ToFrozenSet` both resolve, whichever namespace is opened last. - `tryGetValue`, `choose` and `tryAlternateLookup` use `voption`, and `partition` and `partitionWith` struct tuples; the `Option` and `Tuple` namespaces repeat them with options and reference tuples. - `frozenSet { }` collects its elements in a `ResizeArray` and freezes them once; `FrozenSetCE` created with an equality comparer builds frozen sets that compare with it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f36abd8 commit 474e768

13 files changed

Lines changed: 1593 additions & 0 deletions

‎src/FSharp.Collections.Immutable/ComputationExpressions.fs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ module FSharp.Collections.Immutable.ComputationExpressions
55

66
let flatList = FlatListCE ()
77

8+
[<GeneralizableValue>]
9+
let frozenSet<'T> = FrozenSetCE<'T>()
10+
811
[<GeneralizableValue>]
912
let hashSet<'T> = HashSetCE<'T>()

‎src/FSharp.Collections.Immutable/ComputationExpressions.fsi‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ module FSharp.Collections.Immutable.ComputationExpressions
2020
/// </example>
2121
val flatList : FlatListCE
2222

23+
/// <summary>
24+
/// Builds a frozen set that compares with <see cref="P:System.Collections.Generic.EqualityComparer`1.Default"/>.
25+
/// </summary>
26+
/// <remarks>
27+
/// The value is a type function, so that one value builds frozen sets of every element type. A
28+
/// <see cref="T:FSharp.Collections.Immutable.FrozenSetCE`1"/> created with an equality comparer builds frozen sets that
29+
/// compare with it. The expression freezes its elements once, after it has collected all of them.
30+
/// </remarks>
31+
/// <example id="frozenSet-1">
32+
/// <code lang="fsharp">
33+
/// let squares = frozenSet {
34+
/// for i in -2..2 do
35+
/// yield i * i
36+
/// yield! [ 4; 9 ]
37+
/// }
38+
/// </code>
39+
/// Evaluates to a frozen set of 0, 1, 4 and 9.
40+
/// </example>
41+
/// <example id="frozenSet-2">
42+
/// <code lang="fsharp">
43+
/// let names = FrozenSetCE StringComparer.OrdinalIgnoreCase { "Ann"; "ANN"; "Bob" }
44+
/// </code>
45+
/// Evaluates to a frozen set of "Ann" and "Bob" that ignores case.
46+
/// </example>
47+
[<GeneralizableValue>]
48+
val frozenSet<'T> : FrozenSetCE<'T>
49+
2350
/// <summary>
2451
/// Builds a hash set that compares with <see cref="P:System.Collections.Generic.EqualityComparer`1.Default"/>.
2552
/// </summary>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@
6767
<Compile Include="HashSet\ISetTuple.fs" />
6868
<Compile Include="HashSet\HashSetCE.fsi" />
6969
<Compile Include="HashSet\HashSetCE.fs" />
70+
<Compile Include="FrozenSet\FrozenSet.CustomComparer.fsi">
71+
<DependentUpon>FrozenSet.fsi</DependentUpon>
72+
</Compile>
73+
<Compile Include="FrozenSet\FrozenSet.CustomComparer.fs">
74+
<DependentUpon>FrozenSet.fs</DependentUpon>
75+
</Compile>
76+
<Compile Include="FrozenSet\FrozenSet.fsi" />
77+
<Compile Include="FrozenSet\FrozenSet.fs" />
78+
<Compile Include="FrozenSet\FrozenSetOption.fsi" />
79+
<Compile Include="FrozenSet\FrozenSetOption.fs" />
80+
<Compile Include="FrozenSet\FrozenSetTuple.fsi" />
81+
<Compile Include="FrozenSet\FrozenSetTuple.fs" />
82+
<Compile Include="FrozenSet\FrozenSetCE.fsi" />
83+
<Compile Include="FrozenSet\FrozenSetCE.fs" />
7084
<Compile Include="Stack.fs" />
7185
<Compile Include="ImmutableList.fs" />
7286
<Compile Include="Queue.fs" />
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
namespace FSharp.Collections.Immutable
2+
3+
open System
4+
open System.Collections.Frozen
5+
open System.Collections.Generic
6+
7+
// A struct with a primary constructor cannot check its argument, so the field and the constructor are explicit (ADR 0001,
8+
// D12). The field is null only in the default value of the struct, which no constructor creates; the BCL treats a null
9+
// comparer as the default comparer, and the Comparer property says so.
10+
[<Struct; NoComparison; NoEquality>]
11+
type CustomComparerFrozenSet<'T> =
12+
13+
val private comparer : IEqualityComparer<'T> | null
14+
15+
new (comparer : IEqualityComparer<'T>)
16+
=
17+
checkNotNull (nameof comparer) comparer
18+
{ comparer = comparer }
19+
20+
// ---- Creation ----
21+
22+
// Create returns the shared empty frozen set for the default comparer and, unlike ToFrozenSet, collects nothing first
23+
[<CompiledName "Empty">]
24+
member this.empty : FrozenSet<'T> = FrozenSet.Create<'T>(this.Comparer, ReadOnlySpan<'T>.Empty)
25+
26+
[<CompiledName "OfArray">]
27+
member this.ofArray (array : 'T array) : FrozenSet<'T> =
28+
checkNotNull (nameof array) array
29+
array.ToFrozenSet this.Comparer
30+
31+
[<CompiledName "OfList">]
32+
member this.ofList (elements : 'T list) : FrozenSet<'T> =
33+
// Set.ofList dereferences a null list without a check; a null input throws ArgumentNullException here (D6)
34+
checkNotNull (nameof elements) elements
35+
elements.ToFrozenSet this.Comparer
36+
37+
// The BCL keeps the first of the elements that the comparer finds equal, for every size of the span (the tests pin it)
38+
[<CompiledName "OfReadOnlySpan">]
39+
member this.ofReadOnlySpan (span : ReadOnlySpan<'T>) : FrozenSet<'T> = FrozenSet.Create<'T>(this.Comparer, span)
40+
41+
[<CompiledName "OfSeq">]
42+
member this.ofSeq (elements : seq<'T>) : FrozenSet<'T> =
43+
// A default flat list given as a sequence would fail with InvalidOperationException in the BCL call (ADR 0001, D6)
44+
checkNotNullOrDefaultSeq (nameof elements) elements
45+
// ToFrozenSet returns a frozen set whose comparer equals the comparer, and copies any other sequence
46+
elements.ToFrozenSet this.Comparer
47+
48+
// Create takes a read-only span; F# converts the span of the argument
49+
[<CompiledName "OfSpan">]
50+
member this.ofSpan (span : Span<'T>) : FrozenSet<'T> = FrozenSet.Create<'T>(this.Comparer, span)
51+
52+
[<CompiledName "Singleton">]
53+
member this.singleton (value : 'T) : FrozenSet<'T> = [| value |].ToFrozenSet this.Comparer
54+
55+
// ---- Properties ----
56+
57+
member this.Comparer : IEqualityComparer<'T> =
58+
match this.comparer with
59+
| null -> EqualityComparer<'T>.Default :> IEqualityComparer<'T>
60+
| comparer -> comparer
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
namespace FSharp.Collections.Immutable
2+
3+
open System
4+
open System.Collections.Frozen
5+
open System.Collections.Generic
6+
7+
/// <summary>
8+
/// Creates frozen sets that compare their elements with an equality comparer (ADR 0001, D12).
9+
/// </summary>
10+
/// <remarks>
11+
/// <para>
12+
/// <see cref="M:FSharp.Collections.Immutable.FrozenSet.ForComparer``1(System.Collections.Generic.IEqualityComparer{``0})"/>
13+
/// creates the struct. Its members are the creation functions of <see cref="T:FSharp.Collections.Immutable.FrozenSet"/>
14+
/// under the same names, with the comparer applied, so that a call reads as one phrase. A frozen set has no builder, so
15+
/// the struct has no <c>builder</c> member.
16+
/// </para>
17+
/// <para>
18+
/// The struct holds only the comparer, so creating it allocates nothing. Its default value, which no constructor
19+
/// creates, compares with <see cref="P:System.Collections.Generic.EqualityComparer`1.Default"/>.
20+
/// </para>
21+
/// </remarks>
22+
/// <example id="customComparerFrozenSet-1">
23+
/// <code lang="fsharp">
24+
/// let names = (FrozenSet.forComparer StringComparer.OrdinalIgnoreCase).ofList [ "Ann"; "ANN"; "Bob" ]
25+
/// </code>
26+
/// Evaluates to a frozen set of "Ann" and "Bob" that compares without case.
27+
/// </example>
28+
[<Struct; NoComparison; NoEquality>]
29+
type CustomComparerFrozenSet<'T> =
30+
31+
// A signature reveals the fields of a struct, private ones included (FS0314); null only in the default value
32+
val private comparer : IEqualityComparer<'T> | null
33+
34+
/// <summary>Creates the struct for an equality comparer.</summary>
35+
/// <param name="comparer">The equality comparer of the elements of the frozen sets that the struct creates.</param>
36+
/// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="comparer"/> is <see langword="null"/>.</exception>
37+
new : comparer : IEqualityComparer<'T> -> CustomComparerFrozenSet<'T>
38+
39+
// ---- Creation ----
40+
41+
/// <summary>
42+
/// The empty frozen set that compares with the comparer.
43+
/// </summary>
44+
/// <remarks>
45+
/// O(1). The member wraps
46+
/// <see cref="M:System.Collections.Frozen.FrozenSet.Create``1(System.Collections.Generic.IEqualityComparer{``0},System.ReadOnlySpan{``0})"/>,
47+
/// which returns <see cref="P:System.Collections.Frozen.FrozenSet`1.Empty"/> for
48+
/// <see cref="P:System.Collections.Generic.EqualityComparer`1.Default"/>.
49+
/// </remarks>
50+
[<CompiledName "Empty">]
51+
member empty : FrozenSet<'T>
52+
53+
/// <summary>
54+
/// Creates a frozen set from the elements of an array, compared with the comparer.
55+
/// </summary>
56+
/// <param name="array">The input array.</param>
57+
/// <returns>
58+
/// The frozen set of the elements that are distinct in the comparer; of the elements that the comparer considers equal,
59+
/// it holds the first one.
60+
/// </returns>
61+
/// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="array"/> is <see langword="null"/>.</exception>
62+
/// <remarks>
63+
/// O(n). Later changes to the array do not change the frozen set. The member wraps
64+
/// <see cref="M:System.Collections.Frozen.FrozenSet.ToFrozenSet``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IEqualityComparer{``0})"/>.
65+
/// </remarks>
66+
[<CompiledName "OfArray">]
67+
member ofArray : array : 'T array -> FrozenSet<'T>
68+
69+
/// <summary>
70+
/// Creates a frozen set from the elements of a list, compared with the comparer.
71+
/// </summary>
72+
/// <param name="elements">The input list.</param>
73+
/// <returns>
74+
/// The frozen 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+
/// O(n).
80+
/// </remarks>
81+
[<CompiledName "OfList">]
82+
member ofList : elements : 'T list -> FrozenSet<'T>
83+
84+
/// <summary>
85+
/// Creates a frozen set from the elements of a read-only span, compared with the comparer.
86+
/// </summary>
87+
/// <param name="span">The input span.</param>
88+
/// <returns>
89+
/// The frozen set of the elements that are distinct in the comparer; of the elements that the comparer considers equal,
90+
/// it holds the first one. An empty span gives an empty frozen set.
91+
/// </returns>
92+
/// <remarks>
93+
/// O(n). Later changes to the memory under the span do not change the frozen set. The member wraps
94+
/// <see cref="M:System.Collections.Frozen.FrozenSet.Create``1(System.Collections.Generic.IEqualityComparer{``0},System.ReadOnlySpan{``0})"/>.
95+
/// </remarks>
96+
[<CompiledName "OfReadOnlySpan">]
97+
member ofReadOnlySpan : span : ReadOnlySpan<'T> -> FrozenSet<'T>
98+
99+
/// <summary>
100+
/// Creates a frozen set from the elements of a sequence, compared with the comparer.
101+
/// </summary>
102+
/// <param name="elements">The input sequence, which can be a frozen set with another comparer.</param>
103+
/// <returns>
104+
/// The frozen set of the elements that are distinct in the comparer; of the elements that the comparer considers equal,
105+
/// it holds the first one.
106+
/// </returns>
107+
/// <exception cref="T:System.ArgumentNullException">Thrown when <paramref name="elements"/> is <see langword="null"/> or a default flat list.</exception>
108+
/// <remarks>
109+
/// <para>
110+
/// O(n). The member wraps
111+
/// <see cref="M:System.Collections.Frozen.FrozenSet.ToFrozenSet``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IEqualityComparer{``0})"/>.
112+
/// </para>
113+
/// <para>
114+
/// A frozen set gets the comparer: the member returns the frozen set itself in O(1) when its comparer equals the
115+
/// comparer, and otherwise a new frozen set of its elements in the comparer.
116+
/// </para>
117+
/// </remarks>
118+
[<CompiledName "OfSeq">]
119+
member ofSeq : elements : seq<'T> -> FrozenSet<'T>
120+
121+
/// <summary>
122+
/// Creates a frozen set from the elements of a span, compared with the comparer.
123+
/// </summary>
124+
/// <param name="span">The input span.</param>
125+
/// <returns>
126+
/// The frozen set of the elements that are distinct in the comparer; of the elements that the comparer considers equal,
127+
/// it holds the first one. An empty span gives an empty frozen set.
128+
/// </returns>
129+
/// <remarks>
130+
/// O(n). Later changes to the memory under the span do not change the frozen set. The member converts the span to a
131+
/// read-only span and wraps
132+
/// <see cref="M:System.Collections.Frozen.FrozenSet.Create``1(System.Collections.Generic.IEqualityComparer{``0},System.ReadOnlySpan{``0})"/>.
133+
/// </remarks>
134+
[<CompiledName "OfSpan">]
135+
member ofSpan : span : Span<'T> -> FrozenSet<'T>
136+
137+
/// <summary>
138+
/// Creates a frozen set of one element that compares with the comparer.
139+
/// </summary>
140+
/// <param name="value">The element.</param>
141+
/// <returns>The frozen set of one element.</returns>
142+
/// <remarks>
143+
/// O(1).
144+
/// </remarks>
145+
[<CompiledName "Singleton">]
146+
member singleton : value : 'T -> FrozenSet<'T>
147+
148+
// ---- Properties ----
149+
150+
/// <summary>
151+
/// The equality comparer of the elements of the frozen sets that the struct creates.
152+
/// </summary>
153+
/// <remarks>
154+
/// O(1). The default value of the struct returns <see cref="P:System.Collections.Generic.EqualityComparer`1.Default"/>.
155+
/// </remarks>
156+
member Comparer : IEqualityComparer<'T>

0 commit comments

Comments
 (0)