pax_global_header00006660000000000000000000000064125066242170014517gustar00rootroot0000000000000052 comment=41ecd9f1df9b3b9f84b4f366ad6cce16cf9c3779 libdata-priority-map-clojure-0.0.7/000075500000000000000000000000001250662421700172145ustar00rootroot00000000000000libdata-priority-map-clojure-0.0.7/.gitignore000064400000000000000000000001771250662421700212110ustar00rootroot00000000000000*.jar .classpath .project .settings bin classes clojure-src.jar clojure-contrib.jar clojure.jar clojure-contrib-src.jar target libdata-priority-map-clojure-0.0.7/CONTRIBUTING.md000064400000000000000000000012231250662421700214430ustar00rootroot00000000000000This is a [Clojure contrib] project. Under the Clojure contrib [guidelines], this project cannot accept pull requests. All patches must be submitted via [JIRA]. See [Contributing] and the [FAQ] on the Clojure development [wiki] for more information on how to contribute. [Clojure contrib]: http://dev.clojure.org/display/doc/Clojure+Contrib [Contributing]: http://dev.clojure.org/display/community/Contributing [FAQ]: http://dev.clojure.org/display/community/Contributing+FAQ [JIRA]: http://dev.clojure.org/jira/browse/DPRIMAP [guidelines]: http://dev.clojure.org/display/community/Guidelines+for+Clojure+Contrib+committers [wiki]: http://dev.clojure.org/ libdata-priority-map-clojure-0.0.7/README.md000064400000000000000000000127341250662421700205020ustar00rootroot00000000000000# clojure.data.priority-map Formerly clojure.contrib.priority-map. A priority map is very similar to a sorted map, but whereas a sorted map produces a sequence of the entries sorted by key, a priority map produces the entries sorted by value. In addition to supporting all the functions a sorted map supports, a priority map can also be thought of as a queue of [item priority] pairs. To support usage as a versatile priority queue, priority maps also support conj/peek/pop operations. ## Releases and Dependency Information Latest stable release is [0.0.7] [Leiningen](https://github.com/technomancy/leiningen) dependency information: [org.clojure/data.priority-map "0.0.7"] [Maven](http://maven.apache.org/) dependency information: org.clojure data.priority-map 0.0.7 ## Usage The standard way to construct a priority map is with priority-map: user=> (def p (priority-map :a 2 :b 1 :c 3 :d 5 :e 4 :f 3)) #'user/p user=> p {:b 1, :a 2, :c 3, :f 3, :e 4, :d 5} So :b has priority 1, :a has priority 2, and so on. Notice how the priority map prints in an order sorted by its priorities (i.e., the map's values) We can use assoc to assign a priority to a new item: user=> (assoc p :g 1) {:b 1, :g 1, :a 2, :c 3, :f 3, :e 4, :d 5} or to assign a new priority to an extant item: user=> (assoc p :c 4) {:b 1, :a 2, :f 3, :c 4, :e 4, :d 5} We can remove an item from the priority map: user=> (dissoc p :e) {:b 1, :a 2, :c 3, :f 3, :d 5} An alternative way to add to the priority map is to conj a [item priority] pair: user=> (conj p [:g 0]) {:g 0, :b 1, :a 2, :c 3, :f 3, :e 4, :d 5} or use into: user=> (into p [[:g 0] [:h 1] [:i 2]]) {:g 0, :b 1, :h 1, :a 2, :i 2, :c 3, :f 3, :e 4, :d 5} Priority maps are countable: user=> (count p) 6 Like other maps, equivalence is based not on type, but on contents. In other words, just as a sorted-map can be equal to a hash-map, so can a priority-map. user=> (= p {:b 1, :a 2, :c 3, :f 3, :e 4, :d 5}) true You can test them for emptiness: user=> (empty? (priority-map)) true user=> (empty? p) false You can test whether an item is in the priority map: user=> (contains? p :a) true user=> (contains? p :g) false It is easy to look up the priority of a given item, using any of the standard map mechanisms: user=> (get p :a) 2 user=> (get p :g 10) 10 user=> (p :a) 2 user=> (:a p) 2 Priority maps derive much of their utility by providing priority-based seq. Note that no guarantees are made about the order in which items of the same priority appear. user=> (seq p) ([:b 1] [:a 2] [:c 3] [:f 3] [:e 4] [:d 5]) Because no guarantees are made about the order of same-priority items, note that rseq might not be an exact reverse of the seq. It is only guaranteed to be in descending order. user=> (rseq p) ([:d 5] [:e 4] [:c 3] [:f 3] [:a 2] [:b 1]) This means first/rest/next/for/map/etc. all operate in priority order. user=> (first p) [:b 1] user=> (rest p) ([:a 2] [:c 3] [:f 3] [:e 4] [:d 5]) Priority maps support metadata: user=> (meta (with-meta p {:extra :info})) {:extra :info} But perhaps most importantly, priority maps can also function as priority queues. peek, like first, gives you the first [item priority] pair in the collection. pop removes the first [item priority] from the collection. (Note that unlike rest, which returns a seq, pop returns a priority map). user=> (peek p) [:b 1] user=> (pop p) {:a 2, :c 3, :f 3, :e 4, :d 5} It is also possible to use a custom comparator: user=> (priority-map-by > :a 1 :b 2 :c 3) {:c 3, :b 2, :a 1} Sometimes, it is desirable to have a map where the values contain more information than just the priority. For example, let's say you want a map like: {:a [2 :apple], :b [1 :banana], :c [3 :carrot]} and you want to sort the map by the numeric priority found in the pair. A common mistake is to try to solve this with a custom comparator: (priority-map (fn [[priority1 _] [priority2 _]] (< priority1 priority2)) :a [2 :apple], :b [1 :banana], :c [3 :carrot]) This will not work! In Clojure, like Java, all comparators must be *total orders*, meaning that you can't have a "tie" unless the objects you are comparing are in fact equal. The above comparator breaks that rule because `[2 :apple]` and `[2 :apricot]` tie, but are not equal. The correct way to construct such a priority map is by specifying a keyfn, which is used to compute or extract the true priority from the priority map's vals. (Note: It might seem a little odd that the priority-extraction function is called a *key*fn, even though it is applied to the map's values. This terminology is based on the docstring of clojure.core/sort-by, which uses `keyfn` for the function which computes the *sort keys*.) In the above example, user=> (priority-map-keyfn first :a [2 :apple], :b [1 :banana], :c [3 :carrot]) {:b [1 :banana], :a [2 :apple], :c [3 :carrot]} You can also combine a keyfn with a comparator that operates on the extracted priorities: user=> (priority-map-keyfn-by first > :a [2 :apple], :b [1 :banana], :c [3 :carrot]) {:c [3 :carrot], :a [2 :apple], :b [1 :banana]} ## License Copyright (C) 2013 Mark Engelberg Distributed under the Eclipse Public License, the same as Clojure. libdata-priority-map-clojure-0.0.7/epl.html000064400000000000000000000305361250662421700206710ustar00rootroot00000000000000 Eclipse Public License - Version 1.0

