mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
minor update of Leftistheap
This commit is contained in:
parent
b6212bea7c
commit
d0bb88e365
3 changed files with 17 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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? *)
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue