Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support empty maps #127

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/loom/graph.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -496,16 +496,18 @@ on adjacency lists."
(assoc :attrs (merge (:attrs g) (:attrs init)))))
;; adacency map
(map? init)
(let [es (if (map? (val (first init)))
(for [[n nbrs] init
[nbr wt] nbrs]
[n nbr wt])
(for [[n nbrs] init
nbr nbrs]
[n nbr]))]
(-> g
(add-nodes* (keys init))
(add-edges* es)))
(if (empty? init)
g
(let [es (if (map? (val (first init)))
(for [[n nbrs] init
[nbr wt] nbrs]
[n nbr wt])
(for [[n nbrs] init
nbr nbrs]
[n nbr]))]
(-> g
(add-nodes* (keys init))
(add-edges* es))))
;; edge
(sequential? init) (add-edges g init)
;; node
Expand Down
5 changes: 4 additions & 1 deletion test/loom/test/graph.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
g2 (graph {1 [2 3] 2 [3] 4 []})
g3 (graph g1)
g4 (graph g3 (digraph [5 6]) [7 8] 9)
g5 (graph)]
g5 (graph)
g6 (graph {})]
(testing "Construction, nodes, edges"
(are [expected got] (= expected got)
#{1 2 3 4} (set (nodes g1))
Expand All @@ -34,6 +35,8 @@
[3 2] [5 6] [6 5] [7 8] [8 7]} (set (edges g4))
#{} (set (nodes g5))
#{} (set (edges g5))
#{} (set (nodes g6))
#{} (set (edges g6))
true (has-node? g1 4)
true (has-edge? g1 1 2)
false (has-node? g1 5)
Expand Down