diff --git a/project.clj b/project.clj index 7daa392..f1548a7 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject linked "0.1.2" +(defproject linked "0.1.3" :description "Efficient ordered map and set" :url "http://github.com/frankiesardo/linked" :license {:name "Eclipse Public License" diff --git a/src/linked/map.clj b/src/linked/map.clj index 4ad23f1..d30a071 100644 --- a/src/linked/map.clj +++ b/src/linked/map.clj @@ -1,6 +1,7 @@ (ns linked.map (:import (clojure.lang Associative Counted + IObj IFn ILookup IPersistentCollection @@ -108,13 +109,18 @@ (:key tail-node) (:value tail-node) (let [v (.valAt delegate-map k not-found)] (if (= v not-found) not-found (:value v))))) - IFn (invoke [this k] (.valAt this k)) (invoke [this k not-found] (.valAt this k not-found)) + IObj + (meta [this] + (.meta ^IObj delegate-map)) + (withMeta [this m] + (LinkedMap. head-node tail-node (.withMeta ^IObj delegate-map m))) + Object (toString [this] (str "{" (clojure.string/join ", " (for [[k v] this] (str k " " v))) "}")) diff --git a/src/linked/set.clj b/src/linked/set.clj index a4863d3..cb9a960 100644 --- a/src/linked/set.clj +++ b/src/linked/set.clj @@ -1,6 +1,7 @@ (ns linked.set (:use [linked.map :only [linked-map]]) (:import (clojure.lang Counted + IObj IFn ILookup IPersistentCollection @@ -13,18 +14,18 @@ (java.lang Iterable))) -(deftype LinkedSet [l-map] +(deftype LinkedSet [linked-map] IPersistentSet (disjoin [_ k] - (LinkedSet. (.without l-map k))) + (LinkedSet. (.without linked-map k))) (contains [_ k] - (.containsKey l-map k)) + (.containsKey linked-map k)) (get [_ k] - (.valAt l-map k)) + (.valAt linked-map k)) Set (size [_] - (.size l-map)) + (.size linked-map)) Iterable (iterator [this] @@ -34,9 +35,9 @@ IPersistentCollection (count [_] - (.count l-map)) + (.count linked-map)) (cons [_ o] - (LinkedSet. (.cons l-map [o o]))) + (LinkedSet. (.cons linked-map [o o]))) (empty [_] (linked-set)) (equiv [this o] @@ -46,17 +47,23 @@ Seqable (seq [_] - (if-let [s (.seq l-map)] + (if-let [s (.seq linked-map)] (map first s))) Reversible (rseq [_] - (if-let [s (.rseq l-map)] + (if-let [s (.rseq linked-map)] (map first s))) IFn (invoke [_ k] - (.valAt l-map k)) + (.valAt linked-map k)) + + IObj + (meta [this] + (.meta ^IObj linked-map)) + (withMeta [this m] + (LinkedSet. (.withMeta ^IObj linked-map m))) Object (toString [this] diff --git a/test/linked/map_test.clj b/test/linked/map_test.clj index de88135..9cc20b8 100644 --- a/test/linked/map_test.clj +++ b/test/linked/map_test.clj @@ -100,7 +100,9 @@ (are [x] (= m x) (conj m nil) (merge m ()) - (into m ()))))) + (into m ()))) + (testing "meta support" + (is (= {'a 'b} (meta (with-meta m {'a 'b}))))))) (deftest object-features (let [m (linked-map 'a 1 :b 2)] diff --git a/test/linked/set_test.clj b/test/linked/set_test.clj index f48596e..59d4d67 100644 --- a/test/linked/set_test.clj +++ b/test/linked/set_test.clj @@ -71,7 +71,9 @@ (testing "Falsy lookup support" (is (= false (#{false 1} false)))) (testing "Ordered disj" - (is (= #{:a 1 2 3} (disj s :b :c)))))) + (is (= #{:a 1 2 3} (disj s :b :c)))) + (testing "meta support" + (is (= {'a 'b} (meta (with-meta s {'a 'b}))))))) (deftest object-features (let [s (linked-set 'a 1 :b 2)] @@ -85,4 +87,3 @@ (is (= LinkedSet (type o))) (is (= '(1 2 9 8 7 5) (seq o)))))) -