jon.recoil.org

Source file sequence.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
open! Import
include Base.Sequence

include%template
  Bin_prot.Utils.Make_binable1_without_uuid [@modality portable] [@alert "-legacy"] (struct
    module Binable = struct
      type ('a : value_or_null) t = 'a list [@@deriving bin_io]
    end

    type ('a : value_or_null) t = 'a Base.Sequence.t

    let of_binable = Base.Sequence.of_list
    let to_binable = Base.Sequence.to_list
  end)

module Step = struct
  include Step

  type (+'a : any, 's : any) t = ('a, 's) Base.Sequence.Step.t =
    | Done
    | Skip : ('a : any) ('s : value_or_null). { state : 's } -> ('a, 's) t
    | Yield :
        ('a : value_or_null) ('s : value_or_null).
        { value : 'a
        ; state : 's
        }
        -> ('a, 's) t

  module Non_gadt = struct
    type (+'a : value_or_null, 's : value_or_null) t =
      | Done
      | Skip of { state : 's }
      | Yield of
          { value : 'a
          ; state : 's
          }
    [@@deriving bin_io]
  end

  include%template
    Binable0.Of_binable2_without_uuid [@modality portable] [@alert "-legacy"]
      (Non_gadt)
      (struct
        type nonrec ('a : value_or_null, 's : value_or_null) t = ('a, 's) t

        let to_binable : _ t -> _ Non_gadt.t = function
          | Done -> Done
          | Skip { state } -> Skip { state }
          | Yield { value; state } -> Yield { value; state }
        ;;

        let of_binable : _ Non_gadt.t -> _ t = function
          | Done -> Done
          | Skip { state } -> Skip { state }
          | Yield { value; state } -> Yield { value; state }
        ;;
      end)
end

module Merge_with_duplicates_element = struct
  include Merge_with_duplicates_element

  type ('a : value_or_null, 'b : value_or_null) t =
        ('a, 'b) Merge_with_duplicates_element.t =
    | Left of 'a
    | Right of 'b
    | Both of 'a * 'b
  [@@deriving bin_io]
end

module type Heap = sig
  type 'a t

  val create : compare:('a -> 'a -> int) -> 'a t
  val add : 'a t -> 'a -> 'a t
  val pop_min : 'a t -> ('a * 'a t) option
end

let merge_all (module Heap : Heap) seqs ~compare =
  let module Merge_all_state = struct
    type 'a t =
      { heap : ('a * 'a Base.Sequence.t) Heap.t
      ; not_yet_in_heap : 'a Base.Sequence.t list
      }
    [@@deriving fields ~iterators:create]

    let create = Fields.create
  end
  in
  unfold_step
    ~init:
      (Merge_all_state.create
         ~heap:(Heap.create ~compare:(fun a b -> Base.Comparable.lift compare ~f:fst a b))
         ~not_yet_in_heap:seqs)
    ~f:(fun { heap; not_yet_in_heap } ->
      match not_yet_in_heap with
      | seq :: not_yet_in_heap ->
        (match Expert.next_step seq with
         | Done -> Skip { state = { not_yet_in_heap; heap } }
         | Skip { state = seq } ->
           Skip { state = { not_yet_in_heap = seq :: not_yet_in_heap; heap } }
         | Yield { value = elt; state = seq } ->
           Skip { state = { not_yet_in_heap; heap = Heap.add heap (elt, seq) } })
      | [] ->
        (match Heap.pop_min heap with
         | None -> Done
         | Some ((elt, seq), heap) ->
           Yield { value = elt; state = { heap; not_yet_in_heap = [ seq ] } }))
;;