3 open System.Collections.Generic
5 let inline private
parent (i
: int) : int = (i
- 1) / 2
6 let inline private
left (i
: int) : int = 2 * (i
+ 1) - 1
7 let inline private
right (i
: int) : int = 2 * (i
+ 1)
9 type private Node<'k, 'v
> = { key
: 'k; value : 'v
} with
10 override this.ToString () = sprintf
"%A -> %A" this.key
this.value
12 type Heap<'k, 'v
when 'k : comparison> () =
13 let a = List<Node<'k
, 'v>>()
15 let rec heapUp (i: int) =
16 let l, r = left i, right i
17 let mutable max = if l < a.Count && a.[l].key > a.[i].key then l else i
18 if r < a.Count && a.[r].key > a.[max].key
28 member this.Next () : 'k
* 'v =
29 let { key = key; value = value} = a.[0]
33 member this.Add (key: 'k
) (value
: 'v) =
34 a.Insert(0, { key = key; value = value})
37 member this.IsEmpty = a.Count = 0
38 member this.Max : 'k
= a.[0].key
39 member this.Clear () = a.Clear()