@@ -29,14 +29,6 @@ module Auto =
29
29
| _ -> value <- konst
30
30
true
31
31
member _.Count = source.Count
32
- // if typeof<'TKey>.IsValueType then
33
- // if typeof<'TKey> = typeof<bool> then 2
34
- // else
35
- // let s = sizeof<'TKey>
36
- // if s < 4 then pown 2 (sizeof<'TKey> * 8)
37
- // else -1 // infinity
38
- // elif typeof<'TKey> = typeof<unit> then 1
39
- // else -1
40
32
member _.ContainsKey ( _key : 'TKey ) = true
41
33
member _.Contains ( item : KeyValuePair < 'TKey , 'TValue >) =
42
34
match source.TryGetValue item.Key with
@@ -67,10 +59,10 @@ module Dict =
67
59
open System.Collections .ObjectModel
68
60
open Auto
69
61
70
- /// <summary>Creates a conceptually infinite dictionary containing the same value for all possible keys .</summary>
71
- /// <param name="source ">The value for all possible keys.</param>
72
- let initInfinite < 'TKey , 'TValue when 'TKey : equality > ( source : 'TValue ) : IDictionary < 'TKey , 'TValue > = new DefaultableDict < 'TKey , 'TValue >( source, Dictionary < 'TKey , 'TValue > ())
73
- let initHybrid < 'TKey , 'TValue > ( konst : 'TValue ) ( source : IDictionary < 'TKey , 'TValue >) : IDictionary < 'TKey , 'TValue > = new DefaultableDict< 'TKey, 'TValue>( konst, source)
62
+ /// <summary>Creates a defaultable dictionary.</summary>
63
+ /// <param name="konst ">The value for all missing keys.</param>
64
+ /// <param name=" source">The source dictionary.</param>
65
+ let initWithDefault < 'TKey , 'TValue > ( konst : 'TValue ) ( source : IDictionary < 'TKey , 'TValue >) : IDictionary < 'TKey , 'TValue > = new DefaultableDict< 'TKey, 'TValue>( konst, source)
74
66
75
67
#if ! FABLE_ COMPILER
76
68
open System.Linq
@@ -128,18 +120,13 @@ module Dict =
128
120
///
129
121
/// <returns>The mapped dictionary.</returns>
130
122
let map mapper ( source : IDictionary < 'Key , 'T >) =
131
- match source with
132
- | :? DefaultableDict< 'Key, 'T> as s ->
133
- let dct = DefaultableDict< 'Key, 'U> ( mapper s.DefaultValue, Dictionary< 'Key, 'U> ())
134
- let dct = dct :> IDictionary< 'Key, 'U>
135
- for KeyValue( k, v) in source do
136
- dct.Add ( k, mapper v)
137
- dct
138
- | _ ->
139
- let dct = Dictionary< 'Key, 'U> ()
140
- for KeyValue( k, v) in source do
141
- dct.Add ( k, mapper v)
142
- dct :> IDictionary< 'Key, 'U>
123
+ let dct =
124
+ match source with
125
+ | :? DefaultableDict< 'Key, 'T> as s -> initWithDefault ( mapper s.DefaultValue) ( Dictionary< 'Key, 'U> ())
126
+ | _ -> Dictionary< 'Key, 'U> () :> IDictionary< 'Key, 'U>
127
+ for KeyValue( k, v) in source do
128
+ dct.Add ( k, mapper v)
129
+ dct
143
130
144
131
/// <summary>Creates a Dictionary value from a pair of Dictionaries, using a function to combine them.</summary>
145
132
/// <remarks>Keys that are not present on both dictionaries are dropped.</remarks>
@@ -149,38 +136,41 @@ module Dict =
149
136
///
150
137
/// <returns>The combined dictionary.</returns>
151
138
let map2 mapper ( source1 : IDictionary < 'Key , 'T1 >) ( source2 : IDictionary < 'Key , 'T2 >) =
152
- let map k1 k2 =
139
+ let map () =
153
140
let dct = Dictionary< 'Key, 'U> ()
154
141
let f = OptimizedClosures.FSharpFunc<_, _, _>. Adapt mapper
155
- for k in set source1.Keys + set source2.Keys do
156
- match tryGetValue k source1, tryGetValue k source2, k1, k2 with
157
- | Some vx, Some vy, _ , _
158
- | None , Some vy, Some vx, _
159
- | Some vx, None , _ , Some vy -> dct.Add ( k, f.Invoke ( vx, vy))
160
- | _ , _ , _ , _ -> ()
142
+ let keys = Seq.append source1.Keys source2.Keys |> Seq.distinct
143
+ for k in keys do
144
+ match tryGetValue k source1, tryGetValue k source2 with
145
+ | Some vx, Some vy -> dct.Add ( k, f.Invoke ( vx, vy))
146
+ | _ , _ -> ()
161
147
dct :> IDictionary< 'Key, 'U>
162
148
match source1, source2 with
163
- | (:? DefaultableDict< 'Key, 'T1> as s1), (:? DefaultableDict< 'Key, 'T2> as s2) -> initHybrid ( mapper s1.DefaultValue s2.DefaultValue) ( map ( Some s1.DefaultValue) ( Some s2.DefaultValue))
164
- | (:? DefaultableDict< 'Key, 'T1> as s1), _ -> map ( Some s1.DefaultValue) None
165
- | _, (:? DefaultableDict< 'Key, 'T2> as s2) -> map None ( Some s2.DefaultValue)
166
- | _, _ -> map None None
149
+ | (:? DefaultableDict< 'Key, 'T1> as s1), (:? DefaultableDict< 'Key, 'T2> as s2) -> initWithDefault ( mapper s1.DefaultValue s2.DefaultValue) ( map ())
150
+ | _, _ -> map ()
167
151
168
152
/// <summary>Combines values from three dictionaries using mapping function.</summary>
169
153
/// <remarks>Keys that are not present on every dictionary are dropped.</remarks>
170
- /// <param name="mapping ">The mapping function.</param>
154
+ /// <param name="mapper ">The mapping function.</param>
171
155
/// <param name="source1">First input dictionary.</param>
172
156
/// <param name="source2">Second input dictionary.</param>
173
157
/// <param name="source3">Third input dictionary.</param>
174
158
///
175
159
/// <returns>The mapped dictionary.</returns>
176
- let map3 mapping ( source1 : IDictionary < 'Key , 'T1 >) ( source2 : IDictionary < 'Key , 'T2 >) ( source3 : IDictionary < 'Key , 'T3 >) =
177
- let dct = Dictionary< 'Key, 'U> ()
178
- let f = OptimizedClosures.FSharpFunc<_,_,_,_>. Adapt mapping
179
- for KeyValue( k, vx) in source1 do
180
- match tryGetValue k source2, tryGetValue k source3 with
181
- | Some vy, Some vz -> dct.Add ( k, f.Invoke ( vx, vy, vz))
182
- | _ , _ -> ()
183
- dct :> IDictionary< 'Key, 'U>
160
+ let map3 mapper ( source1 : IDictionary < 'Key , 'T1 >) ( source2 : IDictionary < 'Key , 'T2 >) ( source3 : IDictionary < 'Key , 'T3 >) =
161
+ let map () =
162
+ let dct = Dictionary< 'Key, 'U> ()
163
+ let f = OptimizedClosures.FSharpFunc<_,_,_,_>. Adapt mapper
164
+ let keys = source1.Keys |> Seq.append source2.Keys |> Seq.append source3.Keys |> Seq.distinct
165
+ for k in keys do
166
+ match tryGetValue k source1, tryGetValue k source2, tryGetValue k source3 with
167
+ | Some vx, Some vy, Some vz -> dct.Add ( k, f.Invoke ( vx, vy, vz))
168
+ | _ , _ , _ -> ()
169
+ dct :> IDictionary< 'Key, 'U>
170
+ match source1, source2, source3 with
171
+ | (:? DefaultableDict< 'Key, 'T1> as s1), (:? DefaultableDict< 'Key, 'T2> as s2), (:? DefaultableDict< 'Key, 'T3> as s3) ->
172
+ initWithDefault ( mapper s1.DefaultValue s2.DefaultValue s3.DefaultValue) ( map ())
173
+ | _, _, _ -> map ()
184
174
185
175
/// <summary>Applies given function to each value of the given dictionary.</summary>
186
176
/// <param name="chooser">The mapping function.</param>
@@ -223,17 +213,9 @@ module Dict =
223
213
for KeyValue( k, v ) in source1 do d.[ k] <- v
224
214
for KeyValue( k, v') in source2 do d.[ k] <- match d.TryGetValue k with true , v -> f.Invoke ( v, v') | _ -> v'
225
215
d :> IDictionary< 'Key, 'Value>
226
- // let combineWithKonst source konst =
227
- // let d = Dictionary<'Key,'Value> ()
228
- // for KeyValue(k, v) in source do d.[k] <- combiner v konst
229
- // d :> IDictionary<'Key,'Value>
230
- // let combineKonstWith konst source =
231
- // let d = Dictionary<'Key,'Value> ()
232
- // for KeyValue(k, v) in source do d.[k] <- combiner konst v
233
- // d :> IDictionary<'Key,'Value>
234
216
match source1, source2 with
235
- | (:? DefaultableDict< 'Key, 'Value> as s1), (:? DefaultableDict< 'Key, 'Value> as s2) -> initHybrid ( combiner s1.DefaultValue s2.DefaultValue) ( combine())
236
- | (:? DefaultableDict< 'Key, 'Value> as s), _ | _, (:? DefaultableDict< 'Key, 'Value> as s) -> initHybrid s.DefaultValue ( combine())
217
+ | (:? DefaultableDict< 'Key, 'Value> as s1), (:? DefaultableDict< 'Key, 'Value> as s2) -> initWithDefault ( combiner s1.DefaultValue s2.DefaultValue) ( combine())
218
+ | (:? DefaultableDict< 'Key, 'Value> as s), _ | _, (:? DefaultableDict< 'Key, 'Value> as s) -> initWithDefault s.DefaultValue ( combine())
237
219
| s, empty | empty, s when empty.Count = 0 -> s
238
220
| _, _ -> combine()
239
221
0 commit comments