doc/CCHeap: reorder conversion functions

This commit is contained in:
Glen Mével 2024-07-26 22:00:19 +02:00
parent 02ac5bd78a
commit 6bd5d3aacf
2 changed files with 78 additions and 78 deletions

View file

@ -89,21 +89,11 @@ module type S = sig
(** {2 Conversions} *) (** {2 Conversions} *)
val to_list : t -> elt list
(** Return the elements of the heap, in no particular order. *)
val to_list_sorted : t -> elt list
(** Return the elements in increasing order.
@since 1.1 *)
val add_list : t -> elt list -> t val add_list : t -> elt list -> t
(** Add the elements of the list to the heap. An element occurring several (** Add the elements of the list to the heap. An element occurring several
times will be added that many times to the heap. times will be added that many times to the heap.
@since 0.16 *) @since 0.16 *)
val of_list : elt list -> t
(** [of_list l] is [add_list empty l]. Complexity: [O(n log n)]. *)
val add_iter : t -> elt iter -> t val add_iter : t -> elt iter -> t
(** Like {!add_list}. (** Like {!add_list}.
@since 2.8 *) @since 2.8 *)
@ -112,6 +102,12 @@ module type S = sig
(** Like {!add_list}. (** Like {!add_list}.
@since 2.8 *) @since 2.8 *)
val add_gen : t -> elt gen -> t
(** @since 0.16 *)
val of_list : elt list -> t
(** [of_list l] is [add_list empty l]. Complexity: [O(n log n)]. *)
val of_iter : elt iter -> t val of_iter : elt iter -> t
(** Build a heap from a given [iter]. Complexity: [O(n log n)]. (** Build a heap from a given [iter]. Complexity: [O(n log n)].
@since 2.8 *) @since 2.8 *)
@ -120,6 +116,12 @@ module type S = sig
(** Build a heap from a given [Seq.t]. Complexity: [O(n log n)]. (** Build a heap from a given [Seq.t]. Complexity: [O(n log n)].
@since 2.8 *) @since 2.8 *)
val of_gen : elt gen -> t
(** Build a heap from a given [gen]. Complexity: [O(n log n)]. *)
val to_list : t -> elt list
(** Return the elements of the heap, in no particular order. *)
val to_iter : t -> elt iter val to_iter : t -> elt iter
(** Return a [iter] of the elements of the heap. (** Return a [iter] of the elements of the heap.
@since 2.8 *) @since 2.8 *)
@ -128,6 +130,13 @@ module type S = sig
(** Return a [Seq.t] of the elements of the heap. (** Return a [Seq.t] of the elements of the heap.
@since 2.8 *) @since 2.8 *)
val to_gen : t -> elt gen
(** Return a [gen] of the elements of the heap. *)
val to_list_sorted : t -> elt list
(** Return the elements in increasing order.
@since 1.1 *)
val to_iter_sorted : t -> elt iter val to_iter_sorted : t -> elt iter
(** Iterate on the elements, in increasing order. (** Iterate on the elements, in increasing order.
@since 2.8 *) @since 2.8 *)
@ -136,15 +145,6 @@ module type S = sig
(** Iterate on the elements, in increasing order. (** Iterate on the elements, in increasing order.
@since 2.8 *) @since 2.8 *)
val add_gen : t -> elt gen -> t
(** @since 0.16 *)
val of_gen : elt gen -> t
(** Build a heap from a given [gen]. Complexity: [O(n log n)]. *)
val to_gen : t -> elt gen
(** Return a [gen] of the elements of the heap. *)
val to_tree : t -> elt ktree val to_tree : t -> elt ktree
(** Return a [ktree] of the elements of the heap. *) (** Return a [ktree] of the elements of the heap. *)
@ -283,24 +283,7 @@ module Make (E : PARTIAL_ORD) : S with type elt = E.t = struct
(** {2 Conversions} *) (** {2 Conversions} *)
let to_list h =
let rec aux acc h =
match h with
| E -> acc
| N (_, x, l, r) -> x :: aux (aux acc l) r
in
aux [] h
let to_list_sorted heap =
let rec recurse acc h =
match take h with
| None -> List.rev acc
| Some (h', x) -> recurse (x :: acc) h'
in
recurse [] heap
let add_list h l = List.fold_left add h l let add_list h l = List.fold_left add h l
let of_list l = add_list empty l
let add_iter h i = let add_iter h i =
let h = ref h in let h = ref h in
@ -312,8 +295,24 @@ module Make (E : PARTIAL_ORD) : S with type elt = E.t = struct
Seq.iter (fun x -> h := insert x !h) seq; Seq.iter (fun x -> h := insert x !h) seq;
!h !h
let rec add_gen h g =
match g () with
| None -> h
| Some x -> add_gen (add h x) g
let of_list l = add_list empty l
let of_iter i = add_iter empty i let of_iter i = add_iter empty i
let of_seq seq = add_seq empty seq let of_seq seq = add_seq empty seq
let of_gen g = add_gen empty g
let to_list h =
let rec aux acc h =
match h with
| E -> acc
| N (_, x, l, r) -> x :: aux (aux acc l) r
in
aux [] h
let to_iter h k = iter k h let to_iter h k = iter k h
let to_seq h = let to_seq h =
@ -326,28 +325,6 @@ module Make (E : PARTIAL_ORD) : S with type elt = E.t = struct
in in
aux [ h ] aux [ h ]
let to_iter_sorted heap =
let rec recurse h k =
match take h with
| None -> ()
| Some (h', x) ->
k x;
recurse h' k
in
fun k -> recurse heap k
let rec to_seq_sorted h () =
match take h with
| None -> Seq.Nil
| Some (h', x) -> Seq.Cons (x, to_seq_sorted h')
let rec add_gen h g =
match g () with
| None -> h
| Some x -> add_gen (add h x) g
let of_gen g = add_gen empty g
let to_gen h = let to_gen h =
let stack = Stack.create () in let stack = Stack.create () in
Stack.push h stack; Stack.push h stack;
@ -365,6 +342,29 @@ module Make (E : PARTIAL_ORD) : S with type elt = E.t = struct
in in
next next
let to_list_sorted heap =
let rec recurse acc h =
match take h with
| None -> List.rev acc
| Some (h', x) -> recurse (x :: acc) h'
in
recurse [] heap
let to_iter_sorted heap =
let rec recurse h k =
match take h with
| None -> ()
| Some (h', x) ->
k x;
recurse h' k
in
fun k -> recurse heap k
let rec to_seq_sorted h () =
match take h with
| None -> Seq.Nil
| Some (h', x) -> Seq.Cons (x, to_seq_sorted h')
let rec to_tree h () = let rec to_tree h () =
match h with match h with
| E -> `Nil | E -> `Nil

View file

@ -93,21 +93,11 @@ module type S = sig
(** {2 Conversions} *) (** {2 Conversions} *)
val to_list : t -> elt list
(** [to_list h] returns the elements of the heap [h], in no particular order. *)
val to_list_sorted : t -> elt list
(** [to_list_sorted h] returns the elements of the heap [h] in increasing order.
@since 1.1 *)
val add_list : t -> elt list -> t val add_list : t -> elt list -> t
(** [add_list h l] adds the elements of the list [l] into the heap [h]. (** [add_list h l] adds the elements of the list [l] into the heap [h].
An element occurring several times will be added that many times to the heap. An element occurring several times will be added that many times to the heap.
@since 0.16 *) @since 0.16 *)
val of_list : elt list -> t
(** [of_list l] is [add_list empty l]. Complexity: [O(n log n)]. *)
val add_iter : t -> elt iter -> t val add_iter : t -> elt iter -> t
(** [add_iter h iter] is like {!add_list}. (** [add_iter h iter] is like {!add_list}.
@since 2.8 *) @since 2.8 *)
@ -117,6 +107,13 @@ module type S = sig
Renamed from [add_std_seq] since 3.0. Renamed from [add_std_seq] since 3.0.
@since 3.0 *) @since 3.0 *)
val add_gen : t -> elt gen -> t
(** [add_gen h gen] adds the gen [gen] to the heap [h].
@since 0.16 *)
val of_list : elt list -> t
(** [of_list l] is [add_list empty l]. Complexity: [O(n log n)]. *)
val of_iter : elt iter -> t val of_iter : elt iter -> t
(** [of_iter iter] builds a heap from a given [iter]. Complexity: [O(n log n)]. (** [of_iter iter] builds a heap from a given [iter]. Complexity: [O(n log n)].
@since 2.8 *) @since 2.8 *)
@ -126,6 +123,12 @@ module type S = sig
Renamed from [of_seq] since 3.0. Renamed from [of_seq] since 3.0.
@since 3.0 *) @since 3.0 *)
val of_gen : elt gen -> t
(** [of_gen gen] builds a heap from a given [gen]. Complexity: [O(n log n)]. *)
val to_list : t -> elt list
(** [to_list h] returns the elements of the heap [h], in no particular order. *)
val to_iter : t -> elt iter val to_iter : t -> elt iter
(** [to_iter h] returns a [iter] of the elements of the heap [h]. (** [to_iter h] returns a [iter] of the elements of the heap [h].
@since 2.8 *) @since 2.8 *)
@ -135,6 +138,13 @@ module type S = sig
Renamed from [to_std_seq] since 3.0. Renamed from [to_std_seq] since 3.0.
@since 3.0 *) @since 3.0 *)
val to_gen : t -> elt gen
(** [to_gen h] returns a [gen] of the elements of the heap [h]. *)
val to_list_sorted : t -> elt list
(** [to_list_sorted h] returns the elements of the heap [h] in increasing order.
@since 1.1 *)
val to_iter_sorted : t -> elt iter val to_iter_sorted : t -> elt iter
(** [to_iter_sorted h] returns a [iter] by iterating on the elements of [h], (** [to_iter_sorted h] returns a [iter] by iterating on the elements of [h],
in increasing order. in increasing order.
@ -146,16 +156,6 @@ module type S = sig
Renamed from [to_std_seq_sorted] since 3.0. Renamed from [to_std_seq_sorted] since 3.0.
@since 3.0 *) @since 3.0 *)
val add_gen : t -> elt gen -> t
(** [add_gen h gen] adds the gen [gen] to the heap [h].
@since 0.16 *)
val of_gen : elt gen -> t
(** [of_gen gen] builds a heap from a given [gen]. Complexity: [O(n log n)]. *)
val to_gen : t -> elt gen
(** [to_gen h] returns a [gen] of the elements of the heap [h]. *)
val to_tree : t -> elt ktree val to_tree : t -> elt ktree
(** [to_tree h] returns a [ktree] of the elements of the heap [h]. *) (** [to_tree h] returns a [ktree] of the elements of the heap [h]. *)