Skip to content

Commit

Permalink
fixed priority-map bug with dissoc of item not in map, also addressed…
Browse files Browse the repository at this point in the history
… new map equivalence changes for 1.3
  • Loading branch information
Engelberg committed May 21, 2011
1 parent c4810ab commit 7e1fed9
Showing 1 changed file with 21 additions and 10 deletions.
Expand Up @@ -2,7 +2,7 @@
;; offering queue-like peek/pop as well as the map-like ability to
;; easily reassign priorities and other conveniences.
;; by Mark Engelberg (mark.engelberg@gmail.com)
;; July 16, 2010
;; May 20, 2011

(ns
^{:author "Mark Engelberg",
Expand Down Expand Up @@ -211,6 +211,7 @@ to Clojure's assortment of built-in maps (hash-map and sorted-map).
; their key-value pairs are the same.
(equiv [this o] (= item->priority o))
(hashCode [this] (.hashCode item->priority))
(equals [this o] (= item->priority o))

;containsKey implements (contains? pm k) behavior
(containsKey [this item] (contains? item->priority item))
Expand All @@ -227,17 +228,22 @@ to Clojure's assortment of built-in maps (hash-map and sorted-map).
;without implements (dissoc pm k) behavior
(without
[this item]
(let [priority (item->priority item),
item-set (priority->set-of-items priority),]
(if (= (count item-set) 1)
;If it is the only item with this priority, remove that priority's set completely
(PersistentPriorityMap. (dissoc priority->set-of-items priority) (dissoc item->priority item))
;Otherwise, just remove the item from the priority's set.
(PersistentPriorityMap.
(assoc priority->set-of-items priority (disj item-set item)),
(dissoc item->priority item)))))
(let [priority (item->priority item :not-found)]
(if (= priority :not-found)
;; If item is not in map, return the map unchanged.
this
(let [item-set (priority->set-of-items priority)]
(if (= (count item-set) 1)
;;If it is the only item with this priority, remove that priority's set completely
(PersistentPriorityMap. (dissoc priority->set-of-items priority)
(dissoc item->priority item))
;;Otherwise, just remove the item from the priority's set.
(PersistentPriorityMap.
(assoc priority->set-of-items priority (disj item-set item)),
(dissoc item->priority item)))))))

java.io.Serializable ;Serialization comes for free with the other things implemented
clojure.lang.MapEquivalence
Map ;Makes this compatible with java's map
(size [this] (count item->priority))
(isEmpty [this] (zero? (count item->priority)))
Expand Down Expand Up @@ -320,6 +326,8 @@ Returns a new priority map with supplied mappings"
h {:a 2 :b 1 :c 3 :d 5 :e 4 :f 3}]
(are [x y] (= x y)
p {:a 2 :b 1 :c 3 :d 5 :e 4 :f 3}
h p
(priority-map 1 2) (priority-map 1 2)
(.hashCode p) (.hashCode {:a 2 :b 1 :c 3 :d 5 :e 4 :f 3})
(assoc p :g 1) (assoc h :g 1)
(assoc p :g 0) (assoc h :g 0)
Expand All @@ -330,6 +338,9 @@ Returns a new priority map with supplied mappings"
(dissoc p :e) (dissoc h :e)
(dissoc p :g) (dissoc h :g)
(dissoc p :c) (dissoc h :c)
(dissoc p :x) p
(peek (dissoc p :x)) (peek p)
(pop (dissoc p :x)) (pop p)
(conj p [:g 1]) (conj h [:g 1])
(conj p [:g 0]) (conj h [:g 0])
(conj p [:c 4]) (conj h [:c 4])
Expand Down

0 comments on commit 7e1fed9

Please sign in to comment.