From 75ddfecd035a63093555b92d754d80b56c52e4a8 Mon Sep 17 00:00:00 2001 From: Po Chen Date: Tue, 13 Dec 2022 08:41:20 +1100 Subject: [PATCH] load from libgraph graph --- lib/dg.ex | 22 ++++++++++++++++++++++ mix.exs | 1 + mix.lock | 1 + 3 files changed, 24 insertions(+) diff --git a/lib/dg.ex b/lib/dg.ex index 040be90..f9c8e7f 100644 --- a/lib/dg.ex +++ b/lib/dg.ex @@ -216,4 +216,26 @@ defmodule DG do def topsort(%__MODULE__{dg: dg}) do :digraph_utils.topsort(dg) end + + if Code.ensure_loaded?(Graph) do + def from({:libgraph, graph}) do + dg = DG.new() + + graph + |> Graph.vertices() + |> Enum.map(&{:vertex, &1}) + |> Enum.into(dg) + + graph + |> Graph.edges() + |> Enum.map(fn + %Graph.Edge{label: nil} = e -> + {:edge, e.v1, e.v2} + + %Graph.Edge{} = e -> + {:edge, e.v1, e.v2, e.label} + end) + |> Enum.into(dg) + end + end end diff --git a/mix.exs b/mix.exs index 4a8248f..2146fd3 100644 --- a/mix.exs +++ b/mix.exs @@ -27,6 +27,7 @@ defmodule DG.MixProject do defp deps do [ {:abnf_parsec, "~> 1.2", runtime: false}, + {:libgraph, ">= 0.0.0", optional: true}, {:ex_doc, ">= 0.0.0", only: :docs} ] end diff --git a/mix.lock b/mix.lock index 40fc60f..c48ab2b 100644 --- a/mix.lock +++ b/mix.lock @@ -2,6 +2,7 @@ "abnf_parsec": {:hex, :abnf_parsec, "1.2.1", "c7964f031ac01f61c20680056ca0295b3b34a200038294e8147718d9d468ee28", [:mix], [{:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "df71684ebb3160d8d6e3f880ad1b5261855de43cc25adcb4692240e082d4cff3"}, "earmark_parser": {:hex, :earmark_parser, "1.4.29", "149d50dcb3a93d9f3d6f3ecf18c918fb5a2d3c001b5d3305c926cddfbd33355b", [:mix], [], "hexpm", "4902af1b3eb139016aed210888748db8070b8125c2342ce3dcae4f38dcc63503"}, "ex_doc": {:hex, :ex_doc, "0.29.1", "b1c652fa5f92ee9cf15c75271168027f92039b3877094290a75abcaac82a9f77", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "b7745fa6374a36daf484e2a2012274950e084815b936b1319aeebcf7809574f6"}, + "libgraph": {:hex, :libgraph, "0.16.0", "3936f3eca6ef826e08880230f806bfea13193e49bf153f93edcf0239d4fd1d07", [:mix], [], "hexpm", "41ca92240e8a4138c30a7e06466acc709b0cbb795c643e9e17174a178982d6bf"}, "makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"}, "makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"}, "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},