gen interface in leftistheap

This commit is contained in:
Simon Cruanes 2014-06-13 21:57:47 +02:00
parent 89f4500fc2
commit 71bdc7667d
2 changed files with 24 additions and 0 deletions

View file

@ -29,6 +29,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
type 'a sequence = ('a -> unit) -> unit
type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist]
type 'a gen = unit -> 'a option
type 'a t = {
tree : 'a tree;
@ -158,3 +159,22 @@ let to_klist h =
`Cons (x, next (a :: b :: stack'))
in
next [h.tree]
let rec of_gen h g = match g () with
| None -> h
| Some x ->
of_gen (add h x) g
let to_gen h =
let stack = Stack.create () in
Stack.push h.tree stack;
let rec next () =
if Stack.is_empty stack
then None
else match Stack.pop stack with
| Empty -> next()
| Node (_, x, a, b) ->
Stack.push a stack;
Stack.push b stack;
Some x
in next

View file

@ -28,6 +28,7 @@ Polymorphic implementation, following Okasaki *)
type 'a sequence = ('a -> unit) -> unit
type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist]
type 'a gen = unit -> 'a option
type 'a t
(** Heap containing values of type 'a *)
@ -81,3 +82,6 @@ val to_seq : 'a t -> 'a sequence
val of_klist : 'a t -> 'a klist -> 'a t
val to_klist : 'a t -> 'a klist
val of_gen : 'a t -> 'a gen -> 'a t
val to_gen : 'a t -> 'a gen