X-Git-Url: http://git.euphorik.ch/?p=master-thesis.git;a=blobdiff_plain;f=Parasitemia%2FParasitemiaCore%2FHeap.fs;h=350164bf3ebddac11f3c228f9c6357a0b9dbf10e;hp=32887a230b6cf5cdbde185dde5f441e13661e3f7;hb=b87b35b922551f122228df1fd9c530bbb807935a;hpb=2e3cd07dd099944059ef5e7a7f5ef57ffe3d677b diff --git a/Parasitemia/ParasitemiaCore/Heap.fs b/Parasitemia/ParasitemiaCore/Heap.fs index 32887a2..350164b 100644 --- a/Parasitemia/ParasitemiaCore/Heap.fs +++ b/Parasitemia/ParasitemiaCore/Heap.fs @@ -2,16 +2,16 @@ open System.Collections.Generic -let inline private parent (i: int) : int = (i - 1) / 2 -let inline private left (i: int) : int = 2 * (i + 1) - 1 -let inline private right (i: int) : int = 2 * (i + 1) +let inline private parent (i : int) : int = (i - 1) / 2 +let inline private left (i : int) : int = 2 * (i + 1) - 1 +let inline private right (i : int) : int = 2 * (i + 1) [] type private Node<'k, 'v> = - val mutable key: 'k - val value: 'v + val mutable key : 'k + val value : 'v new (k, v) = { key = k; value = v } - override this.ToString () = sprintf "%A -> %A" this.key this.value + override this.ToString () = sprintf "%O -> %O" this.key this.value /// /// An heap min or max depending of the given key comparer. @@ -20,42 +20,34 @@ type private Node<'k, 'v> = type Heap<'k, 'v> (kComparer : IComparer<'k>) = let a = List>() - let rec heapUp (i: int) = + let rec heapUp (i : int) = let l, r = left i, right i // Is the left child greater than the parent? let mutable max = if l < a.Count && kComparer.Compare(a.[l].key, a.[i].key) > 0 then l else i // Is the right child greater than the parent and the left child? - if r < a.Count && kComparer.Compare(a.[r].key, a.[max].key) > 0 - then + if r < a.Count && kComparer.Compare(a.[r].key, a.[max].key) > 0 then max <- r // If a child is greater than the parent. - if max <> i - then + if max <> i then let tmp = a.[i] a.[i] <- a.[max] a.[max] <- tmp heapUp max // Check the integrity of the heap, use for debug only. - let rec checkIntegrity (i: int) : bool = + let rec checkIntegrity (i : int) : bool = let l, r = left i, right i let leftIntegrity = - if l < a.Count - then - if kComparer.Compare(a.[l].key, a.[i].key) > 0 - then false - else checkIntegrity l + if l < a.Count then + if kComparer.Compare(a.[l].key, a.[i].key) > 0 then false else checkIntegrity l else true let rightIntegrity = - if r < a.Count - then - if kComparer.Compare(a.[r].key, a.[i].key) > 0 - then false - else checkIntegrity r + if r < a.Count then + if kComparer.Compare(a.[r].key, a.[i].key) > 0 then false else checkIntegrity r else true leftIntegrity && rightIntegrity @@ -80,7 +72,7 @@ type Heap<'k, 'v> (kComparer : IComparer<'k>) = a.RemoveAt(a.Count - 1) heapUp 0 - member this.Add (key: 'k) (value: 'v) = + member this.Add (key : 'k) (value : 'v) = a.Add(Node(key, value)) let mutable i = a.Count - 1