Hi, I want to use pairing_heap in core_kernel library to implement a min heap. However, there are some code I don’t quite understand.
In addition to the typical add
, pop
, top
functions pairing_heap provided, there is also a module Elt
. I guess people want to use this module to access and manipulate elements in queue more conveniently. However, I don’t know how to define my own type to meet corresponding function signature.
module Elt : sig
type 'a t [@@deriving sexp_of]
(** [value_exn t] returns the value in the heap controlled by this token if the
value is still in the heap, and raises otherwise. *)
val value_exn : 'a t -> 'a
end
Let’s say I have a type as below
type node = {
name : string;
mutable order : int;
}
The corresponding comparison function is
let node_cmp t1 t2 = Int.compare t1.order t2.order;;
Then I can create a heap from list
let heap = Heap.of_list [{name = "a"; order = 0};
{name = "b"; order = 1};
{name = "c"; order = 2}]
~cmp:node_cmp
Now I want to update order of node b
to 3
. I noticed there is a function with signature
(** [update t token v] is shorthand for [remove t token; add_removable t v]. *)
val update : 'a t -> 'a Elt.t -> 'a -> 'a Elt.t
'a
in this case is type node
apparently. However, how can I get a token with type 'a Elt.t
?
Any help would be appreciated, thank you in advance!