Skip to content

Commit

Permalink
Merge pull request #61 from danielcompton/cljc
Browse files Browse the repository at this point in the history
Move to Clojure 1.7 and cljc
  • Loading branch information
aysylu committed Aug 3, 2015
2 parents 3c63d6f + 962d603 commit f720bf1
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 35 deletions.
8 changes: 4 additions & 4 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
:description "Graph library for Clojure"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.4.0"]
[org.clojure/data.priority-map "0.0.5"]]
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/data.priority-map "0.0.5"]
[tailrecursion/cljs-priority-map "1.1.0"]]
:url "https://github.com/aysylu/loom"
:test-selectors {:default (fn [m] (not (:test-check-slow m)))
:all (constantly true)
:test-check-slow :test-check-slow}
:profiles {:dev
{:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/test.check "0.5.7"]]}}
{:dependencies [[org.clojure/test.check "0.5.7"]]}}
:aliases {"release" ["do" "clean," "with-profile" "default" "deploy" "clojars"]}

:plugins [[codox "0.8.12"]]
Expand Down
7 changes: 7 additions & 0 deletions scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -e

grench eval "(require 'bolth)"
grench eval "(do (bolth/pretty-refresh) nil)"
grench eval "(clojure.pprint/pprint (bolth/run-all-tests #\".*\" {:clear-screen true :warn-on-slow-test-limit-ms 10 :force-real-stdout true :show-slow-tests true}))"
15 changes: 10 additions & 5 deletions src/loom/alg.clj → src/loom/alg.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ can use these functions."
out-degree in-degree weighted? directed? graph transpose]
:as graph]
[loom.alg-generic :refer [trace-path preds->span]]
[clojure.data.priority-map :as pm]
#?(:clj [clojure.data.priority-map :as pm]
:cljs [tailrecursion.priority-map :as pm])
[clojure.set :as clj.set]))

