Skip to content

Commit

Permalink
Add print method for empty map.
Browse files Browse the repository at this point in the history
Also add performance test (disabled by default)
  • Loading branch information
frankiesardo committed Jun 10, 2014
1 parent 8088f71 commit c0beccb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/linked/map.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

(defrecord Node [key value left right])

(defn- insert [{:keys [head-node tail-node delegate-map]} k v]
)

(deftype LinkedMap [head-node tail-node delegate-map]
IPersistentMap
(assoc [this k v]
Expand Down Expand Up @@ -213,7 +216,9 @@

(defmethod print-method LinkedMap [o ^java.io.Writer w]
(.write w "#linked/map ")
(print-method (seq o) w))
(if-let [s (seq o)]
(print-method (seq o) w)
(print-method (list) w)))

(def ^{:private true,
:tag LinkedMap} empty-linked-map (LinkedMap. nil nil (hash-map)))
Expand Down
4 changes: 3 additions & 1 deletion src/linked/set.clj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@

(defmethod print-method LinkedSet [o ^java.io.Writer w]
(.write w "#linked/set ")
(print-method (seq o) w))
(if-let [s (seq o)]
(print-method (seq o) w)
(print-method (list) w)))

(def ^{:private true,
:tag LinkedSet} empty-linked-set (LinkedSet. (linked-map)))
Expand Down
27 changes: 27 additions & 0 deletions test/linked/map_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,30 @@
(is (= LinkedMap (type o)))
(is (= '([1 9] [3 4] [5 6] [7 8])
(seq o))))))

(defn- insertion-time [m]
(let [start-time (System/currentTimeMillis)
result (reduce #(assoc %1 %2 %2) m (range 1000000))
end-time (System/currentTimeMillis)]
[result (- end-time start-time)]))

(defn- removal-time [m]
(let [start-time (System/currentTimeMillis)
result (reduce #(dissoc %1 %2) m (range 1000000))
end-time (System/currentTimeMillis)]
[result (- end-time start-time)]))

#_(deftest performance
(testing "Stress test - TODO should be randomized"
(let [empty-linked (linked-map)
empty-hash (hash-map)
[full-linked linked-insertion-time] (insertion-time empty-linked)
[full-hash hash-insertion-time] (insertion-time empty-hash)]
(println "Linked insertion time" linked-insertion-time)
(println "Hash insertion time" hash-insertion-time)
(is (< linked-insertion-time (* 2 hash-insertion-time)))
(let [[_ linked-removal-time] (removal-time full-linked)
[_ hash-removal-time] (removal-time full-hash)]
(println "Linked removal time" linked-removal-time)
(println "Hash removal time" hash-removal-time)
(is (< linked-removal-time (* 2 hash-removal-time)))))))

0 comments on commit c0beccb

Please sign in to comment.