diff --git a/deps-clr.edn b/deps-clr.edn new file mode 100644 index 0000000..99c6aad --- /dev/null +++ b/deps-clr.edn @@ -0,0 +1,12 @@ +{:paths ["src"] + :deps + {io.github.clojure/clr.data.generators {:git/tag "v1.1.0" :git/sha "d25d292"} + } + + :aliases + {:test + {:extra-paths ["test"] + :extra-deps {io.github.dmiller/test-runner {:git/tag "v0.5.1clr" :git/sha "814e06f"} + io.github.clojure/clr.test.check {:git/tag "v1.1.2" :git/sha "26f34e6"}} + :exec-fn cognitect.test-runner.api/test + :exec-args {:dirs ["test"]}}}} \ No newline at end of file diff --git a/src/editscript/core.cljc b/src/editscript/core.cljc index 97bfd65..cf2df4b 100644 --- a/src/editscript/core.cljc +++ b/src/editscript/core.cljc @@ -15,6 +15,8 @@ [editscript.diff.quick :as q] [editscript.diff.a-star :as a]) #?(:clj (:import [editscript.edit EditScript] + [clojure.lang MapEntry]) + :cljr (:import [editscript.edit EditScript] [clojure.lang MapEntry]))) (defn diff diff --git a/src/editscript/diff/a_star.cljc b/src/editscript/diff/a_star.cljc index 30b61d7..c380f80 100644 --- a/src/editscript/diff/a_star.cljc +++ b/src/editscript/diff/a_star.cljc @@ -17,9 +17,12 @@ #?(:clj (:import [clojure.lang Keyword] [java.io Writer] [java.lang Comparable] + [editscript.util.index Node]) + :cljr (:import [clojure.lang Keyword] [editscript.util.index Node]))) #?(:clj (set! *warn-on-reflection* true)) +#?(:cljr (set! *warn-on-reflection* true)) #?(:clj (set! *unchecked-math* :warn-on-boxed)) ;; diffing @@ -42,6 +45,22 @@ Comparable (compareTo [this that] (- (.hashCode this) (.hashCode that)))) + :cljr + (deftype Coord [^Node a + ^Node b] + ;; Java's native hash is too slow, + ;; overriding hashCode significantly speeds things up + Object + (GetHashCode [_] (coord-hash a b)) + (Equals [_ that] + (and (= (i/get-order a) (i/get-order (.-a ^Coord that))) + (= (i/get-order b) (i/get-order (.-b ^Coord that))))) + (ToString [_] + (str "[" (i/get-value a) "," (i/get-value b) "]")) + + IComparable + (CompareTo [this that] + (- (.GetHashCode this) (.GetHashCode that)))) :cljs (deftype Coord [^Node a @@ -111,6 +130,7 @@ (defn- access-g [g cur] (get g cur #?(:clj Long/MAX_VALUE + :cljr Int64/MaxValue :cljs (getMaxValue)))) (declare diff*) diff --git a/src/editscript/diff/quick.cljc b/src/editscript/diff/quick.cljc index ffaf7c8..44cb587 100644 --- a/src/editscript/diff/quick.cljc +++ b/src/editscript/diff/quick.cljc @@ -16,6 +16,7 @@ #?(:clj (set! *warn-on-reflection* true)) #?(:clj (set! *unchecked-math* :warn-on-boxed)) +#?(:cljr (set! *warn-on-reflection* true)) (declare diff*) diff --git a/src/editscript/edit.cljc b/src/editscript/edit.cljc index 245c609..7cbe31e 100644 --- a/src/editscript/edit.cljc +++ b/src/editscript/edit.cljc @@ -11,7 +11,10 @@ (ns ^:no-doc editscript.edit #?(:clj (:import [clojure.lang PersistentVector IPersistentList IPersistentMap IPersistentSet IPersistentVector MapEntry] - [java.util Map$Entry]))) + [java.util Map$Entry]) + :cljr (:import [clojure.lang PersistentVector IPersistentList IPersistentMap + IPersistentSet IPersistentVector MapEntry] + ))) (defprotocol IEdit (auto-sizing [this path value]) @@ -53,7 +56,7 @@ IPersistentSet (get-type [_] :set) - + Map$Entry (get-type [_] :val) @@ -68,6 +71,32 @@ Object (get-type [_] :val)) + + :cljr + (extend-protocol IType + IPersistentList + (get-type [_] :lst) + + IPersistentMap + (get-type [_] :map) + + IPersistentVector + (get-type [_] :vec) + + IPersistentSet + (get-type [_] :set) + + MapEntry + (get-type [_] :val) + + nil + (get-type [_] :val) + + String + (get-type [_] :str) + + Object + (get-type [_] :val)) :cljs (extend-protocol IType diff --git a/src/editscript/patch.cljc b/src/editscript/patch.cljc index e8ae72f..be7f71b 100644 --- a/src/editscript/patch.cljc +++ b/src/editscript/patch.cljc @@ -15,6 +15,7 @@ [clojure.string :as s])) #?(:clj (set! *warn-on-reflection* true)) +#?(:cljr (set! *warn-on-reflection* true)) #?(:clj (set! *unchecked-math* :warn-on-boxed)) (defn vget diff --git a/src/editscript/util/common.cljc b/src/editscript/util/common.cljc index 958f333..a3ea2da 100644 --- a/src/editscript/util/common.cljc +++ b/src/editscript/util/common.cljc @@ -32,7 +32,9 @@ (defn current-time ^long [] - #?(:clj (System/currentTimeMillis) :cljs (.getTime (js/Date.)))) + #?(:clj (System/currentTimeMillis) + :cljr (.ToUnixTimeMilliseconds (DateTimeOffset/Now)) + :cljs (.getTime (js/Date.)))) (defn- vec-edits* "Based on 'Wu, S. et al., 1990, An O(NP) Sequence Comparison Algorithm, @@ -106,7 +108,7 @@ [ms ps] (split-with #(= % m) coll) mc (count ms) pc (count ps) - delta (Math/abs (- mc pc)) + delta (#?(:cljr Math/Abs :default Math/abs) (- mc pc)) rs (repeat (- (max mc pc) delta) :r)] (cond (< mc pc) (concat rs (repeat delta p)) diff --git a/src/editscript/util/index.cljc b/src/editscript/util/index.cljc index 95317f6..9f365a6 100644 --- a/src/editscript/util/index.cljc +++ b/src/editscript/util/index.cljc @@ -12,7 +12,8 @@ (:require [editscript.edit :as e] #?(:cljs [goog.math.Long :refer [getMaxValue]])) #?(:clj (:import [clojure.lang PersistentVector] - [java.io Writer])) ) + [java.io Writer]) + :cljr (:import [clojure.lang PersistentVector])) ) ;; indexing diff --git a/src/editscript/util/pairing.cljc b/src/editscript/util/pairing.cljc index b1008d8..3ede5f0 100644 --- a/src/editscript/util/pairing.cljc +++ b/src/editscript/util/pairing.cljc @@ -11,7 +11,10 @@ (ns ^:no-doc editscript.util.pairing #?(:clj (:import [clojure.lang IPersistentStack IPersistentMap IPersistentCollection] - [java.io Writer]))) + [java.io Writer]) + :cljr + (:import [clojure.lang IPersistentStack IPersistentMap IPersistentCollection] + ))) #?(:clj (set! *warn-on-reflection* true)) @@ -100,7 +103,45 @@ (set! map (dissoc map (.-item heap))) (set! heap n) this))) +:cljr + (deftype PriorityMap [^:unsynchronized-mutable ^HeapNode heap + ^:unsynchronized-mutable map] + IPersistentCollection + (count [_] (count map)) + (^IPersistentCollection cons [this e] + (let [[item priority] e] + (set! map (assoc map item priority)) + (set! heap (insert heap item priority)) + this)) + (empty [this] + (set! heap nil) + (set! map {}) + this) + (equiv [this o] (identical? this o)) + + IPersistentMap + (^IPersistentMap assoc [this item priority] + (set! map (assoc map item priority)) + (set! heap (insert heap item priority)) + this) + (^clojure.lang.Associative assoc [this item priority] + (set! map (assoc map item priority)) + (set! heap (insert heap item priority)) + this) + (GetHashCode [_] (hash map)) + (Equals [this o] (identical? this o)) + (containsKey [_ item] (contains? map item)) + (entryAt [_ k] (find map k)) + (seq [_] (seq map)) + (without [this item] (dissoc map item) this) + IPersistentStack + (peek [_] [(.-item heap) (.-priority heap)]) + (pop [this] + (let [n (two-pass (get-left heap))] + (set! map (dissoc map (.-item heap))) + (set! heap n) + this))) :cljs (deftype PriorityMap [^:mutable ^HeapNode heap ^:mutable map] @@ -115,6 +156,7 @@ (set! heap (insert heap item priority)) this)) + IAssociative IAssociative (-assoc [this item priority] (set! map (assoc map item priority)) diff --git a/test/editscript/core_test.cljc b/test/editscript/core_test.cljc index 378ebb3..b561bb5 100644 --- a/test/editscript/core_test.cljc +++ b/test/editscript/core_test.cljc @@ -230,17 +230,17 @@ ;; sample data tests (def data1 (-> "resources/drawing1.edn" - #?(:clj slurp :cljs com/vslurp) - #?(:clj read-string :cljs reader/read-string))) + #?(:default slurp :cljs com/vslurp) + #?(:default read-string :cljs reader/read-string))) (def data2 (-> "resources/drawing2.edn" - #?(:clj slurp :cljs com/vslurp) - #?(:clj read-string :cljs reader/read-string))) + #?(:default slurp :cljs com/vslurp) + #?(:default read-string :cljs reader/read-string))) (def data3 (-> "resources/drawing3.edn" - #?(:clj slurp :cljs com/vslurp) - #?(:clj read-string :cljs reader/read-string))) + #?(:default slurp :cljs com/vslurp) + #?(:default read-string :cljs reader/read-string))) (def data4 (-> "resources/drawing4.edn" - #?(:clj slurp :cljs com/vslurp) - #?(:clj read-string :cljs reader/read-string))) + #?(:default slurp :cljs com/vslurp) + #?(:default read-string :cljs reader/read-string))) (deftest drawing-sample-test (testing "A sample JSON data of a drawing program from https://github.com/justsml/json-diff-performance, converted to edn using https://github.com/peterschwarz/json-to-edn" diff --git a/test/editscript/edit_test.cljc b/test/editscript/edit_test.cljc index 0f7d694..b65277a 100644 --- a/test/editscript/edit_test.cljc +++ b/test/editscript/edit_test.cljc @@ -12,6 +12,7 @@ (:require [editscript.edit :as e] [editscript.core :as c] #?(:clj [clojure.test :refer [is are deftest ]] + :cljr [clojure.test :refer [is are deftest ]] :cljs [cljs.test :refer [is are deftest] :include-macros true]))) (deftest edits-equality-test