Skip to content

Commit 22fc333

Browse files
committed
Better equiv implementation
1 parent 2b824ea commit 22fc333

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

‎src/clj/tools/2dtree.clj‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@
8282
(-> rect
8383
(assoc :ymax y)))
8484

85+
;; Copied from here: https://github.com/clojure/data.finger-tree/blob/master/src/main/clojure/clojure/data/finger_tree.clj
86+
(defn- seq-equals [a b]
87+
(boolean
88+
(when (or (sequential? b) (instance? java.util.List b))
89+
(loop [a (seq a)
90+
b (seq b)]
91+
(when (= (nil? a) (nil? b))
92+
(or
93+
(nil? a)
94+
(when (= (first a) (first b))
95+
(recur (next a) (next b)))))))))
96+
8597
(defrecord TreeNode [value vertical rect])
8698

8799
(defn- tree-cons [root pt]
@@ -178,7 +190,7 @@
178190
(intersects-shape? other-rect (:rect child-node)))))
179191
(map #(conj current-path %))))))))))
180192

181-
(deftype TwoTree [root]
193+
(deftype TwoTree [^TreeNode root]
182194
I2DTree
183195
(value [_]
184196
(:value root))
@@ -207,13 +219,15 @@
207219
:else (TwoTree. higher)))]
208220
(more* root [])))
209221

222+
clojure.lang.Sequential
210223
clojure.lang.Seqable
211224
(seq [this]
212225
(when (contains? root :value) this))
213226

214227
clojure.lang.IPersistentCollection
215-
(equiv [_ other]
216-
(= root (.root other)))
228+
(equiv [this other]
229+
(seq-equals this other)
230+
)
217231
(empty [_]
218232
(TwoTree. nil))
219233

@@ -249,7 +263,7 @@
249263
;; See this issue https://github.com/thi-ng/color/pull/11
250264
(prefer-method clojure.pprint/simple-dispatch clojure.lang.ISeq clojure.lang.IPersistentSet)
251265

252-
(defmethod print-method TwoTree [tree ^java.io.Writer w]
266+
(defmethod print-method TwoTree [^TwoTree tree ^java.io.Writer w]
253267
(.write w (str "Tree" " " (.root tree))))
254268

255269
(defn two-tree [& xs]

0 commit comments

Comments
 (0)