-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
371 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
defmodule Exstreme.Graph do | ||
@moduledoc """ | ||
""" | ||
alias __MODULE__ | ||
|
||
@type t :: %Graph{params: [key: term], nodes: %{key: atom}, connections: %{key: atom}} | ||
defstruct params: [], nodes: %{}, connections: %{} | ||
|
||
@doc """ | ||
""" | ||
@spec count_nodes(t) :: non_neg_integer | ||
def count_nodes(%Graph{nodes: nodes}) do | ||
nodes | ||
|> Map.keys | ||
|> Enum.count | ||
end | ||
|
||
@doc """ | ||
""" | ||
@spec count_connections(t) :: non_neg_integer | ||
def count_connections(%Graph{connections: connections}) do | ||
connections | ||
|> Map.keys | ||
|> Enum.count | ||
end | ||
|
||
@doc """ | ||
""" | ||
@spec connections_stats(t) :: %{key: integer} | ||
def connections_stats(graph) do | ||
graph | ||
|> map_to_connections | ||
|> Enum.reduce(Map.new, fn(key, map) -> | ||
Map.update(map, key, 1, &(&1 + 1)) | ||
end) | ||
end | ||
|
||
@doc """ | ||
""" | ||
@spec find_start_node(t) :: [atom] | ||
def find_start_node(%Graph{nodes: nodes, connections: connections}) do | ||
is_first? = | ||
fn(node) -> | ||
at_first?(connections, node) and not(at_last?(connections, node)) | ||
end | ||
|
||
nodes | ||
|> Map.keys | ||
|> Enum.filter(is_first?) | ||
end | ||
|
||
# private | ||
|
||
@spec map_to_connections(t) :: [atom] | ||
defp map_to_connections(%Graph{nodes: nodes, connections: connections}) do | ||
to_connections = | ||
fn(node) -> | ||
case {at_first?(connections, node), at_last?(connections, node)} do | ||
{true, true} -> :connected | ||
{true, false} -> :begin | ||
{false, true} -> :end | ||
{false, false} -> :unconnected | ||
end | ||
end | ||
|
||
nodes | ||
|> Map.keys | ||
|> Enum.map(to_connections) | ||
end | ||
|
||
@spec at_first?(%{key: atom}, atom) :: boolean | ||
defp at_first?(connections, node) do | ||
Map.has_key?(connections, node) | ||
end | ||
|
||
@spec at_last?(%{key: atom}, atom) :: boolean | ||
defp at_last?(connections, node) do | ||
connections | ||
|> Map.values | ||
|> List.flatten | ||
|> Enum.member?(node) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
defmodule Exstreme.GraphValidator do | ||
alias Exstreme.Graph | ||
@moduledoc """ | ||
""" | ||
|
||
@doc """ | ||
""" | ||
@spec validate(Graph.t) :: :ok | {:error, []} | ||
def validate(graph) do | ||
with :ok <- validate_must_have_connections(graph), | ||
:ok <- validate_start_nodes(graph), | ||
:ok <- validate_connectivity(graph), | ||
do: :ok | ||
end | ||
|
||
# private | ||
|
||
@spec validate_must_have_connections(Graph.t) :: :ok | {:error, []} | ||
defp validate_must_have_connections(graph) do | ||
nodes_amount = Graph.count_nodes(graph) | ||
connections_amount = Graph.count_connections(graph) | ||
if nodes_amount > 0 and connections_amount == 0 do | ||
{:error, []} | ||
else | ||
:ok | ||
end | ||
end | ||
|
||
@spec validate_start_nodes(Graph.t) :: :ok | {:error, []} | ||
defp validate_start_nodes(graph) do | ||
start_nodes = Graph.find_start_node(graph) | ||
|
||
with :ok <- validate_should_start_with_one_node(start_nodes), | ||
:ok <- validate_should_start_with_node(start_nodes), | ||
do: :ok | ||
end | ||
|
||
@spec validate_should_start_with_one_node([atom, ...]) :: :ok | {:error, []} | ||
defp validate_should_start_with_one_node([_]), do: :ok | ||
|
||
defp validate_should_start_with_one_node(_), do: {:error, []} | ||
|
||
@spec validate_should_start_with_node([atom, ...]) :: :ok | {:error, []} | ||
defp validate_should_start_with_node([start_node]) do | ||
start_char = start_node |> Atom.to_string |> String.first | ||
if start_char == "n" do | ||
:ok | ||
else | ||
{:error, []} | ||
end | ||
end | ||
|
||
@spec validate_connectivity(Graph.t) :: :ok | {:error, []} | ||
defp validate_connectivity(graph) do | ||
stats = Graph.connections_stats(graph) | ||
if stats[:unconnected] == nil do | ||
:ok | ||
else | ||
{:error, []} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
%{"dialyxir": {:hex, :dialyxir, "0.3.3"}} |
Oops, something went wrong.