Skip to content

Commit

Permalink
Add support for meta
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiesardo committed Sep 20, 2014
1 parent e40104b commit bbec4cf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
8 changes: 7 additions & 1 deletion src/linked/map.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns linked.map
(:import (clojure.lang Associative
Counted
IObj
IFn
ILookup
IPersistentCollection
Expand Down Expand Up @@ -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))) "}"))
Expand Down
27 changes: 17 additions & 10 deletions src/linked/set.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns linked.set
(:use [linked.map :only [linked-map]])
(:import (clojure.lang Counted
IObj
IFn
ILookup
IPersistentCollection
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion test/linked/map_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 3 additions & 2 deletions test/linked/set_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -85,4 +87,3 @@
(is (= LinkedSet (type o)))
(is (= '(1 2 9 8 7 5)
(seq o))))))

0 comments on commit bbec4cf

Please sign in to comment.