44
55namespace System.Collections
66
7- module Generic =
8-
9- type Queue < 'T >() =
10- let xs = ResizeArray< 'T>()
11-
12- member _.Clear () = xs.Clear()
13-
14- member _.Enqueue ( item : 'T ) =
15- xs.Add( item)
16-
17- member _.Dequeue () =
18- let item = xs.Item( 0 )
19- xs.RemoveAt( 0 )
20- item
21-
22- interface System.Collections.IEnumerable with
23- member _.GetEnumerator () =
24- ( xs.GetEnumerator() :> System.Collections.IEnumerator)
25-
26- interface System.Collections.Generic.IEnumerable< 'T> with
27- member _.GetEnumerator () =
28- xs.GetEnumerator()
29-
307module Immutable =
318 open System.Collections .Generic
329
@@ -35,7 +12,7 @@ module Immutable =
3512 static member CreateBuilder () = ResizeArray< 'T>()
3613
3714 [<Sealed>]
38- type ImmutableHashSet < 'T >( values : 'T seq ) =
15+ type ImmutableHashSet < 'T when 'T: equality >( values : 'T seq ) =
3916 let xs = HashSet< 'T>( values)
4017
4118 static member Create < 'T >( values ) = ImmutableHashSet< 'T>( values)
@@ -48,61 +25,63 @@ module Immutable =
4825
4926 member _.Union ( values : seq < 'T >) =
5027 let copy = HashSet< 'T>( xs)
51- copy.UnionWith( values)
28+ // copy.UnionWith(values)
29+ for value in values do
30+ copy.Add( value) |> ignore
5231 ImmutableHashSet< 'T>( copy)
5332
5433 member _.Overlaps ( values : seq < 'T >) =
5534 // xs.Overlaps(values)
5635 values |> Seq.exists ( fun x -> xs.Contains( x))
5736
58- interface System.Collections.IEnumerable with
59- member _.GetEnumerator () =
60- ( xs.GetEnumerator() :> System.Collections.IEnumerator)
61-
6237 interface IEnumerable< 'T> with
6338 member _.GetEnumerator () =
6439 xs.GetEnumerator()
6540
41+ interface System.Collections.IEnumerable with
42+ member _.GetEnumerator () =
43+ ( xs.GetEnumerator() :> System.Collections.IEnumerator)
44+
6645 [<Sealed>]
67- type ImmutableDictionary < 'Key , 'Value when 'Key : equality >( xs : Dictionary < 'Key , 'Value >) =
68- static member Create ( comparer : IEqualityComparer < 'Key >) =
69- ImmutableDictionary< 'Key , 'Value >( Dictionary( comparer))
46+ type ImmutableDictionary < 'K , 'V when 'K : equality >( xs : Dictionary < 'K , 'V >) =
47+ static member Create ( comparer : IEqualityComparer < 'K >) =
48+ ImmutableDictionary< 'K , 'V >( Dictionary( comparer))
7049
71- static member CreateRange ( items : IEnumerable < KeyValuePair < 'Key , 'Value >>) =
72- let xs = Dictionary< 'Key , 'Value >()
50+ static member CreateRange ( items : IEnumerable < KeyValuePair < 'K , 'V >>) =
51+ let xs = Dictionary< 'K , 'V >()
7352 for pair in items do
7453 xs.Add( pair.Key, pair.Value)
75- ImmutableDictionary< 'Key , 'Value >( xs)
54+ ImmutableDictionary< 'K , 'V >( xs)
7655
7756 static member Empty =
78- ImmutableDictionary< 'Key , 'Value >( Dictionary())
57+ ImmutableDictionary< 'K , 'V >( Dictionary())
7958
8059 member _.IsEmpty = xs.Count = 0
81- member _.Item with get ( key : 'Key ): 'Value = xs[ key]
82- member _.ContainsKey ( key : 'Key ) = xs.ContainsKey( key)
60+ member _.Item with get ( key : 'K ): 'V = xs[ key]
61+ member _.ContainsKey ( key : 'K ) = xs.ContainsKey( key)
8362
84- member _.Add ( key : 'Key , value : 'Value ) =
85- let copy = Dictionary< 'Key , 'Value >( xs)
63+ member _.Add ( key : 'K , value : 'V ) =
64+ let copy = Dictionary< 'K , 'V >( xs)
8665 copy.Add( key, value)
87- ImmutableDictionary< 'Key , 'Value >( copy)
66+ ImmutableDictionary< 'K , 'V >( copy)
8867
89- member _.SetItem ( key : 'Key , value : 'Value ) =
90- let copy = Dictionary< 'Key , 'Value >( xs)
68+ member _.SetItem ( key : 'K , value : 'V ) =
69+ let copy = Dictionary< 'K , 'V >( xs)
9170 copy[ key] <- value
92- ImmutableDictionary< 'Key , 'Value >( copy)
71+ ImmutableDictionary< 'K , 'V >( copy)
9372
94- member _.TryGetValue ( key : 'Key ): bool * 'Value =
73+ member _.TryGetValue ( key : 'K ): bool * 'V =
9574 match xs.TryGetValue( key) with
9675 | true , v -> ( true , v)
9776 | false , v -> ( false , v)
9877
99- interface System.Collections. IEnumerable with
78+ interface IEnumerable< KeyValuePair < 'K , 'V >> with
10079 member _.GetEnumerator () =
101- ( xs.GetEnumerator() :> System.Collections.IEnumerator )
80+ xs.GetEnumerator()
10281
103- interface IEnumerable< KeyValuePair < 'Key , 'Value >> with
82+ interface System.Collections. IEnumerable with
10483 member _.GetEnumerator () =
105- xs.GetEnumerator()
84+ ( xs.GetEnumerator() :> System.Collections.IEnumerator )
10685
10786module Concurrent =
10887 open System.Collections .Generic
@@ -116,92 +95,95 @@ module Concurrent =
11695 member _.Clear () = xs.Clear()
11796 member _.ToArray () = xs.ToArray()
11897
119- interface System.Collections.IEnumerable with
120- member _.GetEnumerator () =
121- ( xs.GetEnumerator() :> System.Collections.IEnumerator)
122-
12398 interface IEnumerable< 'T> with
12499 member _.GetEnumerator () =
125100 xs.GetEnumerator()
126101
102+ interface System.Collections.IEnumerable with
103+ member _.GetEnumerator () =
104+ ( xs.GetEnumerator() :> System.Collections.IEnumerator)
105+
127106 // not thread safe, just a Dictionary // TODO: threaded implementation
128107 [<AllowNullLiteral>]
129- type ConcurrentDictionary < 'Key , 'Value >( comparer : IEqualityComparer < 'Key >) =
108+ type ConcurrentDictionary < 'K , 'V >( comparer : IEqualityComparer < 'K >) =
130109 let xs = Dictionary( comparer)
131110
132111 new () =
133- ConcurrentDictionary< 'Key , 'Value >( EqualityComparer.Default)
112+ ConcurrentDictionary< 'K , 'V >( EqualityComparer.Default)
134113 new ( _concurrencyLevel : int , _capacity : int ) =
135- ConcurrentDictionary< 'Key , 'Value >()
136- new ( _concurrencyLevel : int , comparer : IEqualityComparer < 'Key >) =
137- ConcurrentDictionary< 'Key , 'Value >( comparer)
138- new ( _concurrencyLevel : int , _capacity : int , comparer : IEqualityComparer < 'Key >) =
139- ConcurrentDictionary< 'Key , 'Value >( comparer)
114+ ConcurrentDictionary< 'K , 'V >()
115+ new ( _concurrencyLevel : int , comparer : IEqualityComparer < 'K >) =
116+ ConcurrentDictionary< 'K , 'V >( comparer)
117+ new ( _concurrencyLevel : int , _capacity : int , comparer : IEqualityComparer < 'K >) =
118+ ConcurrentDictionary< 'K , 'V >( comparer)
140119
120+ member _.Comparer = comparer
141121 member _.Keys = xs.Keys
142122 member _.Values = xs.Values
143123
144124 member _.Item
145- with get ( key : 'Key ): 'Value = xs[ key]
146- and set ( key : 'Key ) ( value : 'Value ) = xs[ key] <- value
125+ with get ( key : 'K ): 'V = xs[ key]
126+ and set ( key : 'K ) ( value : 'V ) = xs[ key] <- value
147127
148128 member _.Clear () = xs.Clear()
149- member _.ContainsKey ( key : 'Key ) = xs.ContainsKey( key)
129+ member _.ContainsKey ( key : 'K ) = xs.ContainsKey( key)
150130
151- member _.TryGetValue ( key : 'Key ): bool * 'Value =
131+ member _.TryGetValue ( key : 'K ): bool * 'V =
152132 match xs.TryGetValue( key) with
153133 | true , v -> ( true , v)
154134 | false , v -> ( false , v)
155135
156- member _.TryAdd ( key : 'Key , value : 'Value ): bool =
136+ member _.TryAdd ( key : 'K , value : 'V ): bool =
157137 if xs.ContainsKey( key)
158138 then false
159139 else xs.Add( key, value); true
160140
161- member _.TryRemove ( key : 'Key ): bool * 'Value =
141+ member _.TryRemove ( key : 'K ): bool * 'V =
162142 match xs.TryGetValue( key) with
163143 | true , v -> ( xs.Remove( key), v)
164144 | _ as res -> res
165145
166- member _.GetOrAdd ( key : 'Key , value : 'Value ): 'Value =
146+ member _.GetOrAdd ( key : 'K , value : 'V ): 'V =
167147 match xs.TryGetValue( key) with
168148 | true , v -> v
169149 | _ -> let v = value in xs.Add( key, v); v
170150
171- member _.GetOrAdd ( key : 'Key , valueFactory : System.Func < 'Key , 'Value >): 'Value =
151+ member _.GetOrAdd ( key : 'K , valueFactory : System.Func < 'K , 'V >): 'V =
172152 match xs.TryGetValue( key) with
173153 | true , v -> v
174154 | _ -> let v = valueFactory.Invoke( key) in xs.Add( key, v); v
175155
176- // member _.GetOrAdd<'Arg> (key: 'Key , valueFactory: 'Key * 'Arg -> 'Value , arg: 'Arg): 'Value =
156+ // member _.GetOrAdd<'Arg> (key: 'K , valueFactory: 'K * 'Arg -> 'V , arg: 'Arg): 'V =
177157 // match xs.TryGetValue(key) with
178158 // | true, v -> v
179159 // | _ -> let v = valueFactory(key, arg) in xs.Add(key, v); v
180160
181- member _.TryUpdate ( key : 'Key , value : 'Value , comparisonValue : 'Value ): bool =
182- match xs.TryGetValue( key) with
183- | true , v when Unchecked.equals v comparisonValue -> xs[ key] <- value; true
184- | _ -> false
161+ member _.TryUpdate ( key : 'K , value : 'V , comparisonValue : 'V ): bool =
162+ // match xs.TryGetValue(key) with
163+ // | true, v when Unchecked.equals v comparisonValue -> xs[key] <- value; true
164+ // | _ -> false
165+ xs[ key] <- value
166+ true
185167
186- member _.AddOrUpdate ( key : 'Key , value : 'Value , updateFactory : System.Func < 'Key , 'Value , 'Value >): 'Value =
168+ member _.AddOrUpdate ( key : 'K , value : 'V , updateFactory : System.Func < 'K , 'V , 'V >): 'V =
187169 match xs.TryGetValue( key) with
188170 | true , v -> let v = updateFactory.Invoke( key, v) in xs[ key] <- v; v
189171 | _ -> let v = value in xs.Add( key, v); v
190172
191- // member _.AddOrUpdate (key: 'Key , valueFactory: 'Key -> 'Value , updateFactory: 'Key * 'Value -> 'Value ): 'Value =
173+ // member _.AddOrUpdate (key: 'K , valueFactory: 'K -> 'V , updateFactory: 'K * 'V -> 'V ): 'V =
192174 // match xs.TryGetValue(key) with
193175 // | true, v -> let v = updateFactory(key, v) in xs[key] <- v; v
194176 // | _ -> let v = valueFactory(key) in xs.Add(key, v); v
195177
196- // member _.AddOrUpdate (key: 'Key , valueFactory: 'Key * 'Arg -> 'Value , updateFactory: 'Key * 'Arg * 'Value -> 'Value , arg: 'Arg): 'Value =
178+ // member _.AddOrUpdate (key: 'K , valueFactory: 'K * 'Arg -> 'V , updateFactory: 'K * 'Arg * 'V -> 'V , arg: 'Arg): 'V =
197179 // match xs.TryGetValue(key) with
198180 // | true, v -> let v = updateFactory(key, arg, v) in xs[key] <- v; v
199181 // | _ -> let v = valueFactory(key, arg) in xs.Add(key, v); v
200182
201- interface System.Collections. IEnumerable with
183+ interface IEnumerable< KeyValuePair < 'K , 'V >> with
202184 member _.GetEnumerator () =
203- ( xs.GetEnumerator() :> System.Collections.IEnumerator )
185+ xs.GetEnumerator()
204186
205- interface IEnumerable< KeyValuePair < 'Key , 'Value >> with
187+ interface System.Collections. IEnumerable with
206188 member _.GetEnumerator () =
207- xs.GetEnumerator()
189+ ( xs.GetEnumerator() :> System.Collections.IEnumerator )
0 commit comments