|
| 1 | +//------------------------------------------------------------------------ |
| 2 | +// shims for things not yet implemented in Fable |
| 3 | +//------------------------------------------------------------------------ |
| 4 | + |
| 5 | +namespace System.Collections.Concurrent |
| 6 | + |
| 7 | +open System.Collections.Generic |
| 8 | + |
| 9 | +/// not actually thread safe, just an extension of Dictionary |
| 10 | +type ConcurrentDictionary<'Key, 'Value when 'Key: equality>(comparer: IEqualityComparer<'Key>) = |
| 11 | + inherit Dictionary<'Key, 'Value>(comparer) |
| 12 | + |
| 13 | + new () = |
| 14 | + let comparer = { |
| 15 | + new IEqualityComparer<'Key> with |
| 16 | + member __.GetHashCode(x) = x.GetHashCode() |
| 17 | + member __.Equals(x, y) = x.Equals(y) } |
| 18 | + ConcurrentDictionary(comparer) |
| 19 | + |
| 20 | + new (_concurrencyLevel: int, _capacity: int) = |
| 21 | + ConcurrentDictionary() |
| 22 | + new (_concurrencyLevel: int, comparer: IEqualityComparer<'Key>) = |
| 23 | + ConcurrentDictionary(comparer) |
| 24 | + new (_concurrencyLevel: int, _capacity: int, comparer: IEqualityComparer<'Key>) = |
| 25 | + ConcurrentDictionary(comparer) |
| 26 | + |
| 27 | + member x.TryAdd (key: 'Key, value: 'Value): bool = |
| 28 | + if x.ContainsKey(key) |
| 29 | + then false |
| 30 | + else x.Add(key, value); true |
| 31 | + |
| 32 | + member x.TryRemove (key: 'Key): bool * 'Value = |
| 33 | + match x.TryGetValue(key) with |
| 34 | + | true, v -> (x.Remove(key), v) |
| 35 | + | _ as res -> res |
| 36 | + |
| 37 | + member x.GetOrAdd (key: 'Key, valueFactory: 'Key -> 'Value): 'Value = |
| 38 | + match x.TryGetValue(key) with |
| 39 | + | true, v -> v |
| 40 | + | _ -> let v = valueFactory(key) in x.Add(key, v); v |
| 41 | + |
| 42 | + // member x.GetOrAdd (key: 'Key, value: 'Value): 'Value = |
| 43 | + // match x.TryGetValue(key) with |
| 44 | + // | true, v -> v |
| 45 | + // | _ -> let v = value in x.Add(key, v); v |
| 46 | + |
| 47 | + // member x.GetOrAdd<'Arg> (key: 'Key, valueFactory: 'Key * 'Arg -> 'Value, arg: 'Arg): 'Value = |
| 48 | + // match x.TryGetValue(key) with |
| 49 | + // | true, v -> v |
| 50 | + // | _ -> let v = valueFactory(key, arg) in x.Add(key, v); v |
| 51 | + |
| 52 | + // member x.TryUpdate (key: 'Key, value: 'Value, comparisonValue: 'Value): bool = |
| 53 | + // match x.TryGetValue(key) with |
| 54 | + // | true, v when v = comparisonValue -> x.[key] <- value; true |
| 55 | + // | _ -> false |
| 56 | + |
| 57 | + // member x.AddOrUpdate (key: 'Key, value: 'Value, updateFactory: 'Key * 'Value -> 'Value): 'Value = |
| 58 | + // match x.TryGetValue(key) with |
| 59 | + // | true, v -> let v = updateFactory(key, v) in x.[key] <- v; v |
| 60 | + // | _ -> let v = value in x.Add(key, v); v |
| 61 | + |
| 62 | + // member x.AddOrUpdate (key: 'Key, valueFactory: 'Key -> 'Value, updateFactory: 'Key * 'Value -> 'Value): 'Value = |
| 63 | + // match x.TryGetValue(key) with |
| 64 | + // | true, v -> let v = updateFactory(key, v) in x.[key] <- v; v |
| 65 | + // | _ -> let v = valueFactory(key) in x.Add(key, v); v |
| 66 | + |
| 67 | + // member x.AddOrUpdate (key: 'Key, valueFactory: 'Key * 'Arg -> 'Value, updateFactory: 'Key * 'Arg * 'Value -> 'Value, arg: 'Arg): 'Value = |
| 68 | + // match x.TryGetValue(key) with |
| 69 | + // | true, v -> let v = updateFactory(key, arg, v) in x.[key] <- v; v |
| 70 | + // | _ -> let v = valueFactory(key, arg) in x.Add(key, v); v |
0 commit comments