Eclipse Public License - v 1.0

THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.

1. DEFINITIONS

"Contribution" means:

a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and

b) in the case of each subsequent Contributor:

i) changes to the Program, and

ii) additions to the Program;

where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program.

"Contributor" means any person or entity that distributes the Program.

"Licensed Patents" mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program.

"Program" means the Contributions distributed in accordance with this Agreement.

"Recipient" means anyone who receives the Program under this Agreement, including all Contributors.

2. GRANT OF RIGHTS

a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form.

b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder.

c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program.

d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement.

3. REQUIREMENTS

A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that:

a) it complies with the terms and conditions of this Agreement; and

b) its license agreement:

i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose;

ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits;

iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and

iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange.

When the Program is made available in source code form:

a) it must be made available under this Agreement; and

b) a copy of this Agreement must be included with each copy of the Program.

Contributors may not remove or alter any copyright notices contained within the Program.

Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution.

4. COMMERCIAL DISTRIBUTION

Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense.

For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages.

5. NO WARRANTY

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement , including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations.

6. DISCLAIMER OF LIABILITY

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

7. GENERAL

If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.

If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed.

All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive.

Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved.

This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation.

libdata-priority-map-clojure-0.0.7/pom.xml000064400000000000000000000015711250662421700205350ustar00rootroot00000000000000 4.0.0 data.priority-map 0.0.7 ${project.artifactId} org.clojure pom.contrib 0.1.2 Mark Engelberg scm:git:git@github.com:clojure/data.priority-map.git scm:git:git@github.com:clojure/data.priority-map.git git@github.com:clojure/data.priority-map.git data.priority-map-0.0.7 libdata-priority-map-clojure-0.0.7/src/000075500000000000000000000000001250662421700200035ustar00rootroot00000000000000libdata-priority-map-clojure-0.0.7/src/main/000075500000000000000000000000001250662421700207275ustar00rootroot00000000000000libdata-priority-map-clojure-0.0.7/src/main/clojure/000075500000000000000000000000001250662421700223725ustar00rootroot00000000000000libdata-priority-map-clojure-0.0.7/src/main/clojure/clojure/000075500000000000000000000000001250662421700240355ustar00rootroot00000000000000libdata-priority-map-clojure-0.0.7/src/main/clojure/clojure/data/000075500000000000000000000000001250662421700247465ustar00rootroot00000000000000libdata-priority-map-clojure-0.0.7/src/main/clojure/clojure/data/priority_map.clj000064400000000000000000000427241250662421700301670ustar00rootroot00000000000000;; A priority map is a map from items to priorities, ;; 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) ;; October 31, 2013 (ns ^{:author "Mark Engelberg", :doc "A priority map is very similar to a sorted map, but whereas a sorted map produces a sequence of the entries sorted by key, a priority map produces the entries sorted by value. In addition to supporting all the functions a sorted map supports, a priority map can also be thought of as a queue of [item priority] pairs. To support usage as a versatile priority queue, priority maps also support conj/peek/pop operations. The standard way to construct a priority map is with priority-map: user=> (def p (priority-map :a 2 :b 1 :c 3 :d 5 :e 4 :f 3)) #'user/p user=> p {:b 1, :a 2, :c 3, :f 3, :e 4, :d 5} So :b has priority 1, :a has priority 2, and so on. Notice how the priority map prints in an order sorted by its priorities (i.e., the map's values) We can use assoc to assign a priority to a new item: user=> (assoc p :g 1) {:b 1, :g 1, :a 2, :c 3, :f 3, :e 4, :d 5} or to assign a new priority to an extant item: user=> (assoc p :c 4) {:b 1, :a 2, :f 3, :c 4, :e 4, :d 5} We can remove an item from the priority map: user=> (dissoc p :e) {:b 1, :a 2, :c 3, :f 3, :d 5} An alternative way to add to the priority map is to conj a [item priority] pair: user=> (conj p [:g 0]) {:g 0, :b 1, :a 2, :c 3, :f 3, :e 4, :d 5} or use into: user=> (into p [[:g 0] [:h 1] [:i 2]]) {:g 0, :b 1, :h 1, :a 2, :i 2, :c 3, :f 3, :e 4, :d 5} Priority maps are countable: user=> (count p) 6 Like other maps, equivalence is based not on type, but on contents. In other words, just as a sorted-map can be equal to a hash-map, so can a priority-map. user=> (= p {:b 1, :a 2, :c 3, :f 3, :e 4, :d 5}) true You can test them for emptiness: user=> (empty? (priority-map)) true user=> (empty? p) false You can test whether an item is in the priority map: user=> (contains? p :a) true user=> (contains? p :g) false It is easy to look up the priority of a given item, using any of the standard map mechanisms: user=> (get p :a) 2 user=> (get p :g 10) 10 user=> (p :a) 2 user=> (:a p) 2 Priority maps derive much of their utility by providing priority-based seq. Note that no guarantees are made about the order in which items of the same priority appear. user=> (seq p) ([:b 1] [:a 2] [:c 3] [:f 3] [:e 4] [:d 5]) Because no guarantees are made about the order of same-priority items, note that rseq might not be an exact reverse of the seq. It is only guaranteed to be in descending order. user=> (rseq p) ([:d 5] [:e 4] [:c 3] [:f 3] [:a 2] [:b 1]) This means first/rest/next/for/map/etc. all operate in priority order. user=> (first p) [:b 1] user=> (rest p) ([:a 2] [:c 3] [:f 3] [:e 4] [:d 5]) Priority maps support metadata: user=> (meta (with-meta p {:extra :info})) {:extra :info} But perhaps most importantly, priority maps can also function as priority queues. peek, like first, gives you the first [item priority] pair in the collection. pop removes the first [item priority] from the collection. (Note that unlike rest, which returns a seq, pop returns a priority map). user=> (peek p) [:b 1] user=> (pop p) {:a 2, :c 3, :f 3, :e 4, :d 5} It is also possible to use a custom comparator: user=> (priority-map-by > :a 1 :b 2 :c 3) {:c 3, :b 2, :a 1} Sometimes, it is desirable to have a map where the values contain more information than just the priority. For example, let's say you want a map like: {:a [2 :apple], :b [1 :banana], :c [3 :carrot]} and you want to sort the map by the numeric priority found in the pair. A common mistake is to try to solve this with a custom comparator: (priority-map (fn [[priority1 _] [priority2 _]] (< priority1 priority2)) :a [2 :apple], :b [1 :banana], :c [3 :carrot]) This will not work! In Clojure, like Java, all comparators must be total orders, meaning that you can't have a tie unless the objects you are comparing are in fact equal. The above comparator breaks that rule because [2 :apple] and [2 :apricot] tie, but are not equal. The correct way to construct such a priority map is by specifying a keyfn, which is used to extract the true priority from the priority map's vals. (Note: It might seem a little odd that the priority-extraction function is called a *key*fn, even though it is applied to the map's values. This terminology is based on the docstring of clojure.core/sort-by, which uses `keyfn` for the function which extracts the sort order.) In the above example, user=> (priority-map-keyfn first :a [2 :apple], :b [1 :banana], :c [3 :carrot]) {:b [1 :banana], :a [2 :apple], :c [3 :carrot]} You can also combine a keyfn with a comparator that operates on the extracted priorities: user=> (priority-map-keyfn-by first > :a [2 :apple], :b [1 :banana], :c [3 :carrot]) {:c [3 :carrot], :a [2 :apple], :b [1 :banana]} All of these operations are efficient. Generally speaking, most operations are O(log n) where n is the number of distinct priorities. Some operations (for example, straightforward lookup of an item's priority, or testing whether a given item is in the priority map) are as efficient as Clojure's built-in map. The key to this efficiency is that internally, not only does the priority map store an ordinary hash map of items to priority, but it also stores a sorted map that maps priorities to sets of items with that priority. A typical textbook priority queue data structure supports at the ability to add a [item priority] pair to the queue, and to pop/peek the next [item priority] pair. But many real-world applications of priority queues require more features, such as the ability to test whether something is already in the queue, or to reassign a priority. For example, a standard formulation of Dijkstra's algorithm requires the ability to reduce the priority number associated with a given item. Once you throw persistence into the mix with the desire to adjust priorities, the traditional structures just don't work that well. This particular blend of Clojure's built-in hash sets, hash maps, and sorted maps proved to be a great way to implement an especially flexible persistent priority queue. Connoisseurs of algorithms will note that this structure's peek operation is not O(1) as it would be if based upon a heap data structure, but I feel this is a small concession for the blend of persistence, priority reassignment, and priority-sorted seq, which can be quite expensive to achieve with a heap (I did actually try this for comparison). Furthermore, this peek's logarithmic behavior is quite good (on my computer I can do a million peeks at a priority map with a million items in 750ms). Also, consider that peek and pop usually follow one another, and even with a heap, pop is logarithmic. So the net combination of peek and pop is not much different between this versatile formulation of a priority map and a more limited heap-based one. In a nutshell, peek, although not O(1), is unlikely to be the bottleneck in your program. All in all, I hope you will find priority maps to be an easy-to-use and useful addition to Clojure's assortment of built-in maps (hash-map and sorted-map). "} clojure.data.priority-map (:import clojure.lang.MapEntry java.util.Map clojure.lang.PersistentTreeMap)) ; Note that the plan is to eventually support subseq, but this will require ; some changes to core: ;; user=> (subseq p < 3) ;; ([:b 1] [:a 2]) ;; user=> (subseq p >= 3) ;; ([:c 3] [:f 3] [:e 4] [:d 5]) (declare pm-empty) (defmacro apply-keyfn [x] `(if ~'keyfn (~'keyfn ~x) ~x)) ; A Priority Map is comprised of a sorted map that maps priorities to hash sets of items ; with that priority (priority->set-of-items), ; as well as a hash map that maps items to priorities (item->priority) ; Priority maps may also have metadata ; Priority maps can also have a keyfn which is applied to the "priorities" found as values in ; the item->priority map to get the actual sortable priority keys used in priority->set-of-items. (deftype PersistentPriorityMap [priority->set-of-items item->priority _meta keyfn] Object (toString [this] (str (.seq this))) clojure.lang.ILookup ; valAt gives (get pm key) and (get pm key not-found) behavior (valAt [this item] (get item->priority item)) (valAt [this item not-found] (get item->priority item not-found)) clojure.lang.IPersistentMap (count [this] (count item->priority)) (assoc [this item priority] (let [current-priority (get item->priority item nil)] (if current-priority ;Case 1 - item is already in priority map, so this is a reassignment (if (= current-priority priority) ;Subcase 1 - no change in priority, do nothing this (let [priority-key (apply-keyfn priority) current-priority-key (apply-keyfn current-priority) item-set (get priority->set-of-items current-priority-key)] (if (= (count item-set) 1) ;Subcase 2 - it was the only item of this priority ;so remove old priority entirely ;and conj item onto new priority's set (PersistentPriorityMap. (assoc (dissoc priority->set-of-items current-priority-key) priority-key (conj (get priority->set-of-items priority-key #{}) item)) (assoc item->priority item priority) (meta this) keyfn) ;Subcase 3 - there were many items associated with the item's original priority, ;so remove it from the old set and conj it onto the new one. (PersistentPriorityMap. (assoc priority->set-of-items current-priority-key (disj (get priority->set-of-items current-priority-key) item) priority-key (conj (get priority->set-of-items priority-key #{}) item)) (assoc item->priority item priority) (meta this) keyfn)))) ; Case 2: Item is new to the priority map, so just add it. (let [priority-key (apply-keyfn priority)] (PersistentPriorityMap. (assoc priority->set-of-items priority-key (conj (get priority->set-of-items priority-key #{}) item)) (assoc item->priority item priority) (meta this) keyfn))))) (empty [this] (PersistentPriorityMap. (empty priority->set-of-items) {} _meta keyfn)) ; cons defines conj behavior (cons [this e] (if (map? e) (into this e) (let [[item priority] e] (.assoc this item priority)))) ; Like sorted maps, priority maps are equal to other maps provided ; their key-value pairs are the same. (equiv [this o] (= item->priority o)) (hashCode [this] (.hashCode item->priority)) (equals [this o] (or (identical? this o) (.equals item->priority o))) ;containsKey implements (contains? pm k) behavior (containsKey [this item] (contains? item->priority item)) (entryAt [this k] (let [v (.valAt this k this)] (when-not (identical? v this) (MapEntry. k v)))) (seq [this] (if keyfn (seq (for [[priority item-set] priority->set-of-items, item item-set] (MapEntry. item (item->priority item)))) (seq (for [[priority item-set] priority->set-of-items, item item-set] (MapEntry. item priority))))) ;without implements (dissoc pm k) behavior (without [this item] (let [priority (item->priority item ::not-found)] (if (= priority ::not-found) ;; If item is not in map, return the map unchanged. this (let [priority-key (apply-keyfn priority) item-set (priority->set-of-items priority-key)] (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-key) (dissoc item->priority item) (meta this) keyfn) ;;Otherwise, just remove the item from the priority's set. (PersistentPriorityMap. (assoc priority->set-of-items priority-key (disj item-set item)), (dissoc item->priority item) (meta this) keyfn)))))) 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))) (containsValue [this v] (if keyfn (some (partial = v) (vals this)) ; no shortcut if there is a keyfn (contains? priority->set-of-items v))) (get [this k] (.valAt this k)) (put [this k v] (throw (UnsupportedOperationException.))) (remove [this k] (throw (UnsupportedOperationException.))) (putAll [this m] (throw (UnsupportedOperationException.))) (clear [this] (throw (UnsupportedOperationException.))) (keySet [this] (set (keys this))) (values [this] (vals this)) (entrySet [this] (set this)) Iterable (iterator [this] (clojure.lang.SeqIterator. (seq this))) clojure.lang.IPersistentStack (peek [this] (when-not (.isEmpty this) (let [f (first priority->set-of-items) item (first (val f))] (if keyfn (MapEntry. item (item->priority item)) (MapEntry. item (key f)))))) (pop [this] (if (.isEmpty this) (throw (IllegalStateException. "Can't pop empty priority map")) (let [f (first priority->set-of-items), item-set (val f) item (first item-set), priority-key (key f)] (if (= (count item-set) 1) ;If the first item is the only item with its priority, remove that priority's set completely (PersistentPriorityMap. (dissoc priority->set-of-items priority-key) (dissoc item->priority item) (meta this) keyfn) ;Otherwise, just remove the item from the priority's set. (PersistentPriorityMap. (assoc priority->set-of-items priority-key (disj item-set item)), (dissoc item->priority item) (meta this) keyfn))))) clojure.lang.IFn ;makes priority map usable as a function (invoke [this k] (.valAt this k)) (invoke [this k not-found] (.valAt this k not-found)) clojure.lang.IObj ;adds metadata support (meta [this] _meta) (withMeta [this m] (PersistentPriorityMap. priority->set-of-items item->priority m keyfn)) clojure.lang.Reversible (rseq [this] (if keyfn (seq (for [[priority item-set] (rseq priority->set-of-items), item item-set] (MapEntry. item (item->priority item)))) (seq (for [[priority item-set] (rseq priority->set-of-items), item item-set] (MapEntry. item priority)))))) ;; clojure.lang.Sorted ;; ; These methods provide support for subseq ;; (comparator [this] (.comparator ^PersistentTreeMap priority->set-of-items)) ;; (entryKey [this entry] (val entry)) ;; (seqFrom [this k ascending] ;; (let [sets (if ascending (subseq priority->set-of-items >= k) (rsubseq priority->set-of-items <= k))] ;; (seq (for [[priority item-set] sets, item item-set] ;; (MapEntry. item priority))))) ;; (seq [this ascending] ;; (if ascending (seq this) (rseq this)))) (def ^:private pm-empty (PersistentPriorityMap. (sorted-map) {} {} nil)) (defn- pm-empty-by [comparator] (PersistentPriorityMap. (sorted-map-by comparator) {} {} nil)) (defn- pm-empty-keyfn ([keyfn] (PersistentPriorityMap. (sorted-map) {} {} keyfn)) ([keyfn comparator] (PersistentPriorityMap. (sorted-map-by comparator) {} {} keyfn))) ; The main way to build priority maps (defn priority-map "Usage: (priority-map key val key val ...) Returns a new priority map with optional supplied mappings. (priority-map) returns an empty priority map." [& keyvals] {:pre [(even? (count keyvals))]} (reduce conj pm-empty (partition 2 keyvals))) (defn priority-map-by "Usage: (priority-map comparator key val key val ...) Returns a new priority map with custom comparator and optional supplied mappings. (priority-map-by comparator) yields an empty priority map with custom comparator." [comparator & keyvals] {:pre [(even? (count keyvals))]} (reduce conj (pm-empty-by comparator) (partition 2 keyvals))) (defn priority-map-keyfn "Usage: (priority-map-keyfn keyfn key val key val ...) Returns a new priority map with custom keyfn and optional supplied mappings. The priority is determined by comparing (keyfn val). (priority-map-keyfn keyfn) yields an empty priority map with custom keyfn." [keyfn & keyvals] {:pre [(even? (count keyvals))]} (reduce conj (pm-empty-keyfn keyfn) (partition 2 keyvals))) (defn priority-map-keyfn-by "Usage: (priority-map-keyfn-by keyfn comparator key val key val ...) Returns a new priority map with custom keyfn, custom comparator, and optional supplied mappings. The priority is determined by comparing (keyfn val). (priority-map-keyfn-by keyfn comparator) yields an empty priority map with custom keyfn and comparator." [keyfn comparator & keyvals] {:pre [(even? (count keyvals))]} (reduce conj (pm-empty-keyfn keyfn comparator) (partition 2 keyvals))) libdata-priority-map-clojure-0.0.7/src/test/000075500000000000000000000000001250662421700207625ustar00rootroot00000000000000libdata-priority-map-clojure-0.0.7/src/test/clojure/000075500000000000000000000000001250662421700224255ustar00rootroot00000000000000libdata-priority-map-clojure-0.0.7/src/test/clojure/clojure/000075500000000000000000000000001250662421700240705ustar00rootroot00000000000000libdata-priority-map-clojure-0.0.7/src/test/clojure/clojure/data/000075500000000000000000000000001250662421700250015ustar00rootroot00000000000000libdata-priority-map-clojure-0.0.7/src/test/clojure/clojure/data/test_priority_map.clj000064400000000000000000000143671250662421700312630ustar00rootroot00000000000000(ns clojure.data.test-priority-map (:use clojure.test clojure.data.priority-map)) (deftest test-priority-map (let [p (priority-map :a 2 :b 1 :c 3 :d 5 :e 4 :f 3) 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) (assoc p :c 4) (assoc h :c 4) (assoc p :c 6) (assoc h :c 6) (assoc p :b 2) (assoc h :b 2) (assoc p :b 6) (assoc h :b 6) (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]) (conj p [:c 6]) (conj h [:c 6]) (conj p [:b 2]) (conj h [:b 2]) (conj p [:b 6]) (conj h [:b 6]) (conj p {:b 6}) (conj h {:b 6}) (into p [[:g 0] [:h 1] [:i 2]]) (into h [[:g 0] [:h 1] [:i 2]]) (count p) (count h) (empty? p) false (empty? (priority-map)) true (contains? p :a) true (contains? p :g) false (get p :a) 2 (get p :a 8) 2 (get p :g) nil (get p :g 8) 8 (p :a) 2 (:a p) 2 ;; (subseq p < 3) '([:b 1] [:a 2]) ;; (subseq p <= 3) '([:b 1] [:a 2] [:c 3] [:f 3]) ;; (subseq p > 3) '([:e 4] [:d 5]) ;; (subseq p >= 3) '([:c 3] [:f 3] [:e 4] [:d 5]) ;; (subseq p > 3 <= 4) '([:e 4]) ;; (subseq p >= 3 <= 4) '([:c 3] [:f 3] [:e 4]) ;; (subseq p >= 3 < 4) '([:c 3] [:f 3]) ;; (subseq p > 3 < 4) nil ;; (subseq p > 2 < 3) nil ;; (subseq p > 2 <= 3) '([:c 3] [:f 3]) ;; (subseq p >= 2 < 3) '([:a 2]) ;; (subseq p >= 2 <= 3) '([:a 2] [:c 3] [:f 3]) ;; (rsubseq p < 3) '([:a 2] [:b 1]) ;; (rsubseq p <= 3) '([:c 3] [:f 3] [:a 2] [:b 1] ) ;; (rsubseq p > 3) '([:d 5] [:e 4]) ;; (rsubseq p >= 3) '([:d 5] [:e 4] [:c 3] [:f 3]) ;; (rsubseq p > 3 <= 4) '([:e 4]) ;; (rsubseq p >= 3 <= 4) '([:e 4] [:c 3] [:f 3] ) ;; (rsubseq p >= 3 < 4) '([:c 3] [:f 3]) ;; (rsubseq p > 3 < 4) nil ;; (rsubseq p > 2 < 3) nil ;; (rsubseq p > 2 <= 3) '([:c 3] [:f 3]) ;; (rsubseq p >= 2 < 3) '([:a 2]) ;; (rsubseq p >= 2 <= 3) '([:c 3] [:f 3] [:a 2] ) (first p) [:b 1] (meta (with-meta p {:extra :info})) {:extra :info} (peek p) [:b 1] (pop p) {:a 2 :c 3 :f 3 :e 4 :d 5} (peek (priority-map)) nil (seq (priority-map-by (comparator >) :a 1 :b 2 :c 3)) [[:c 3] [:b 2] [:a 1]]))) (deftest test-priority-map-with-flexible-order ;Note when implementation of hash-set changed, ;we need to consider that the :c and :f entries might be swapped (let [p (priority-map :a 2 :b 1 :c 3 :d 5 :e 4 :f 3) h {:a 2 :b 1 :c 3 :d 5 :e 4 :f 3}] (are [x y z] (or (= x y) (= x z)) (seq p) '([:b 1] [:a 2] [:c 3] [:f 3] [:e 4] [:d 5]) '([:b 1] [:a 2] [:f 3] [:c 3] [:e 4] [:d 5]) (rseq p) '([:d 5] [:e 4] [:c 3] [:f 3] [:a 2] [:b 1]) '([:d 5] [:e 4] [:f 3] [:c 3] [:a 2] [:b 1]) (rest p) '([:a 2] [:c 3] [:f 3] [:e 4] [:d 5]) '([:a 2] [:f 3] [:c 3] [:e 4] [:d 5])))) (deftest test-priority-map-keyfn (let [p (priority-map-keyfn first :a [2 :a] :b [1 :b] :c [3 :c] :d [5 :d] :e [4 :e] :f [3 :f]) h {:a [2 :a] :b [1 :b] :c [3 :c] :d [5 :d] :e [4 :e] :f [3 :f]}] (are [x y] (= x y) p h h p (.hashCode p) (.hashCode h) (assoc p :g [1 :g]) (assoc h :g [1 :g]) (assoc p :g [0 :g]) (assoc h :g [0 :g]) (assoc p :c [4 :c]) (assoc h :c [4 :c]) (assoc p :c [6 :c]) (assoc h :c [6 :c]) (assoc p :b [2 :b]) (assoc h :b [2 :b]) (assoc p :b [6 :b]) (assoc h :b [6 :b]) (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 :g]]) (conj h [:g [1 :g]]) (conj p [:g [0 :g]]) (conj h [:g [0 :g]]) (conj p [:c [4 :c]]) (conj h [:c [4 :c]]) (conj p [:c [6 :c]]) (conj h [:c [6 :c]]) (conj p [:b [2 :b]]) (conj h [:b [2 :b]]) (conj p [:b [6 :b]]) (conj h [:b [6 :b]]) (into p [[:g [0 :g]] [:h [1 :h]] [:i [2 :i]]]) (into h [[:g [0 :g]] [:h [1 :h]] [:i [2 :i]]]) (count p) (count h) (empty? p) false (empty? (priority-map-keyfn first)) true (contains? p :a) true (contains? p :g) false (get p :a) [2 :a] (get p :a 8) [2 :a] (get p :g) nil (get p :g 8) 8 (p :a) [2 :a] (:a p) [2 :a] (first p) [:b [1 :b]] (meta (with-meta p {:extra :info})) {:extra :info} (peek p) [:b [1 :b]] (pop p) {:a [2 :a] :c [3 :c] :f [3 :f] :e [4 :e] :d [5 :d]} (into (empty (priority-map-by >)) [[:a 2] [:b 1] [:c 3] [:d 5] [:e 4] [:f 3]]) {:d 5, :e 4, :c 3, :f 3, :a 2, :b 1} (peek (priority-map-keyfn first)) nil (seq (into (empty (priority-map-keyfn-by first (comparator >))) [[:a [1 :a]] [:b [2 :b]] [:c [3 :c]]])) '([:c [3 :c]] [:b [2 :b]] [:a [1 :a]]) (seq (priority-map-keyfn-by first (comparator >) :a [1 :a] :b [2 :b] :c [3 :c])) [[:c [3 :c]] [:b [2 :b]] [:a [1 :a]]]))) (deftest test-priority-map-keyfn-with-flexible-order ;Note when implementation of hash-set changed, ;we need to consider that the :c and :f entries might be swapped (let [p (priority-map-keyfn first :a [2 :a] :b [1 :b] :c [3 :c] :d [5 :d] :e [4 :e] :f [3 :f]) h {:a [2 :a] :b [1 :b] :c [3 :c] :d [5 :d] :e [4 :e] :f [3 :f]}] (are [x y z] (or (= x y) (= x z)) (seq p) '([:b [1 :b]] [:a [2 :a]] [:c [3 :c]] [:f [3 :f]] [:e [4 :e]] [:d [5 :d]]) '([:b [1 :b]] [:a [2 :a]] [:f [3 :f]] [:c [3 :c]] [:e [4 :e]] [:d [5 :d]]) (rseq p) '([:d [5 :d]] [:e [4 :e]] [:c [3 :c]] [:f [3 :f]] [:a [2 :a]] [:b [1 :b]]) '([:d [5 :d]] [:e [4 :e]] [:f [3 :f]] [:c [3 :c]] [:a [2 :a]] [:b [1 :b]]) (rest p) '([:a [2 :a]] [:c [3 :c]] [:f [3 :f]] [:e [4 :e]] [:d [5 :d]]) '([:a [2 :a]] [:f [3 :f]] [:c [3 :c]] [:e [4 :e]] [:d [5 :d]]))))