package catala

All checked builds pass
  1. Overview
  2. Docs
Compiler and library for the literate programming language for tax code specification

Install

dune-project
 Dependency

Authors

Maintainers

Sources

1.3.2.tar.gz
md5=4f8ba5a733ebdb5c115241162f618d6e
sha512=a091173c7f0a7ef3842849a72250e4fb2da65776f8b7423f5d2473234aa1e842eee63c424640622e89fd1059a6a442831d339bdb8406fb1b362efc0073058637

doc/src/catala.catala_utils/graphs.ml.html

Source file graphs.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
module JSON_Graph
    (V : sig
      include Graph.Sig.COMPARABLE

      val format : Format.formatter -> t -> unit
    end)
    (G : Graph.Sig.G with type V.t = V.t) =
struct
  module H = Hashtbl.Make (V)

  module Edges = Set.Make (struct
    type t = int * int

    let compare = compare
  end)

  let graph_to_json g =
    let ids = H.create (G.nb_vertex g) in
    let nodes =
      G.fold_vertex
        (fun v acc ->
          let id = H.length ids in
          H.add ids v id;
          (string_of_int id, `String (Format.asprintf "%a" V.format v)) :: acc)
        g []
    in
    let edges =
      G.fold_edges
        (fun v1 v2 acc -> Edges.add (H.find ids v1, H.find ids v2) acc)
        g Edges.empty
      |> Edges.elements
      |> List.map (fun (from, to_) ->
          `Assoc ["from", `Int from; "to", `Int to_])
    in
    `Assoc ["nodes", `Assoc nodes; "edges", `List edges]
end