Download code

From LiteratePrograms

Jump to: navigation, search

Back to Priority_Queue_(Haskell)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

PriorityQueue.hs

 1 {- Copyright (c) 2010 the authors listed at the following URL, and/or
 2 the authors of referenced articles or incorporated external code:
 3 http://en.literateprograms.org/Priority_Queue_(Haskell)?action=history&offset=20080608152146
 4 
 5 Permission is hereby granted, free of charge, to any person obtaining
 6 a copy of this software and associated documentation files (the
 7 "Software"), to deal in the Software without restriction, including
 8 without limitation the rights to use, copy, modify, merge, publish,
 9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 Retrieved from: http://en.literateprograms.org/Priority_Queue_(Haskell)?oldid=13634
25 -}
26 
27 module PriorityQueue (
28     PriorityQueue,
29     empty,
30     minKey,
31     minKeyValue,
32     insert,
33     deleteMinAndInsert
34 ) where
35 
36  
37 import Prelude
38 
39 
40 -- Declare the data type constructors.

41 data Ord k => PriorityQueue k a = Nil | Branch k a (PriorityQueue k a) (PriorityQueue k a)
42  
43 
44 -- Declare the exported interface functions.

45 -- Return an empty priority queue.

46 empty :: Ord k => PriorityQueue k a
47 empty = Nil
48 
49 
50 -- Return the highest-priority key.

51 minKey :: Ord k => PriorityQueue k a -> k
52 minKey = fst . minKeyValue
53 
54 
55 -- Return the highest-priority key plus its associated value.

56 minKeyValue :: Ord k => PriorityQueue k a -> (k, a)
57 minKeyValue Nil              = error "empty queue"
58 minKeyValue (Branch k a _ _) = (k, a)
59 
60 
61 -- Insert a key/value pair into a queue.

62 insert :: Ord k => k -> a -> PriorityQueue k a -> PriorityQueue k a
63 insert k a q = union (singleton k a) q
64 
65 
66 -- Delete the highest-priority key/value pair and insert a new key/value pair into the queue.

67 deleteMinAndInsert :: Ord k => k -> a -> PriorityQueue k a -> PriorityQueue k a
68 deleteMinAndInsert k a Nil              = singleton k a
69 deleteMinAndInsert k a (Branch _ _ l r) = union (insert k a l) r
70 
71 
72 
73 -- Declare the private helper functions.

74 -- Join two queues in sorted order.

75 union :: Ord k => PriorityQueue k a -> PriorityQueue k a -> PriorityQueue k a
76 union l Nil = l
77 union Nil r = r
78 union l@(Branch kl _ _ _) r@(Branch kr _ _ _)
79     | kl <= kr  = link l r
80     | otherwise = link r l
81 
82 
83 -- Join two queues without regard to order.

84 -- (This is a helper to the union helper.)

85 link (Branch k a Nil m) r = Branch k a r m
86 link (Branch k a ll lr) r = Branch k a lr (union ll r)
87 
88 
89 -- Return a queue with a single item from a key/value pair.

90 singleton :: Ord k => k -> a -> PriorityQueue k a
91 singleton k a = Branch k a Nil Nil
92 
93 
94 


Views
Personal tools