Skip to content

Fix #77: tagged literal issue #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

* Fix [#77](https://github.com/clj-commons/ordered/pull/77): tagged literal issue

## 1.15.12 - 2024-05-13

* Fix NPE when hashing ordered-set that contain `nil`.
Expand Down
26 changes: 16 additions & 10 deletions src/flatland/ordered/map.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

(set! *warn-on-reflection* true)

(defmacro key* [map-entry]
`(.nth ~(with-meta map-entry {:tag 'IPersistentVector}) 0))

(defmacro val* [map-entry]
`(.nth ~(with-meta map-entry {:tag 'IPersistentVector}) 1))

(defn entry [k v i]
(MapEntry. k (MapEntry. i v)))

Expand All @@ -37,9 +43,9 @@
(and (instance? Map other)
(= (.count this) (.size ^Map other))
(every? (fn [^MapEntry e]
(let [k (.key e)]
(let [k (key* e)]
(and (.containsKey ^Map other k)
(= (.val e) (.get ^Map other k)))))
(= (val* e) (.get ^Map other k)))))
(.seq this))))
(entryAt [this k]
(let [v (get this k ::not-found)]
Expand All @@ -48,8 +54,8 @@
(valAt [this k]
(.valAt this k nil))
(valAt [this k not-found]
(if-let [^MapEntry e (.get ^Map backing-map k)]
(.val e)
(if-let [^IPersistentVector e (.get ^Map backing-map k)]
(.nth e 1)
not-found))
(count [this]
(.count backing-map))
Expand All @@ -70,18 +76,18 @@

(assoc [this k v]
(if-let [^MapEntry e (.get ^Map backing-map k)]
(let [old-v (.val e)]
(let [old-v (val* e)]
(if (identical? old-v v)
this
(let [i (.key e)]
(let [i (key* e)]
(OrderedMap. (.cons backing-map (entry k v i))
(.assoc order i (MapEntry. k v))))))
(OrderedMap. (.cons backing-map (entry k v (.count order)))
(.cons order (MapEntry. k v)))))
(without [this k]
(if-let [^MapEntry e (.get ^Map backing-map k)]
(OrderedMap. (.without backing-map k)
(.assoc order (.key e) nil))
(.assoc order (key* e) nil))
this))
(seq [this]
(seq (keep identity order)))
Expand Down Expand Up @@ -172,15 +178,15 @@ assoc'ed for the first time. Supports transient."
(.valAt this k nil))
(valAt [this k not-found]
(if-let [^MapEntry e (.valAt backing-map k)]
(.val e)
(val* e)
not-found))
(assoc [this k v]
(let [^MapEntry e (.valAt backing-map k this)
vector-entry (MapEntry. k v)
i (if (identical? e this)
(do (change! order .conj vector-entry)
(dec (.count order)))
(let [idx (.key e)]
(let [idx (key* e)]
(change! order .assoc idx vector-entry)
idx))]
(change! backing-map .conj (entry k v i))
Expand All @@ -191,7 +197,7 @@ assoc'ed for the first time. Supports transient."
(without [this k]
(let [^MapEntry e (.valAt backing-map k this)]
(when-not (identical? e this)
(let [i (.key e)]
(let [i (key* e)]
(change! backing-map dissoc! k)
(change! order assoc! i nil)))
this))
Expand Down
7 changes: 7 additions & 0 deletions test/flatland/ordered/map_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,10 @@
[[nil :a]]
[[:a nil]]
[[nil nil]])))

#?(:clj
(deftest issue-77-reader-macro-test
(is (= 1 (let [x #ordered/map [[:a 1]]]
(:a x))))
(is (= [:a] (let [x #ordered/map [[:a 1]]]
(keys x) )))))