diff --git a/leftistheap.ml b/leftistheap.ml index 0c1c5675..a5fc3e34 100644 --- a/leftistheap.ml +++ b/leftistheap.ml @@ -23,7 +23,7 @@ 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. *) -(** {1 Leftist Heaps *) +(** {1 Leftist Heaps} *) (** Polymorphic implementation, following Okasaki *) @@ -36,11 +36,16 @@ and 'a tree = | Empty | Node of int * 'a * 'a tree * 'a tree -let empty ~leq = +let empty_with ~leq = { tree = Empty; leq; } +let empty = + { tree = Empty; + leq = (fun x y -> x <= y); + } + let is_empty heap = match heap.tree with | Empty -> true diff --git a/leftistheap.mli b/leftistheap.mli index b01f4741..7565509a 100644 --- a/leftistheap.mli +++ b/leftistheap.mli @@ -23,16 +23,19 @@ 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. *) -(** {1 Leftist Heaps *) +(** {1 Leftist Heaps} *) (** Polymorphic implementation, following Okasaki *) type 'a t (** Heap containing values of type 'a *) -val empty : leq:('a -> 'a -> bool) -> 'a t - (** Empty heap. The function is used to check whether - the first element is smaller than the second. *) +val empty_with : leq:('a -> 'a -> bool) -> 'a t + (** Empty heap. The function is used to check whether the first element is + smaller than the second. *) + +val empty : 'a t + (** Empty heap using Pervasives.compare *) val is_empty : _ t -> bool (** Is the heap empty? *) diff --git a/tests/test_leftistheap.ml b/tests/test_leftistheap.ml index e5f124c4..ee93eb56 100644 --- a/tests/test_leftistheap.ml +++ b/tests/test_leftistheap.ml @@ -3,7 +3,7 @@ open OUnit -let empty = Leftistheap.empty ~leq:(fun i j -> i <= j) +let empty = Leftistheap.empty let test1 () = let h = Leftistheap.of_seq empty (Sequence.of_list [5;3;4;1;42;0]) in @@ -27,6 +27,7 @@ let rec is_sorted l = match l with | [] -> true | x::((y::_) as l') -> x <= y && is_sorted l' +(* extract the content of the heap into a list *) let extract_list heap = let rec recurse acc h = if Leftistheap.is_empty h @@ -43,6 +44,7 @@ let test_sort () = let l = Sequence.to_rev_list (Sequence.take n (Sequence.random_int n)) in (* put elements into a heap *) let h = Leftistheap.of_seq empty (Sequence.of_list l) in + OUnit.assert_equal n (Leftistheap.size h); let l' = extract_list h in OUnit.assert_bool "sorted" (is_sorted l'); ()