ocaml中优先级队列的函数

ocaml中优先级队列的函数,ocaml,priority-queue,Ocaml,Priority Queue,ocaml中是否有一个库,我可以使用它创建优先级队列并处理它 我查过这个“http://holgerarnold.net/software/ocaml/doc/base/PriorityQueue.Make.html" 但是它没有任何关于如何使用这些命令的示例。包含的OCaml电池在名为的模块中有一个多态优先级队列。您只需将元素添加到空堆中即可使用它,以此类推 Jane Stree Core在名为的模块中有一个看起来更奇特的优先级队列 更新: 简·斯特里的堆芯真是太棒了。描述它的一种方法是一个堆

ocaml中是否有一个库,我可以使用它创建优先级队列并处理它

我查过这个“http://holgerarnold.net/software/ocaml/doc/base/PriorityQueue.Make.html"
但是它没有任何关于如何使用这些命令的示例。

包含的OCaml电池在名为的模块中有一个多态优先级队列。您只需将元素添加到空堆中即可使用它,以此类推

Jane Stree Core在名为的模块中有一个看起来更奇特的优先级队列

更新:

简·斯特里的堆芯真是太棒了。描述它的一种方法是一个堆有两个接口。第一个接口是有序值的集合,其最小元素可以在固定时间内定位,并在日志时间内删除。第二个接口将堆视为容器(“堆元素”)的集合,其中包含有序值。如果您愿意显式地处理这些容器,则可以更快地执行一些堆操作

下面是一个非常简单的示例,它使用堆(第一个接口)对列表进行排序:

let heapsort l =
    let heap = Core.Std.Heap.create compare in
    List.iter (fun x -> ignore (Core.Std.Heap.push heap x)) l;
    let rec extract () =
        match Core.Std.Heap.pop heap with
        | None -> []
        | Some x -> x :: extract ()
    in
    extract ()
(这段代码有点做作;它只是演示如何将值放入堆中并将其取出。)

下面是一个运行此代码的示例(在具有核心支持的OCaml顶级中):

##使用“sort.ml”;;
val heapsort:'列表->'列表=
#希普索尔[3;1;4;1;5;9];;
-:int list=[1;1;3;4;5;9]
# 

这里有一个稍微大一点的关于核心堆的教程

open Core.Std

(* A heap only expects a comparsion function on its elements. Use
  polymorphic compare if you just want something tham makes sense most
  of the time *)

let pq = Heap.create compare

let reverse_pq = Heap.create ~min_size:10 (Fn.flip compare)

(* The optional min size argument is there for optimization purposes. If you
   know that your heap will grow past a certain size you can allocate the array
   of that size in advance to save copying/resizing later on since the heap is
   array based *)

let () = 
  let random_list = List.init 10 ~f:(fun _ -> Random.int 10) in
  (* core wraps values inserted into the heap in the type 'el heap_el
    where 'el is the type of elements in your heap *)
  let heap_el = Heap.push pq (Random.int 10) in
  (* this gives you O(1) existence check in the heap: *)
  let x = Heap.heap_el_mem pq heap_el in (* true in O(1) *)
  let value_in_el = Heap.heap_el_get_el heap_el in
  (* now standard heap stuff, insert a list into a heap *)
  random_list |> List.iter ~f:(Fn.compose ignore (Heap.push pq));
  (* now drain the heap and get a list in sorted order, for reverse
  order you'd us reverse_pq *)
  let sorted_list = 
    let rec loop acc =
      match Heap.pop pq with
      | None -> acc
      | Some e -> loop (e::acc)
    in loop [] in
  printf "Sorted: %s\n" 
    (Sexp.to_string_hum (List.sexp_of_t Int.sexp_of_t sorted_list))

不要犹豫使用核心。它将使您的OCaml更加愉快。欢迎提出更多问题。

OCaml手册的一章从实现优先级队列的代码示例开始。这是我的优先队列实现,由于整个实现包含25行,因此易于使用和理解。

我已经检查了它们,如果我知道如何使用它们,这将非常有用。我是ocaml新手,我需要演示如何使用“create”。当我阅读val create:?min_size:int->('el->'el->int)->“el t create?min_size cmp”时,我可以理解“create”是什么,但不能理解“create”是如何实现的(我指的是它得到了什么操作数,我是如何称呼它的)。我现在正在安装Jane Street Core,但不幸的是,我要到晚饭后才能为您做一个示例。还有许多其他核心用户可能会更快地提供帮助:-)。感谢您的帮助!我不明白的一件事是,我如何知道通过阅读“val create:?min_size:int->”('el->'el->'el->int)->“el t create?min_size cmp”来编写Core.Std.Heap.create。有什么我必须读的东西才能更容易理解这类事情吗?谢谢你的回答。我想在此代码中再添加两个函数。一个可以找到一个特定的元素,一个可以删除它。我试图让它发生,但我遇到了一些短的问题。这个链接现在是brokenthanks@rgrinberg,我把一个链接,应该是更持久的。
open Core.Std

(* A heap only expects a comparsion function on its elements. Use
  polymorphic compare if you just want something tham makes sense most
  of the time *)

let pq = Heap.create compare

let reverse_pq = Heap.create ~min_size:10 (Fn.flip compare)

(* The optional min size argument is there for optimization purposes. If you
   know that your heap will grow past a certain size you can allocate the array
   of that size in advance to save copying/resizing later on since the heap is
   array based *)

let () = 
  let random_list = List.init 10 ~f:(fun _ -> Random.int 10) in
  (* core wraps values inserted into the heap in the type 'el heap_el
    where 'el is the type of elements in your heap *)
  let heap_el = Heap.push pq (Random.int 10) in
  (* this gives you O(1) existence check in the heap: *)
  let x = Heap.heap_el_mem pq heap_el in (* true in O(1) *)
  let value_in_el = Heap.heap_el_get_el heap_el in
  (* now standard heap stuff, insert a list into a heap *)
  random_list |> List.iter ~f:(Fn.compose ignore (Heap.push pq));
  (* now drain the heap and get a list in sorted order, for reverse
  order you'd us reverse_pq *)
  let sorted_list = 
    let rec loop acc =
      match Heap.pop pq with
      | None -> acc
      | Some e -> loop (e::acc)
    in loop [] in
  printf "Sorted: %s\n" 
    (Sexp.to_string_hum (List.sexp_of_t Int.sexp_of_t sorted_list))