I donβt think there is an easy way to wrap a mutable data structure as immutable. Inevitable data structures become effective when a new version can use data with the old version in smart ways, and I cannot figure out how to do this without access to the internal PriorityQueue elements.
If you really need a constant priority queue, this thread can be interesting. However, they seem to have linear time inserts, so if this is a problem, you may have to look for another implementation.
Edit: On the other hand, a simple implementation of a constant priority queue is to simply store (prio, value) pairs in a sorted set. Something like that:
(defn make-pqueue [] (sorted-set)) (defn pqueue-add [pq x prio] (conj pq [prio x])) (defn pqueue-peek [pq] (first pq)) (defn pqueue-pop [pq] (let [top (first pq)] (disj pq top)))
Of course, the code above is rather limited (for example, there are not a few entries), but it illustrates the idea.
CAdaker
source share