From c0beccb59292fa6b922add113d1c9e05c11df4d5 Mon Sep 17 00:00:00 2001 From: frankiesardo Date: Tue, 10 Jun 2014 10:27:08 +0200 Subject: [PATCH] Add print method for empty map. Also add performance test (disabled by default) --- src/linked/map.clj | 7 ++++++- src/linked/set.clj | 4 +++- test/linked/map_test.clj | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/linked/map.clj b/src/linked/map.clj index cf35804..d888372 100644 --- a/src/linked/map.clj +++ b/src/linked/map.clj @@ -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] @@ -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))) diff --git a/src/linked/set.clj b/src/linked/set.clj index 6322c91..a4863d3 100644 --- a/src/linked/set.clj +++ b/src/linked/set.clj @@ -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))) diff --git a/test/linked/map_test.clj b/test/linked/map_test.clj index 5b70b27..de88135 100644 --- a/test/linked/map_test.clj +++ b/test/linked/map_test.clj @@ -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)))))))