;;;
Expand Down Expand Up @@ -200,7 +201,8 @@ can use these functions."
(let [nodes (disj (nodes graph) start)
path-costs {start 0}
paths {start nil}
infinities (repeat Double/POSITIVE_INFINITY)
infinities (repeat #?(:clj Double/POSITIVE_INFINITY
:cljs js/Infinity))
nils (repeat nil)
init-costs (interleave nodes infinities)
init-paths (interleave nodes nils)]
Expand Down Expand Up @@ -234,7 +236,9 @@ can use these functions."
[costs
(->> (keys paths)
;;remove vertices that are unreachable from source
(remove #(= Double/POSITIVE_INFINITY (get costs %)))
(remove #(= #?(:clj Double/POSITIVE_INFINITY
:cljs js/Infinity)
(get costs %)))
(reduce
(fn [final-paths v]
(assoc final-paths v
Expand Down Expand Up @@ -425,7 +429,8 @@ can use these functions."
[g]
(letfn [(color-component [coloring start]
(loop [coloring (assoc coloring start 1)
queue (conj clojure.lang.PersistentQueue/EMPTY start)]
queue (conj #?(:clj clojure.lang.PersistentQueue/EMPTY
:cljs cljs.core.PersistentQueue/EMPTY) start)]
(if (empty? queue)
coloring
(let [v (peek queue)
Expand Down Expand Up @@ -551,7 +556,7 @@ can use these functions."
for un-weighted graphs."
([wg]
(cond
(directed? wg) (throw (Exception.
(directed? wg) (throw (#?(:clj Exception. :cljs js/Error)
"Spanning tree only defined for undirected graphs"))
:else (let [mst (prim-mst-edges wg (nodes wg) nil #{} [])]
(if (weighted? wg)
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions src/loom/dataflow.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
(set? start) start
(coll? start) (set start)
:else #{start})]
(loop [out-values {}
queue (into clojure.lang.PersistentQueue/EMPTY start)]
(loop [out-values {}
queue (into
#?(:clj clojure.lang.PersistentQueue/EMPTY
:cljs #queue [])
start)]
(let [node (peek queue)
worklist (pop queue)
in-value (join (mapv out-values (g/predecessors graph node)))
Expand Down
3 changes: 2 additions & 1 deletion src/loom/flow.clj → src/loom/flow.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
(assoc-in flow_1 [vn1 vn0] (- reverse-flow pushback))
flow_1)]
(cond (> pushback reverse-flow) (throw
(java.lang.RuntimeException.
(#?(:clj java.lang.RuntimeException.
:cljs js/Object)
(str "Path augmentation failure: "
vn0 " " vn1)))
(> (count path) 2) (recur flow_2 capacity (next path) increase)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
48 changes: 25 additions & 23 deletions test/loom/test/alg.clj
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
[:g :a :b :c :f :e :d] (topsort g5)
nil (topsort g7)
[5 6 7] (topsort g7 5)
[1 2 4 3] (topsort g14 1)
[1 2 3 4] (topsort g14 1)
[1 2 4] (topsort g15 1)))

(deftest breadth-first-test
Expand Down Expand Up @@ -287,19 +287,19 @@

{1 {1 [5], 5 [3], 3 [6 2], 2 [4], 6 [10]}
2 {2 [4], 4 [10]}
3 {3 [6 2 1], 1 [5], 2 [4], 6 [10]}
3 {3 [1 6 2], 1 [5], 2 [4], 6 [10]}
4 {4 [10], 10 [2]}
5 {5 [3], 3 [6 2 1], 2 [4], 6 [10]}
6 {6 [10 1], 1 [5], 10 [2], 5 [3], 2 [4]}
7 {4 [10], 8 [9 11], 7 [8], 9 [3 5], 11 [2 4], 3 [1 6]}
8 {4 [10], 8 [9 11], 9 [3 5 7], 11 [2 4], 3 [1 6]}
9 {8 [11], 6 [10], 7 [8], 2 [4], 9 [3 5 7], 3 [1 2 6]}
5 {5 [3], 3 [1 6 2], 2 [4], 6 [10]}
6 {6 [1 10], 1 [5], 10 [2], 5 [3], 2 [4]}
7 {4 [10], 8 [11 9], 7 [8], 9 [3 5], 11 [4 2], 3 [1 6]}
8 {4 [10], 8 [11 9], 9 [7 3 5], 11 [4 2], 3 [1 6]}
9 {8 [11], 6 [10], 7 [8], 2 [4], 9 [7 3 5], 3 [1 6 2]}
10 {10 [2], 2 [4]}
11 {11 [4 2], 4 [10]}} (all-pairs-shortest-paths g13)))

(deftest connectivity-test
(are [expected got] (= expected got)
[#{1 2 3 4} #{5 6 7 8} #{9}] (map set (connected-components
[#{5 6 7 8} #{1 2 3 4} #{9}] (map set (connected-components
(add-nodes g8 9)))
[#{:r :g :b :o :p}] (map set (connected-components g2))
[#{1 2 3 4 5 6 8 7}] (map set (connected-components g9))
Expand Down Expand Up @@ -384,12 +384,12 @@
(deftest bipartite-test
(are [expected got] (= expected got)
{0 1, 1 0, 5 0, 2 1, 3 1, 4 0} (bipartite-color g6)
{5 1, 1 1, 2 0, 3 0, 4 0, 6 0, 7 0, 8 0} (bipartite-color g8)
{1 1, 2 0, 3 0, 4 0, 5 0, 6 1, 7 1, 8 1} (bipartite-color g8)
nil (bipartite-color g1)
true (bipartite? g6)
true (bipartite? g8)
false (bipartite? g1)
#{#{2 3 4 6 7 8} #{1 5}} (set (bipartite-sets g8))))
#{#{2 3 4 5} #{1 6 7 8}} (set (bipartite-sets g8))))

(deftest coloring?-test
(are [expected got] (= expected got)
Expand All @@ -414,23 +414,25 @@
(are [expected got] (= expected got)
#{#{2 4 10} #{1 3 5 6} #{11} #{7 8 9}} (set (map set (scc g13)))))

(deftest prim-mst-edges-test
(are [expected got] (= expected got)
[[:a :e 1] [:a :b 3] [:b :c 5] [:c :d 2]] (prim-mst-edges mst_wt_g1)
[[:a :d 1] [:a :b 2] [:b :c 1] [:f :e 1]] (prim-mst-edges mst_wt_g2)
[[:a :c] [:a :b] [:a :d]] (prim-mst-edges mst_unweighted_g3)
[[:a :b 1]] (prim-mst-edges mst_wt_g4)
[[:a :c 2] [:c :b 2]] (prim-mst-edges mst_wt_g5)
[[:a :b 4] [:b :c 8] [:c :i 2] [:c :f 4] [:f :g 2]
[:g :h 1] [:c :d 7] [:d :e 9]] (prim-mst-edges mst_wt_g6)))
(deftest prim-mst-edges-weighted-test
(are [expected got] (= (set expected) (set got))
[[:e :a 1] [:a :b 3] [:b :c 5] [:c :d 2]] (prim-mst-edges mst_wt_g1)
[[:d :a 1] [:b :d 2] [:c :b 1] [:e :f 1]] (prim-mst-edges mst_wt_g2)
[[:c :a] [:d :b] [:c :d]] (prim-mst-edges mst_unweighted_g3)
[[:b :a 1]] (prim-mst-edges mst_wt_g4)
[[:c :a 2] [:c :b 2]] (prim-mst-edges mst_wt_g5)
[[:b :a 4] [:c :b 8] [:c :i 2] [:c :f 4] [:f :g 2]
[:g :h 1] [:d :c 7] [:e :d 9]] (prim-mst-edges mst_wt_g6)))

(deftest prim-mst-test
(are [expected got] (= expected got)
[#{:a :b :d :e :f :g :h} [[:a :b][:b :d][:b :a][:f :e][:d :b][:e :f]]]
[#{:a :b :d :e :f :g :h} (set [[:a :b] [:b :d] [:b :a] [:f :e] [:d :b] [:e :f]])]
(let [mst (prim-mst mst_wt_g7)]
[(nodes mst) (edges mst)])
[#{:a :b :c} [[:a :c] [:c :b] [:c :a] [:b :c]]] (let [mst (prim-mst mst_wt_g5)]
[(nodes mst) (edges mst)])))
[(nodes mst) (set (edges mst))])

[#{:a :b :c} (set [[:a :c] [:c :b] [:c :a] [:b :c]])]
(let [mst (prim-mst mst_wt_g5)]
[(nodes mst) (set (edges mst))])))


;;;;graphs for A* path
Expand Down

0 comments on commit f720bf1

Please sign in to comment.