namespace SimpleHeap
(**********************************************
Roughly transliterated from Haskell's Data.Heap, and then
simplified as much as possible.
Haskell's Data.Heap bears the following copyright notice,
this is probably a derivative work:
Copyright (c) 2008-2009, Stephan Friedrichs
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Oh, yeah, I'm Joe Bowers, at http://www.joe-bowers.com
***********************************************)
/// A *very* simple heap, for keeping things sorted :)
module Heap =
type private 'Value HeapRecord = { value:'Value
left:'Value Heap
right:'Value Heap
rightrank: int
size: int }
and public 'Value Heap = private | Empty
| Heap of 'Value HeapRecord
/// Length of the Rightmost path through the heap
let private rightrank = function | Empty -> 0
| Heap h -> h.rightrank
(***************************************************)
let public empty = Empty
let public isEmpty = function | Empty -> true
| _ -> false
let public singleton v = Heap { value=v; left=Empty; right=Empty; rightrank=1; size=1 }
// Consider caching this?
/// Total number of values in the queue
let rec public size = function | Empty -> 0
| Heap h -> h.size
// Where everything actually happens. We make unions by
// - Picking the lowest of the two (presumably well-formed) heaps as our head
// - Merging to the current right
// - using the smallest branch for the new right
//
// The hope being that we're more or less balanced in the long run.
let rec public union h1 h2 =
match (h1,h2) with
| (heap, Empty) | (Empty, heap) -> heap
| (Heap heap1, Heap heap2) ->
let (lowheap, highheap) =
if heap1.value < heap2.value then
(heap1, heap2)
else (heap2, heap1)
let newrootvalue = lowheap.value
let kid1 = lowheap.left
let kid2 = union lowheap.right (Heap highheap)
let (newrank, rightkid, leftkid) =
let kid1_rank = rightrank kid1
let kid2_rank = rightrank kid2
if kid1_rank < kid2_rank then
(kid1_rank + 1, kid1, kid2)
else (kid2_rank + 1, kid2, kid1)
let newsize = 1 + (size rightkid) + (size leftkid)
Heap { value=newrootvalue;
right=rightkid;
left=leftkid;
rightrank=newrank;
size=newsize }
let public insert heap v = union (singleton v) heap
/// the lowest heap value, along with the rest of the heap
/// (Or None for an empty heap)
let public pop = function | Empty -> None
| Heap h -> Some (h.value, union h.left h.right)
/// Just the lowest value, leaving the heap unchanged.
let public peek = function | Empty -> None
| Heap h -> Some h.value