-
Notifications
You must be signed in to change notification settings - Fork 52
/
Map.fs
148 lines (121 loc) · 4.79 KB
/
Map.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// $begin{copyright}
//
// This file is part of WebSharper
//
// Copyright (c) 2008-2018 IntelliFactory
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You may
// obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// $end{copyright}
namespace WebSharper.Collections
open System.Collections
open System.Collections.Generic
open WebSharper
open WebSharper.JavaScript
open WebSharper.Collections
open System.Runtime.InteropServices
module T = BalancedTree
[<JavaScript>]
[<AutoOpen>]
module private MapUtil =
let fromSeq(s: seq<_>) =
let a : Pair<_,_> [] =
s
|> Seq.rev
|> Seq.distinctBy fst
|> Seq.map (fun (k, v) ->
{ Key = k; Value = v }
)
|> Array.ofSeq
Array.sortInPlace a
T.OfSorted a
/// Implements a proxy for the F# Map type.
[<Name "FSharpMap">]
[<Proxy(typeof<Map<_,_>>)>]
type internal FSharpMap<'K,'V when 'K : comparison>
(tree: T.Tree<Pair<'K,'V>>) =
new (s: seq<_>) = new FSharpMap<_,_>(fromSeq s)
member this.Tree = tree
member this.Add(k: 'K, v: 'V) : Map<'K,'V> =
As (FSharpMap<'K,'V>(tree |> T.Add {Key=k; Value=v}))
member this.Change(k: 'K, f: 'V option -> 'V option) : Map<'K,'V> =
As (FSharpMap<'K,'V>(tree |> T.Change {Key=k; Value=JS.Undefined} (fun x -> f (x |> Option.map (fun p -> p.Value)) |> Option.map (fun v -> {Key=k; Value=v}))))
member this.ContainsKey k =
tree |> T.Contains {Key=k; Value = JS.Undefined}
member this.TryGetValue (k: 'K, [<Out>] r: byref<'V>) =
match this.TryFind k with
| Some v ->
r <- v
true
| _ ->
false
[<Name("Count")>]
member this.Count = T.Count tree
member this.IsEmpty = T.IsEmpty tree
member this.Item
with get (k: 'K) : 'V =
match this.TryFind k with
| Some v ->v
| None ->
raise (KeyNotFoundException())
member this.Remove(k: 'K) : Map<'K,'V> =
As (FSharpMap(tree |> T.Remove {Key=k; Value=JS.Undefined}))
member this.TryFind(k: 'K) =
tree
|> T.TryFind {Key=k; Value=JS.Undefined}
|> Option.map (fun kv -> kv.Value)
[<Name("GetEnumerator")>]
member this.GetEnumerator() =
let s =
T.Ascend tree
|> Seq.map (fun kv ->
new KeyValuePair<_,_>(kv.Key, kv.Value))
s.GetEnumerator()
override this.GetHashCode() =
hash (Seq.toArray this)
override this.Equals(other) =
let other = As<FSharpMap<'K,'V>> other
this.Count = other.Count
&& Seq.forall2 ( = ) this other
interface System.IComparable with
member this.CompareTo other =
Seq.compareWith (fun x y ->
compare (As<Pair<'K,'V>> x) (As<Pair<'K,'V>> y))
this
(As<Map<'K,'V>> other)
interface IEnumerable with
[<JavaScript(false)>]
member this.GetEnumerator() = X<_>
interface IEnumerable<KeyValuePair<'K,'V>> with
[<JavaScript(false)>]
member this.GetEnumerator() = X<_>
member this.Keys : System.Collections.Generic.ICollection<'K> =
Seq.map (fun kvp -> kvp.Key) (T.Ascend this.Tree)
|> ResizeArray
:> _
member this.Values : System.Collections.Generic.ICollection<'V> =
Seq.map (fun kvp -> kvp.Value) (T.Ascend this.Tree)
|> ResizeArray
:> _
interface ICollection<KeyValuePair<'K,'V>> with
member this.IsReadOnly = true
[<JavaScript(false)>]
member this.Count = X<int>
member this.Add(p) = failwith "Map values cannot be mutated."
member this.Clear() = failwith "Map values cannot be mutated."
member this.Contains(p) =
let mutable v = JS.Undefined
if this.TryGetValue(p.Key, &v) then Unchecked.equals v p.Value else false
member this.CopyTo(arr: KeyValuePair<'K,'V>[], index: int) =
(Seq.toArray this).CopyTo(arr, index)
member this.Remove(p) = failwith "Map values cannot be mutated."