Source file set_diff.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
open Base
open Bin_prot.Std

module Stable = struct
  module V1 = struct
    module Change = struct
      type 'a t =
        | Add of 'a
        | Remove of 'a
      [@@deriving bin_io, quickcheck, sexp]
    end

    include struct
      open Base_quickcheck

      type 'a t = 'a Change.t list [@@deriving bin_io, quickcheck, sexp]
    end

    let get ~from ~to_ = exclave_
      if phys_equal from to_
      then Optional_diff.get_none ()
      else (
        let diff =
          Set.symmetric_diff from to_
          |> Sequence.to_list
          |> List.map ~f:(function
            | First a -> Change.Remove a
            | Second a -> Change.Add a)
        in
        if List.is_empty diff
        then Optional_diff.get_none ()
        else Optional_diff.return diff)
    ;;

    let apply_exn set diff =
      List.fold diff ~init:set ~f:(fun acc diff ->
        match diff with
        | Change.Remove set -> Set.remove acc set
        | Change.Add set -> Set.add acc set)
    ;;

    let of_list_exn = function
      | [] -> exclave_ Optional_diff.get_none ()
      | _ :: _ as l -> exclave_ Optional_diff.return (List.concat l)
    ;;

    module Make (S : sig
        module Elt : sig
          type t
          type comparator_witness
        end

        type t = (Elt.t, Elt.comparator_witness) Set.t
      end) : Diff_intf.S_plain with type derived_on := S.t and type t := S.Elt.t t =
    struct
      let get = get
      let apply_exn = apply_exn
      let of_list_exn = of_list_exn
    end
  end
end

include Stable.V1