[project @ 2005-01-29 16:10:27 by wolfgang]
[ghc-base.git] / Data / Map.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Data.Map
4 -- Copyright   :  (c) Daan Leijen 2002
5 -- License     :  BSD-style
6 -- Maintainer  :  libraries@haskell.org
7 -- Stability   :  provisional
8 -- Portability :  portable
9 --
10 -- An efficient implementation of maps from keys to values (dictionaries).
11 --
12 -- This module is intended to be imported @qualified@, to avoid name
13 -- clashes with Prelude functions.  eg.
14 --
15 -- >  import Data.Map as Map
16 --
17 -- The implementation of 'Map' is based on /size balanced/ binary trees (or
18 -- trees of /bounded balance/) as described by:
19 --
20 --    * Stephen Adams, \"/Efficient sets: a balancing act/\",
21 --      Journal of Functional Programming 3(4):553-562, October 1993,
22 --      <http://www.swiss.ai.mit.edu/~adams/BB>.
23 --
24 --    * J. Nievergelt and E.M. Reingold,
25 --      \"/Binary search trees of bounded balance/\",
26 --      SIAM journal of computing 2(1), March 1973.
27 -----------------------------------------------------------------------------
28
29 module Data.Map  ( 
30             -- * Map type
31               Map          -- instance Eq,Show
32
33             -- * Operators
34             , (!), (\\)
35
36
37             -- * Query
38             , null
39             , size
40             , member
41             , lookup
42             , findWithDefault
43             
44             -- * Construction
45             , empty
46             , singleton
47
48             -- ** Insertion
49             , insert
50             , insertWith, insertWithKey, insertLookupWithKey
51             
52             -- ** Delete\/Update
53             , delete
54             , adjust
55             , adjustWithKey
56             , update
57             , updateWithKey
58             , updateLookupWithKey
59
60             -- * Combine
61
62             -- ** Union
63             , union         
64             , unionWith          
65             , unionWithKey
66             , unions
67             , unionsWith
68
69             -- ** Difference
70             , difference
71             , differenceWith
72             , differenceWithKey
73             
74             -- ** Intersection
75             , intersection           
76             , intersectionWith
77             , intersectionWithKey
78
79             -- * Traversal
80             -- ** Map
81             , map
82             , mapWithKey
83             , mapAccum
84             , mapAccumWithKey
85             , mapKeys
86             , mapKeysWith
87             , mapKeysMonotonic
88
89             -- ** Fold
90             , fold
91             , foldWithKey
92
93             -- * Conversion
94             , elems
95             , keys
96             , keysSet
97             , assocs
98             
99             -- ** Lists
100             , toList
101             , fromList
102             , fromListWith
103             , fromListWithKey
104
105             -- ** Ordered lists
106             , toAscList
107             , fromAscList
108             , fromAscListWith
109             , fromAscListWithKey
110             , fromDistinctAscList
111
112             -- * Filter 
113             , filter
114             , filterWithKey
115             , partition
116             , partitionWithKey
117
118             , split         
119             , splitLookup   
120
121             -- * Submap
122             , isSubmapOf, isSubmapOfBy
123             , isProperSubmapOf, isProperSubmapOfBy
124
125             -- * Indexed 
126             , lookupIndex
127             , findIndex
128             , elemAt
129             , updateAt
130             , deleteAt
131
132             -- * Min\/Max
133             , findMin
134             , findMax
135             , deleteMin
136             , deleteMax
137             , deleteFindMin
138             , deleteFindMax
139             , updateMin
140             , updateMax
141             , updateMinWithKey
142             , updateMaxWithKey
143             
144             -- * Debugging
145             , showTree
146             , showTreeWith
147             , valid
148             ) where
149
150 import Prelude hiding (lookup,map,filter,foldr,foldl,null)
151 import Data.Monoid
152 import qualified Data.Set as Set
153 import qualified Data.List as List
154 import Data.Typeable
155
156 {-
157 -- for quick check
158 import qualified Prelude
159 import qualified List
160 import Debug.QuickCheck       
161 import List(nub,sort)    
162 -}
163
164 #if __GLASGOW_HASKELL__
165 import Data.Generics.Basics
166 import Data.Generics.Instances
167 #endif
168
169 {--------------------------------------------------------------------
170   Operators
171 --------------------------------------------------------------------}
172 infixl 9 !,\\ --
173
174 -- | /O(log n)/. Find the value at a key.
175 -- Calls 'error' when the element can not be found.
176 (!) :: Ord k => Map k a -> k -> a
177 m ! k    = find k m
178
179 -- | /O(n+m)/. See 'difference'.
180 (\\) :: Ord k => Map k a -> Map k b -> Map k a
181 m1 \\ m2 = difference m1 m2
182
183 {--------------------------------------------------------------------
184   Size balanced trees.
185 --------------------------------------------------------------------}
186 -- | A Map from keys @k@ to values @a@. 
187 data Map k a  = Tip 
188               | Bin {-# UNPACK #-} !Size !k a !(Map k a) !(Map k a) 
189
190 type Size     = Int
191
192 #if __GLASGOW_HASKELL__
193
194 {--------------------------------------------------------------------
195   A Data instance  
196 --------------------------------------------------------------------}
197
198 -- This instance preserves data abstraction at the cost of inefficiency.
199 -- We omit reflection services for the sake of data abstraction.
200
201 instance (Data k, Data a, Ord k) => Data (Map k a) where
202   gfoldl f z map = z fromList `f` (toList map)
203   toConstr _     = error "toConstr"
204   gunfold _ _    = error "gunfold"
205   dataTypeOf _   = mkNorepType "Data.Map.Map"
206
207 #endif
208
209 {--------------------------------------------------------------------
210   Query
211 --------------------------------------------------------------------}
212 -- | /O(1)/. Is the map empty?
213 null :: Map k a -> Bool
214 null t
215   = case t of
216       Tip             -> True
217       Bin sz k x l r  -> False
218
219 -- | /O(1)/. The number of elements in the map.
220 size :: Map k a -> Int
221 size t
222   = case t of
223       Tip             -> 0
224       Bin sz k x l r  -> sz
225
226
227 -- | /O(log n)/. Lookup the value at a key in the map.
228 lookup :: (Monad m,Ord k) => k -> Map k a -> m a
229 lookup k t = case lookup' k t of
230     Just x -> return x
231     Nothing -> fail "Data.Map.lookup: Key not found"
232 lookup' :: Ord k => k -> Map k a -> Maybe a
233 lookup' k t
234   = case t of
235       Tip -> Nothing
236       Bin sz kx x l r
237           -> case compare k kx of
238                LT -> lookup' k l
239                GT -> lookup' k r
240                EQ -> Just x       
241
242 -- | /O(log n)/. Is the key a member of the map?
243 member :: Ord k => k -> Map k a -> Bool
244 member k m
245   = case lookup k m of
246       Nothing -> False
247       Just x  -> True
248
249 -- | /O(log n)/. Find the value at a key.
250 -- Calls 'error' when the element can not be found.
251 find :: Ord k => k -> Map k a -> a
252 find k m
253   = case lookup k m of
254       Nothing -> error "Map.find: element not in the map"
255       Just x  -> x
256
257 -- | /O(log n)/. The expression @('findWithDefault' def k map)@ returns
258 -- the value at key @k@ or returns @def@ when the key is not in the map.
259 findWithDefault :: Ord k => a -> k -> Map k a -> a
260 findWithDefault def k m
261   = case lookup k m of
262       Nothing -> def
263       Just x  -> x
264
265
266
267 {--------------------------------------------------------------------
268   Construction
269 --------------------------------------------------------------------}
270 -- | /O(1)/. The empty map.
271 empty :: Map k a
272 empty 
273   = Tip
274
275 -- | /O(1)/. A map with a single element.
276 singleton :: k -> a -> Map k a
277 singleton k x  
278   = Bin 1 k x Tip Tip
279
280 {--------------------------------------------------------------------
281   Insertion
282   [insert] is the inlined version of [insertWith (\k x y -> x)]
283 --------------------------------------------------------------------}
284 -- | /O(log n)/. Insert a new key and value in the map.
285 insert :: Ord k => k -> a -> Map k a -> Map k a
286 insert kx x t
287   = case t of
288       Tip -> singleton kx x
289       Bin sz ky y l r
290           -> case compare kx ky of
291                LT -> balance ky y (insert kx x l) r
292                GT -> balance ky y l (insert kx x r)
293                EQ -> Bin sz kx x l r
294
295 -- | /O(log n)/. Insert with a combining function.
296 insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
297 insertWith f k x m          
298   = insertWithKey (\k x y -> f x y) k x m
299
300 -- | /O(log n)/. Insert with a combining function.
301 insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
302 insertWithKey f kx x t
303   = case t of
304       Tip -> singleton kx x
305       Bin sy ky y l r
306           -> case compare kx ky of
307                LT -> balance ky y (insertWithKey f kx x l) r
308                GT -> balance ky y l (insertWithKey f kx x r)
309                EQ -> Bin sy ky (f ky x y) l r
310
311 -- | /O(log n)/. The expression (@'insertLookupWithKey' f k x map@)
312 -- is a pair where the first element is equal to (@'lookup' k map@)
313 -- and the second element equal to (@'insertWithKey' f k x map@).
314 insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a,Map k a)
315 insertLookupWithKey f kx x t
316   = case t of
317       Tip -> (Nothing, singleton kx x)
318       Bin sy ky y l r
319           -> case compare kx ky of
320                LT -> let (found,l') = insertLookupWithKey f kx x l in (found,balance ky y l' r)
321                GT -> let (found,r') = insertLookupWithKey f kx x r in (found,balance ky y l r')
322                EQ -> (Just y, Bin sy ky (f ky x y) l r)
323
324 {--------------------------------------------------------------------
325   Deletion
326   [delete] is the inlined version of [deleteWith (\k x -> Nothing)]
327 --------------------------------------------------------------------}
328 -- | /O(log n)/. Delete a key and its value from the map. When the key is not
329 -- a member of the map, the original map is returned.
330 delete :: Ord k => k -> Map k a -> Map k a
331 delete k t
332   = case t of
333       Tip -> Tip
334       Bin sx kx x l r 
335           -> case compare k kx of
336                LT -> balance kx x (delete k l) r
337                GT -> balance kx x l (delete k r)
338                EQ -> glue l r
339
340 -- | /O(log n)/. Adjust a value at a specific key. When the key is not
341 -- a member of the map, the original map is returned.
342 adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a
343 adjust f k m
344   = adjustWithKey (\k x -> f x) k m
345
346 -- | /O(log n)/. Adjust a value at a specific key. When the key is not
347 -- a member of the map, the original map is returned.
348 adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
349 adjustWithKey f k m
350   = updateWithKey (\k x -> Just (f k x)) k m
351
352 -- | /O(log n)/. The expression (@'update' f k map@) updates the value @x@
353 -- at @k@ (if it is in the map). If (@f x@) is 'Nothing', the element is
354 -- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@.
355 update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a
356 update f k m
357   = updateWithKey (\k x -> f x) k m
358
359 -- | /O(log n)/. The expression (@'updateWithKey' f k map@) updates the
360 -- value @x@ at @k@ (if it is in the map). If (@f k x@) is 'Nothing',
361 -- the element is deleted. If it is (@'Just' y@), the key @k@ is bound
362 -- to the new value @y@.
363 updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
364 updateWithKey f k t
365   = case t of
366       Tip -> Tip
367       Bin sx kx x l r 
368           -> case compare k kx of
369                LT -> balance kx x (updateWithKey f k l) r
370                GT -> balance kx x l (updateWithKey f k r)
371                EQ -> case f kx x of
372                        Just x' -> Bin sx kx x' l r
373                        Nothing -> glue l r
374
375 -- | /O(log n)/. Lookup and update.
376 updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a,Map k a)
377 updateLookupWithKey f k t
378   = case t of
379       Tip -> (Nothing,Tip)
380       Bin sx kx x l r 
381           -> case compare k kx of
382                LT -> let (found,l') = updateLookupWithKey f k l in (found,balance kx x l' r)
383                GT -> let (found,r') = updateLookupWithKey f k r in (found,balance kx x l r') 
384                EQ -> case f kx x of
385                        Just x' -> (Just x',Bin sx kx x' l r)
386                        Nothing -> (Just x,glue l r)
387
388 {--------------------------------------------------------------------
389   Indexing
390 --------------------------------------------------------------------}
391 -- | /O(log n)/. Return the /index/ of a key. The index is a number from
392 -- /0/ up to, but not including, the 'size' of the map. Calls 'error' when
393 -- the key is not a 'member' of the map.
394 findIndex :: Ord k => k -> Map k a -> Int
395 findIndex k t
396   = case lookupIndex k t of
397       Nothing  -> error "Map.findIndex: element is not in the map"
398       Just idx -> idx
399
400 -- | /O(log n)/. Lookup the /index/ of a key. The index is a number from
401 -- /0/ up to, but not including, the 'size' of the map. 
402 lookupIndex :: (Monad m,Ord k) => k -> Map k a -> m Int
403 lookupIndex k t = case lookup 0 t of
404     Nothing -> fail "Data.Map.lookupIndex: Key not found."
405     Just x -> return x
406   where
407     lookup idx Tip  = Nothing
408     lookup idx (Bin _ kx x l r)
409       = case compare k kx of
410           LT -> lookup idx l
411           GT -> lookup (idx + size l + 1) r 
412           EQ -> Just (idx + size l)
413
414 -- | /O(log n)/. Retrieve an element by /index/. Calls 'error' when an
415 -- invalid index is used.
416 elemAt :: Int -> Map k a -> (k,a)
417 elemAt i Tip = error "Map.elemAt: index out of range"
418 elemAt i (Bin _ kx x l r)
419   = case compare i sizeL of
420       LT -> elemAt i l
421       GT -> elemAt (i-sizeL-1) r
422       EQ -> (kx,x)
423   where
424     sizeL = size l
425
426 -- | /O(log n)/. Update the element at /index/. Calls 'error' when an
427 -- invalid index is used.
428 updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a
429 updateAt f i Tip  = error "Map.updateAt: index out of range"
430 updateAt f i (Bin sx kx x l r)
431   = case compare i sizeL of
432       LT -> updateAt f i l
433       GT -> updateAt f (i-sizeL-1) r
434       EQ -> case f kx x of
435               Just x' -> Bin sx kx x' l r
436               Nothing -> glue l r
437   where
438     sizeL = size l
439
440 -- | /O(log n)/. Delete the element at /index/.
441 -- Defined as (@'deleteAt' i map = 'updateAt' (\k x -> 'Nothing') i map@).
442 deleteAt :: Int -> Map k a -> Map k a
443 deleteAt i map
444   = updateAt (\k x -> Nothing) i map
445
446
447 {--------------------------------------------------------------------
448   Minimal, Maximal
449 --------------------------------------------------------------------}
450 -- | /O(log n)/. The minimal key of the map.
451 findMin :: Map k a -> (k,a)
452 findMin (Bin _ kx x Tip r)  = (kx,x)
453 findMin (Bin _ kx x l r)    = findMin l
454 findMin Tip                 = error "Map.findMin: empty tree has no minimal element"
455
456 -- | /O(log n)/. The maximal key of the map.
457 findMax :: Map k a -> (k,a)
458 findMax (Bin _ kx x l Tip)  = (kx,x)
459 findMax (Bin _ kx x l r)    = findMax r
460 findMax Tip                 = error "Map.findMax: empty tree has no maximal element"
461
462 -- | /O(log n)/. Delete the minimal key.
463 deleteMin :: Map k a -> Map k a
464 deleteMin (Bin _ kx x Tip r)  = r
465 deleteMin (Bin _ kx x l r)    = balance kx x (deleteMin l) r
466 deleteMin Tip                 = Tip
467
468 -- | /O(log n)/. Delete the maximal key.
469 deleteMax :: Map k a -> Map k a
470 deleteMax (Bin _ kx x l Tip)  = l
471 deleteMax (Bin _ kx x l r)    = balance kx x l (deleteMax r)
472 deleteMax Tip                 = Tip
473
474 -- | /O(log n)/. Update the value at the minimal key.
475 updateMin :: (a -> Maybe a) -> Map k a -> Map k a
476 updateMin f m
477   = updateMinWithKey (\k x -> f x) m
478
479 -- | /O(log n)/. Update the value at the maximal key.
480 updateMax :: (a -> Maybe a) -> Map k a -> Map k a
481 updateMax f m
482   = updateMaxWithKey (\k x -> f x) m
483
484
485 -- | /O(log n)/. Update the value at the minimal key.
486 updateMinWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a
487 updateMinWithKey f t
488   = case t of
489       Bin sx kx x Tip r  -> case f kx x of
490                               Nothing -> r
491                               Just x' -> Bin sx kx x' Tip r
492       Bin sx kx x l r    -> balance kx x (updateMinWithKey f l) r
493       Tip                -> Tip
494
495 -- | /O(log n)/. Update the value at the maximal key.
496 updateMaxWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a
497 updateMaxWithKey f t
498   = case t of
499       Bin sx kx x l Tip  -> case f kx x of
500                               Nothing -> l
501                               Just x' -> Bin sx kx x' l Tip
502       Bin sx kx x l r    -> balance kx x l (updateMaxWithKey f r)
503       Tip                -> Tip
504
505
506 {--------------------------------------------------------------------
507   Union. 
508 --------------------------------------------------------------------}
509 -- | The union of a list of maps:
510 --   (@'unions' == 'Prelude.foldl' 'union' 'empty'@).
511 unions :: Ord k => [Map k a] -> Map k a
512 unions ts
513   = foldlStrict union empty ts
514
515 -- | The union of a list of maps, with a combining operation:
516 --   (@'unionsWith' f == 'Prelude.foldl' ('unionWith' f) 'empty'@).
517 unionsWith :: Ord k => (a->a->a) -> [Map k a] -> Map k a
518 unionsWith f ts
519   = foldlStrict (unionWith f) empty ts
520
521 -- | /O(n+m)/.
522 -- The expression (@'union' t1 t2@) takes the left-biased union of @t1@ and @t2@. 
523 -- It prefers @t1@ when duplicate keys are encountered,
524 -- i.e. (@'union' == 'unionWith' 'const'@).
525 -- The implementation uses the efficient /hedge-union/ algorithm.
526 -- Hedge-union is more efficient on (bigset `union` smallset)?
527 union :: Ord k => Map k a -> Map k a -> Map k a
528 union Tip t2  = t2
529 union t1 Tip  = t1
530 union t1 t2
531    | size t1 >= size t2  = hedgeUnionL (const LT) (const GT) t1 t2
532    | otherwise           = hedgeUnionR (const LT) (const GT) t2 t1
533
534 -- left-biased hedge union
535 hedgeUnionL cmplo cmphi t1 Tip 
536   = t1
537 hedgeUnionL cmplo cmphi Tip (Bin _ kx x l r)
538   = join kx x (filterGt cmplo l) (filterLt cmphi r)
539 hedgeUnionL cmplo cmphi (Bin _ kx x l r) t2
540   = join kx x (hedgeUnionL cmplo cmpkx l (trim cmplo cmpkx t2)) 
541               (hedgeUnionL cmpkx cmphi r (trim cmpkx cmphi t2))
542   where
543     cmpkx k  = compare kx k
544
545 -- right-biased hedge union
546 hedgeUnionR cmplo cmphi t1 Tip 
547   = t1
548 hedgeUnionR cmplo cmphi Tip (Bin _ kx x l r)
549   = join kx x (filterGt cmplo l) (filterLt cmphi r)
550 hedgeUnionR cmplo cmphi (Bin _ kx x l r) t2
551   = join kx newx (hedgeUnionR cmplo cmpkx l lt) 
552                  (hedgeUnionR cmpkx cmphi r gt)
553   where
554     cmpkx k     = compare kx k
555     lt          = trim cmplo cmpkx t2
556     (found,gt)  = trimLookupLo kx cmphi t2
557     newx        = case found of
558                     Nothing -> x
559                     Just y  -> y
560
561 {--------------------------------------------------------------------
562   Union with a combining function
563 --------------------------------------------------------------------}
564 -- | /O(n+m)/. Union with a combining function. The implementation uses the efficient /hedge-union/ algorithm.
565 unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
566 unionWith f m1 m2
567   = unionWithKey (\k x y -> f x y) m1 m2
568
569 -- | /O(n+m)/.
570 -- Union with a combining function. The implementation uses the efficient /hedge-union/ algorithm.
571 -- Hedge-union is more efficient on (bigset `union` smallset).
572 unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
573 unionWithKey f Tip t2  = t2
574 unionWithKey f t1 Tip  = t1
575 unionWithKey f t1 t2
576   | size t1 >= size t2  = hedgeUnionWithKey f (const LT) (const GT) t1 t2
577   | otherwise           = hedgeUnionWithKey flipf (const LT) (const GT) t2 t1
578   where
579     flipf k x y   = f k y x
580
581 hedgeUnionWithKey f cmplo cmphi t1 Tip 
582   = t1
583 hedgeUnionWithKey f cmplo cmphi Tip (Bin _ kx x l r)
584   = join kx x (filterGt cmplo l) (filterLt cmphi r)
585 hedgeUnionWithKey f cmplo cmphi (Bin _ kx x l r) t2
586   = join kx newx (hedgeUnionWithKey f cmplo cmpkx l lt) 
587                  (hedgeUnionWithKey f cmpkx cmphi r gt)
588   where
589     cmpkx k     = compare kx k
590     lt          = trim cmplo cmpkx t2
591     (found,gt)  = trimLookupLo kx cmphi t2
592     newx        = case found of
593                     Nothing -> x
594                     Just y  -> f kx x y
595
596 {--------------------------------------------------------------------
597   Difference
598 --------------------------------------------------------------------}
599 -- | /O(n+m)/. Difference of two maps. 
600 -- The implementation uses an efficient /hedge/ algorithm comparable with /hedge-union/.
601 difference :: Ord k => Map k a -> Map k b -> Map k a
602 difference Tip t2  = Tip
603 difference t1 Tip  = t1
604 difference t1 t2   = hedgeDiff (const LT) (const GT) t1 t2
605
606 hedgeDiff cmplo cmphi Tip t     
607   = Tip
608 hedgeDiff cmplo cmphi (Bin _ kx x l r) Tip 
609   = join kx x (filterGt cmplo l) (filterLt cmphi r)
610 hedgeDiff cmplo cmphi t (Bin _ kx x l r) 
611   = merge (hedgeDiff cmplo cmpkx (trim cmplo cmpkx t) l) 
612           (hedgeDiff cmpkx cmphi (trim cmpkx cmphi t) r)
613   where
614     cmpkx k = compare kx k   
615
616 -- | /O(n+m)/. Difference with a combining function. 
617 -- The implementation uses an efficient /hedge/ algorithm comparable with /hedge-union/.
618 differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
619 differenceWith f m1 m2
620   = differenceWithKey (\k x y -> f x y) m1 m2
621
622 -- | /O(n+m)/. Difference with a combining function. When two equal keys are
623 -- encountered, the combining function is applied to the key and both values.
624 -- If it returns 'Nothing', the element is discarded (proper set difference). If
625 -- it returns (@'Just' y@), the element is updated with a new value @y@. 
626 -- The implementation uses an efficient /hedge/ algorithm comparable with /hedge-union/.
627 differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
628 differenceWithKey f Tip t2  = Tip
629 differenceWithKey f t1 Tip  = t1
630 differenceWithKey f t1 t2   = hedgeDiffWithKey f (const LT) (const GT) t1 t2
631
632 hedgeDiffWithKey f cmplo cmphi Tip t     
633   = Tip
634 hedgeDiffWithKey f cmplo cmphi (Bin _ kx x l r) Tip 
635   = join kx x (filterGt cmplo l) (filterLt cmphi r)
636 hedgeDiffWithKey f cmplo cmphi t (Bin _ kx x l r) 
637   = case found of
638       Nothing -> merge tl tr
639       Just y  -> case f kx y x of
640                    Nothing -> merge tl tr
641                    Just z  -> join kx z tl tr
642   where
643     cmpkx k     = compare kx k   
644     lt          = trim cmplo cmpkx t
645     (found,gt)  = trimLookupLo kx cmphi t
646     tl          = hedgeDiffWithKey f cmplo cmpkx lt l
647     tr          = hedgeDiffWithKey f cmpkx cmphi gt r
648
649
650
651 {--------------------------------------------------------------------
652   Intersection
653 --------------------------------------------------------------------}
654 -- | /O(n+m)/. Intersection of two maps. The values in the first
655 -- map are returned, i.e. (@'intersection' m1 m2 == 'intersectionWith' 'const' m1 m2@).
656 intersection :: Ord k => Map k a -> Map k b -> Map k a
657 intersection m1 m2
658   = intersectionWithKey (\k x y -> x) m1 m2
659
660 -- | /O(n+m)/. Intersection with a combining function.
661 intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c
662 intersectionWith f m1 m2
663   = intersectionWithKey (\k x y -> f x y) m1 m2
664
665 -- | /O(n+m)/. Intersection with a combining function.
666 -- Intersection is more efficient on (bigset `intersection` smallset)
667 intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c
668 intersectionWithKey f Tip t = Tip
669 intersectionWithKey f t Tip = Tip
670 intersectionWithKey f t1 t2
671   | size t1 >= size t2  = intersectWithKey f t1 t2
672   | otherwise           = intersectWithKey flipf t2 t1
673   where
674     flipf k x y   = f k y x
675
676 intersectWithKey f Tip t = Tip
677 intersectWithKey f t Tip = Tip
678 intersectWithKey f t (Bin _ kx x l r)
679   = case found of
680       Nothing -> merge tl tr
681       Just y  -> join kx (f kx y x) tl tr
682   where
683     (lt,found,gt) = splitLookup kx t
684     tl            = intersectWithKey f lt l
685     tr            = intersectWithKey f gt r
686
687
688
689 {--------------------------------------------------------------------
690   Submap
691 --------------------------------------------------------------------}
692 -- | /O(n+m)/. 
693 -- This function is defined as (@'isSubmapOf' = 'isSubmapOfBy' (==)@).
694 isSubmapOf :: (Ord k,Eq a) => Map k a -> Map k a -> Bool
695 isSubmapOf m1 m2
696   = isSubmapOfBy (==) m1 m2
697
698 {- | /O(n+m)/. 
699  The expression (@'isSubmapOfBy' f t1 t2@) returns 'True' if
700  all keys in @t1@ are in tree @t2@, and when @f@ returns 'True' when
701  applied to their respective values. For example, the following 
702  expressions are all 'True':
703  
704  > isSubmapOfBy (==) (fromList [('a',1)]) (fromList [('a',1),('b',2)])
705  > isSubmapOfBy (<=) (fromList [('a',1)]) (fromList [('a',1),('b',2)])
706  > isSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1),('b',2)])
707
708  But the following are all 'False':
709  
710  > isSubmapOfBy (==) (fromList [('a',2)]) (fromList [('a',1),('b',2)])
711  > isSubmapOfBy (<)  (fromList [('a',1)]) (fromList [('a',1),('b',2)])
712  > isSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1)])
713 -}
714 isSubmapOfBy :: Ord k => (a->b->Bool) -> Map k a -> Map k b -> Bool
715 isSubmapOfBy f t1 t2
716   = (size t1 <= size t2) && (submap' f t1 t2)
717
718 submap' f Tip t = True
719 submap' f t Tip = False
720 submap' f (Bin _ kx x l r) t
721   = case found of
722       Nothing -> False
723       Just y  -> f x y && submap' f l lt && submap' f r gt
724   where
725     (lt,found,gt) = splitLookup kx t
726
727 -- | /O(n+m)/. Is this a proper submap? (ie. a submap but not equal). 
728 -- Defined as (@'isProperSubmapOf' = 'isProperSubmapOfBy' (==)@).
729 isProperSubmapOf :: (Ord k,Eq a) => Map k a -> Map k a -> Bool
730 isProperSubmapOf m1 m2
731   = isProperSubmapOfBy (==) m1 m2
732
733 {- | /O(n+m)/. Is this a proper submap? (ie. a submap but not equal).
734  The expression (@'isProperSubmapOfBy' f m1 m2@) returns 'True' when
735  @m1@ and @m2@ are not equal,
736  all keys in @m1@ are in @m2@, and when @f@ returns 'True' when
737  applied to their respective values. For example, the following 
738  expressions are all 'True':
739  
740   > isProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
741   > isProperSubmapOfBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
742
743  But the following are all 'False':
744  
745   > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
746   > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
747   > isProperSubmapOfBy (<)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])
748 -}
749 isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool
750 isProperSubmapOfBy f t1 t2
751   = (size t1 < size t2) && (submap' f t1 t2)
752
753 {--------------------------------------------------------------------
754   Filter and partition
755 --------------------------------------------------------------------}
756 -- | /O(n)/. Filter all values that satisfy the predicate.
757 filter :: Ord k => (a -> Bool) -> Map k a -> Map k a
758 filter p m
759   = filterWithKey (\k x -> p x) m
760
761 -- | /O(n)/. Filter all keys\/values that satisfy the predicate.
762 filterWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> Map k a
763 filterWithKey p Tip = Tip
764 filterWithKey p (Bin _ kx x l r)
765   | p kx x    = join kx x (filterWithKey p l) (filterWithKey p r)
766   | otherwise = merge (filterWithKey p l) (filterWithKey p r)
767
768
769 -- | /O(n)/. partition the map according to a predicate. The first
770 -- map contains all elements that satisfy the predicate, the second all
771 -- elements that fail the predicate. See also 'split'.
772 partition :: Ord k => (a -> Bool) -> Map k a -> (Map k a,Map k a)
773 partition p m
774   = partitionWithKey (\k x -> p x) m
775
776 -- | /O(n)/. partition the map according to a predicate. The first
777 -- map contains all elements that satisfy the predicate, the second all
778 -- elements that fail the predicate. See also 'split'.
779 partitionWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> (Map k a,Map k a)
780 partitionWithKey p Tip = (Tip,Tip)
781 partitionWithKey p (Bin _ kx x l r)
782   | p kx x    = (join kx x l1 r1,merge l2 r2)
783   | otherwise = (merge l1 r1,join kx x l2 r2)
784   where
785     (l1,l2) = partitionWithKey p l
786     (r1,r2) = partitionWithKey p r
787
788
789 {--------------------------------------------------------------------
790   Mapping
791 --------------------------------------------------------------------}
792 -- | /O(n)/. Map a function over all values in the map.
793 map :: (a -> b) -> Map k a -> Map k b
794 map f m
795   = mapWithKey (\k x -> f x) m
796
797 -- | /O(n)/. Map a function over all values in the map.
798 mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
799 mapWithKey f Tip = Tip
800 mapWithKey f (Bin sx kx x l r) 
801   = Bin sx kx (f kx x) (mapWithKey f l) (mapWithKey f r)
802
803 -- | /O(n)/. The function 'mapAccum' threads an accumulating
804 -- argument through the map in ascending order of keys.
805 mapAccum :: (a -> b -> (a,c)) -> a -> Map k b -> (a,Map k c)
806 mapAccum f a m
807   = mapAccumWithKey (\a k x -> f a x) a m
808
809 -- | /O(n)/. The function 'mapAccumWithKey' threads an accumulating
810 -- argument through the map in ascending order of keys.
811 mapAccumWithKey :: (a -> k -> b -> (a,c)) -> a -> Map k b -> (a,Map k c)
812 mapAccumWithKey f a t
813   = mapAccumL f a t
814
815 -- | /O(n)/. The function 'mapAccumL' threads an accumulating
816 -- argument throught the map in ascending order of keys.
817 mapAccumL :: (a -> k -> b -> (a,c)) -> a -> Map k b -> (a,Map k c)
818 mapAccumL f a t
819   = case t of
820       Tip -> (a,Tip)
821       Bin sx kx x l r
822           -> let (a1,l') = mapAccumL f a l
823                  (a2,x') = f a1 kx x
824                  (a3,r') = mapAccumL f a2 r
825              in (a3,Bin sx kx x' l' r')
826
827 -- | /O(n)/. The function 'mapAccumR' threads an accumulating
828 -- argument throught the map in descending order of keys.
829 mapAccumR :: (a -> k -> b -> (a,c)) -> a -> Map k b -> (a,Map k c)
830 mapAccumR f a t
831   = case t of
832       Tip -> (a,Tip)
833       Bin sx kx x l r 
834           -> let (a1,r') = mapAccumR f a r
835                  (a2,x') = f a1 kx x
836                  (a3,l') = mapAccumR f a2 l
837              in (a3,Bin sx kx x' l' r')
838
839 -- | /O(n*log n)/. 
840 -- @'mapKeys' f s@ is the map obtained by applying @f@ to each key of @s@.
841 -- 
842 -- The size of the result may be smaller if @f@ maps two or more distinct
843 -- keys to the same new key.  In this case the value at the smallest of
844 -- these keys is retained.
845
846 mapKeys :: Ord k2 => (k1->k2) -> Map k1 a -> Map k2 a
847 mapKeys = mapKeysWith (\x y->x)
848
849 -- | /O(n*log n)/. 
850 -- @'mapKeysWith' c f s@ is the map obtained by applying @f@ to each key of @s@.
851 -- 
852 -- The size of the result may be smaller if @f@ maps two or more distinct
853 -- keys to the same new key.  In this case the associated values will be
854 -- combined using @c@.
855
856 mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1->k2) -> Map k1 a -> Map k2 a
857 mapKeysWith c f = fromListWith c . List.map fFirst . toList
858     where fFirst (x,y) = (f x, y)
859
860
861 -- | /O(n)/.
862 -- @'mapKeysMonotonic' f s == 'mapKeys' f s@, but works only when @f@
863 -- is strictly monotonic.
864 -- /The precondition is not checked./
865 -- Semi-formally, we have:
866 -- 
867 -- > and [x < y ==> f x < f y | x <- ls, y <- ls] 
868 -- >                     ==> mapKeysMonotonic f s == mapKeys f s
869 -- >     where ls = keys s
870
871 mapKeysMonotonic :: (k1->k2) -> Map k1 a -> Map k2 a
872 mapKeysMonotonic f Tip = Tip
873 mapKeysMonotonic f (Bin sz k x l r) =
874     Bin sz (f k) x (mapKeysMonotonic f l) (mapKeysMonotonic f r)
875
876 {--------------------------------------------------------------------
877   Folds  
878 --------------------------------------------------------------------}
879
880 -- | /O(n)/. Fold the values in the map, such that
881 -- @'fold' f z == 'Prelude.foldr' f z . 'elems'@.
882 -- For example,
883 --
884 -- > elems map = fold (:) [] map
885 --
886 fold :: (a -> b -> b) -> b -> Map k a -> b
887 fold f z m
888   = foldWithKey (\k x z -> f x z) z m
889
890 -- | /O(n)/. Fold the keys and values in the map, such that
891 -- @'foldWithKey' f z == 'Prelude.foldr' ('uncurry' f) z . 'toAscList'@.
892 -- For example,
893 --
894 -- > keys map = foldWithKey (\k x ks -> k:ks) [] map
895 --
896 foldWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b
897 foldWithKey f z t
898   = foldr f z t
899
900 -- | /O(n)/. In-order fold.
901 foldi :: (k -> a -> b -> b -> b) -> b -> Map k a -> b 
902 foldi f z Tip               = z
903 foldi f z (Bin _ kx x l r)  = f kx x (foldi f z l) (foldi f z r)
904
905 -- | /O(n)/. Post-order fold.
906 foldr :: (k -> a -> b -> b) -> b -> Map k a -> b
907 foldr f z Tip              = z
908 foldr f z (Bin _ kx x l r) = foldr f (f kx x (foldr f z r)) l
909
910 -- | /O(n)/. Pre-order fold.
911 foldl :: (b -> k -> a -> b) -> b -> Map k a -> b
912 foldl f z Tip              = z
913 foldl f z (Bin _ kx x l r) = foldl f (f (foldl f z l) kx x) r
914
915 {--------------------------------------------------------------------
916   List variations 
917 --------------------------------------------------------------------}
918 -- | /O(n)/.
919 -- Return all elements of the map in the ascending order of their keys.
920 elems :: Map k a -> [a]
921 elems m
922   = [x | (k,x) <- assocs m]
923
924 -- | /O(n)/. Return all keys of the map in ascending order.
925 keys  :: Map k a -> [k]
926 keys m
927   = [k | (k,x) <- assocs m]
928
929 -- | /O(n)/. The set of all keys of the map.
930 keysSet :: Map k a -> Set.Set k
931 keysSet m = Set.fromDistinctAscList (keys m)
932
933 -- | /O(n)/. Return all key\/value pairs in the map in ascending key order.
934 assocs :: Map k a -> [(k,a)]
935 assocs m
936   = toList m
937
938 {--------------------------------------------------------------------
939   Lists 
940   use [foldlStrict] to reduce demand on the control-stack
941 --------------------------------------------------------------------}
942 -- | /O(n*log n)/. Build a map from a list of key\/value pairs. See also 'fromAscList'.
943 fromList :: Ord k => [(k,a)] -> Map k a 
944 fromList xs       
945   = foldlStrict ins empty xs
946   where
947     ins t (k,x) = insert k x t
948
949 -- | /O(n*log n)/. Build a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'.
950 fromListWith :: Ord k => (a -> a -> a) -> [(k,a)] -> Map k a 
951 fromListWith f xs
952   = fromListWithKey (\k x y -> f x y) xs
953
954 -- | /O(n*log n)/. Build a map from a list of key\/value pairs with a combining function. See also 'fromAscListWithKey'.
955 fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k,a)] -> Map k a 
956 fromListWithKey f xs 
957   = foldlStrict ins empty xs
958   where
959     ins t (k,x) = insertWithKey f k x t
960
961 -- | /O(n)/. Convert to a list of key\/value pairs.
962 toList :: Map k a -> [(k,a)]
963 toList t      = toAscList t
964
965 -- | /O(n)/. Convert to an ascending list.
966 toAscList :: Map k a -> [(k,a)]
967 toAscList t   = foldr (\k x xs -> (k,x):xs) [] t
968
969 -- | /O(n)/. 
970 toDescList :: Map k a -> [(k,a)]
971 toDescList t  = foldl (\xs k x -> (k,x):xs) [] t
972
973
974 {--------------------------------------------------------------------
975   Building trees from ascending/descending lists can be done in linear time.
976   
977   Note that if [xs] is ascending that: 
978     fromAscList xs       == fromList xs
979     fromAscListWith f xs == fromListWith f xs
980 --------------------------------------------------------------------}
981 -- | /O(n)/. Build a map from an ascending list in linear time.
982 -- /The precondition (input list is ascending) is not checked./
983 fromAscList :: Eq k => [(k,a)] -> Map k a 
984 fromAscList xs
985   = fromAscListWithKey (\k x y -> x) xs
986
987 -- | /O(n)/. Build a map from an ascending list in linear time with a combining function for equal keys.
988 -- /The precondition (input list is ascending) is not checked./
989 fromAscListWith :: Eq k => (a -> a -> a) -> [(k,a)] -> Map k a 
990 fromAscListWith f xs
991   = fromAscListWithKey (\k x y -> f x y) xs
992
993 -- | /O(n)/. Build a map from an ascending list in linear time with a
994 -- combining function for equal keys.
995 -- /The precondition (input list is ascending) is not checked./
996 fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k,a)] -> Map k a 
997 fromAscListWithKey f xs
998   = fromDistinctAscList (combineEq f xs)
999   where
1000   -- [combineEq f xs] combines equal elements with function [f] in an ordered list [xs]
1001   combineEq f xs
1002     = case xs of
1003         []     -> []
1004         [x]    -> [x]
1005         (x:xx) -> combineEq' x xx
1006
1007   combineEq' z [] = [z]
1008   combineEq' z@(kz,zz) (x@(kx,xx):xs)
1009     | kx==kz    = let yy = f kx xx zz in combineEq' (kx,yy) xs
1010     | otherwise = z:combineEq' x xs
1011
1012
1013 -- | /O(n)/. Build a map from an ascending list of distinct elements in linear time.
1014 -- /The precondition is not checked./
1015 fromDistinctAscList :: [(k,a)] -> Map k a 
1016 fromDistinctAscList xs
1017   = build const (length xs) xs
1018   where
1019     -- 1) use continutations so that we use heap space instead of stack space.
1020     -- 2) special case for n==5 to build bushier trees. 
1021     build c 0 xs   = c Tip xs 
1022     build c 5 xs   = case xs of
1023                        ((k1,x1):(k2,x2):(k3,x3):(k4,x4):(k5,x5):xx) 
1024                             -> c (bin k4 x4 (bin k2 x2 (singleton k1 x1) (singleton k3 x3)) (singleton k5 x5)) xx
1025     build c n xs   = seq nr $ build (buildR nr c) nl xs
1026                    where
1027                      nl = n `div` 2
1028                      nr = n - nl - 1
1029
1030     buildR n c l ((k,x):ys) = build (buildB l k x c) n ys
1031     buildB l k x c r zs     = c (bin k x l r) zs
1032                       
1033
1034
1035 {--------------------------------------------------------------------
1036   Utility functions that return sub-ranges of the original
1037   tree. Some functions take a comparison function as argument to
1038   allow comparisons against infinite values. A function [cmplo k]
1039   should be read as [compare lo k].
1040
1041   [trim cmplo cmphi t]  A tree that is either empty or where [cmplo k == LT]
1042                         and [cmphi k == GT] for the key [k] of the root.
1043   [filterGt cmp t]      A tree where for all keys [k]. [cmp k == LT]
1044   [filterLt cmp t]      A tree where for all keys [k]. [cmp k == GT]
1045
1046   [split k t]           Returns two trees [l] and [r] where all keys
1047                         in [l] are <[k] and all keys in [r] are >[k].
1048   [splitLookup k t]     Just like [split] but also returns whether [k]
1049                         was found in the tree.
1050 --------------------------------------------------------------------}
1051
1052 {--------------------------------------------------------------------
1053   [trim lo hi t] trims away all subtrees that surely contain no
1054   values between the range [lo] to [hi]. The returned tree is either
1055   empty or the key of the root is between @lo@ and @hi@.
1056 --------------------------------------------------------------------}
1057 trim :: (k -> Ordering) -> (k -> Ordering) -> Map k a -> Map k a
1058 trim cmplo cmphi Tip = Tip
1059 trim cmplo cmphi t@(Bin sx kx x l r)
1060   = case cmplo kx of
1061       LT -> case cmphi kx of
1062               GT -> t
1063               le -> trim cmplo cmphi l
1064       ge -> trim cmplo cmphi r
1065               
1066 trimLookupLo :: Ord k => k -> (k -> Ordering) -> Map k a -> (Maybe a, Map k a)
1067 trimLookupLo lo cmphi Tip = (Nothing,Tip)
1068 trimLookupLo lo cmphi t@(Bin sx kx x l r)
1069   = case compare lo kx of
1070       LT -> case cmphi kx of
1071               GT -> (lookup lo t, t)
1072               le -> trimLookupLo lo cmphi l
1073       GT -> trimLookupLo lo cmphi r
1074       EQ -> (Just x,trim (compare lo) cmphi r)
1075
1076
1077 {--------------------------------------------------------------------
1078   [filterGt k t] filter all keys >[k] from tree [t]
1079   [filterLt k t] filter all keys <[k] from tree [t]
1080 --------------------------------------------------------------------}
1081 filterGt :: Ord k => (k -> Ordering) -> Map k a -> Map k a
1082 filterGt cmp Tip = Tip
1083 filterGt cmp (Bin sx kx x l r)
1084   = case cmp kx of
1085       LT -> join kx x (filterGt cmp l) r
1086       GT -> filterGt cmp r
1087       EQ -> r
1088       
1089 filterLt :: Ord k => (k -> Ordering) -> Map k a -> Map k a
1090 filterLt cmp Tip = Tip
1091 filterLt cmp (Bin sx kx x l r)
1092   = case cmp kx of
1093       LT -> filterLt cmp l
1094       GT -> join kx x l (filterLt cmp r)
1095       EQ -> l
1096
1097 {--------------------------------------------------------------------
1098   Split
1099 --------------------------------------------------------------------}
1100 -- | /O(log n)/. The expression (@'split' k map@) is a pair @(map1,map2)@ where
1101 -- the keys in @map1@ are smaller than @k@ and the keys in @map2@ larger than @k@. Any key equal to @k@ is found in neither @map1@ nor @map2@.
1102 split :: Ord k => k -> Map k a -> (Map k a,Map k a)
1103 split k Tip = (Tip,Tip)
1104 split k (Bin sx kx x l r)
1105   = case compare k kx of
1106       LT -> let (lt,gt) = split k l in (lt,join kx x gt r)
1107       GT -> let (lt,gt) = split k r in (join kx x l lt,gt)
1108       EQ -> (l,r)
1109
1110 -- | /O(log n)/. The expression (@'splitLookup' k map@) splits a map just
1111 -- like 'split' but also returns @'lookup' k map@.
1112 splitLookup :: Ord k => k -> Map k a -> (Map k a,Maybe a,Map k a)
1113 splitLookup k Tip = (Tip,Nothing,Tip)
1114 splitLookup k (Bin sx kx x l r)
1115   = case compare k kx of
1116       LT -> let (lt,z,gt) = splitLookup k l in (lt,z,join kx x gt r)
1117       GT -> let (lt,z,gt) = splitLookup k r in (join kx x l lt,z,gt)
1118       EQ -> (l,Just x,r)
1119
1120 {--------------------------------------------------------------------
1121   Utility functions that maintain the balance properties of the tree.
1122   All constructors assume that all values in [l] < [k] and all values
1123   in [r] > [k], and that [l] and [r] are valid trees.
1124   
1125   In order of sophistication:
1126     [Bin sz k x l r]  The type constructor.
1127     [bin k x l r]     Maintains the correct size, assumes that both [l]
1128                       and [r] are balanced with respect to each other.
1129     [balance k x l r] Restores the balance and size.
1130                       Assumes that the original tree was balanced and
1131                       that [l] or [r] has changed by at most one element.
1132     [join k x l r]    Restores balance and size. 
1133
1134   Furthermore, we can construct a new tree from two trees. Both operations
1135   assume that all values in [l] < all values in [r] and that [l] and [r]
1136   are valid:
1137     [glue l r]        Glues [l] and [r] together. Assumes that [l] and
1138                       [r] are already balanced with respect to each other.
1139     [merge l r]       Merges two trees and restores balance.
1140
1141   Note: in contrast to Adam's paper, we use (<=) comparisons instead
1142   of (<) comparisons in [join], [merge] and [balance]. 
1143   Quickcheck (on [difference]) showed that this was necessary in order 
1144   to maintain the invariants. It is quite unsatisfactory that I haven't 
1145   been able to find out why this is actually the case! Fortunately, it 
1146   doesn't hurt to be a bit more conservative.
1147 --------------------------------------------------------------------}
1148
1149 {--------------------------------------------------------------------
1150   Join 
1151 --------------------------------------------------------------------}
1152 join :: Ord k => k -> a -> Map k a -> Map k a -> Map k a
1153 join kx x Tip r  = insertMin kx x r
1154 join kx x l Tip  = insertMax kx x l
1155 join kx x l@(Bin sizeL ky y ly ry) r@(Bin sizeR kz z lz rz)
1156   | delta*sizeL <= sizeR  = balance kz z (join kx x l lz) rz
1157   | delta*sizeR <= sizeL  = balance ky y ly (join kx x ry r)
1158   | otherwise             = bin kx x l r
1159
1160
1161 -- insertMin and insertMax don't perform potentially expensive comparisons.
1162 insertMax,insertMin :: k -> a -> Map k a -> Map k a 
1163 insertMax kx x t
1164   = case t of
1165       Tip -> singleton kx x
1166       Bin sz ky y l r
1167           -> balance ky y l (insertMax kx x r)
1168              
1169 insertMin kx x t
1170   = case t of
1171       Tip -> singleton kx x
1172       Bin sz ky y l r
1173           -> balance ky y (insertMin kx x l) r
1174              
1175 {--------------------------------------------------------------------
1176   [merge l r]: merges two trees.
1177 --------------------------------------------------------------------}
1178 merge :: Map k a -> Map k a -> Map k a
1179 merge Tip r   = r
1180 merge l Tip   = l
1181 merge l@(Bin sizeL kx x lx rx) r@(Bin sizeR ky y ly ry)
1182   | delta*sizeL <= sizeR = balance ky y (merge l ly) ry
1183   | delta*sizeR <= sizeL = balance kx x lx (merge rx r)
1184   | otherwise            = glue l r
1185
1186 {--------------------------------------------------------------------
1187   [glue l r]: glues two trees together.
1188   Assumes that [l] and [r] are already balanced with respect to each other.
1189 --------------------------------------------------------------------}
1190 glue :: Map k a -> Map k a -> Map k a
1191 glue Tip r = r
1192 glue l Tip = l
1193 glue l r   
1194   | size l > size r = let ((km,m),l') = deleteFindMax l in balance km m l' r
1195   | otherwise       = let ((km,m),r') = deleteFindMin r in balance km m l r'
1196
1197
1198 -- | /O(log n)/. Delete and find the minimal element.
1199 deleteFindMin :: Map k a -> ((k,a),Map k a)
1200 deleteFindMin t 
1201   = case t of
1202       Bin _ k x Tip r -> ((k,x),r)
1203       Bin _ k x l r   -> let (km,l') = deleteFindMin l in (km,balance k x l' r)
1204       Tip             -> (error "Map.deleteFindMin: can not return the minimal element of an empty map", Tip)
1205
1206 -- | /O(log n)/. Delete and find the maximal element.
1207 deleteFindMax :: Map k a -> ((k,a),Map k a)
1208 deleteFindMax t
1209   = case t of
1210       Bin _ k x l Tip -> ((k,x),l)
1211       Bin _ k x l r   -> let (km,r') = deleteFindMax r in (km,balance k x l r')
1212       Tip             -> (error "Map.deleteFindMax: can not return the maximal element of an empty map", Tip)
1213
1214
1215 {--------------------------------------------------------------------
1216   [balance l x r] balances two trees with value x.
1217   The sizes of the trees should balance after decreasing the
1218   size of one of them. (a rotation).
1219
1220   [delta] is the maximal relative difference between the sizes of
1221           two trees, it corresponds with the [w] in Adams' paper.
1222   [ratio] is the ratio between an outer and inner sibling of the
1223           heavier subtree in an unbalanced setting. It determines
1224           whether a double or single rotation should be performed
1225           to restore balance. It is correspondes with the inverse
1226           of $\alpha$ in Adam's article.
1227
1228   Note that:
1229   - [delta] should be larger than 4.646 with a [ratio] of 2.
1230   - [delta] should be larger than 3.745 with a [ratio] of 1.534.
1231   
1232   - A lower [delta] leads to a more 'perfectly' balanced tree.
1233   - A higher [delta] performs less rebalancing.
1234
1235   - Balancing is automaic for random data and a balancing
1236     scheme is only necessary to avoid pathological worst cases.
1237     Almost any choice will do, and in practice, a rather large
1238     [delta] may perform better than smaller one.
1239
1240   Note: in contrast to Adam's paper, we use a ratio of (at least) [2]
1241   to decide whether a single or double rotation is needed. Allthough
1242   he actually proves that this ratio is needed to maintain the
1243   invariants, his implementation uses an invalid ratio of [1].
1244 --------------------------------------------------------------------}
1245 delta,ratio :: Int
1246 delta = 5
1247 ratio = 2
1248
1249 balance :: k -> a -> Map k a -> Map k a -> Map k a
1250 balance k x l r
1251   | sizeL + sizeR <= 1    = Bin sizeX k x l r
1252   | sizeR >= delta*sizeL  = rotateL k x l r
1253   | sizeL >= delta*sizeR  = rotateR k x l r
1254   | otherwise             = Bin sizeX k x l r
1255   where
1256     sizeL = size l
1257     sizeR = size r
1258     sizeX = sizeL + sizeR + 1
1259
1260 -- rotate
1261 rotateL k x l r@(Bin _ _ _ ly ry)
1262   | size ly < ratio*size ry = singleL k x l r
1263   | otherwise               = doubleL k x l r
1264
1265 rotateR k x l@(Bin _ _ _ ly ry) r
1266   | size ry < ratio*size ly = singleR k x l r
1267   | otherwise               = doubleR k x l r
1268
1269 -- basic rotations
1270 singleL k1 x1 t1 (Bin _ k2 x2 t2 t3)  = bin k2 x2 (bin k1 x1 t1 t2) t3
1271 singleR k1 x1 (Bin _ k2 x2 t1 t2) t3  = bin k2 x2 t1 (bin k1 x1 t2 t3)
1272
1273 doubleL k1 x1 t1 (Bin _ k2 x2 (Bin _ k3 x3 t2 t3) t4) = bin k3 x3 (bin k1 x1 t1 t2) (bin k2 x2 t3 t4)
1274 doubleR k1 x1 (Bin _ k2 x2 t1 (Bin _ k3 x3 t2 t3)) t4 = bin k3 x3 (bin k2 x2 t1 t2) (bin k1 x1 t3 t4)
1275
1276
1277 {--------------------------------------------------------------------
1278   The bin constructor maintains the size of the tree
1279 --------------------------------------------------------------------}
1280 bin :: k -> a -> Map k a -> Map k a -> Map k a
1281 bin k x l r
1282   = Bin (size l + size r + 1) k x l r
1283
1284
1285 {--------------------------------------------------------------------
1286   Eq converts the tree to a list. In a lazy setting, this 
1287   actually seems one of the faster methods to compare two trees 
1288   and it is certainly the simplest :-)
1289 --------------------------------------------------------------------}
1290 instance (Eq k,Eq a) => Eq (Map k a) where
1291   t1 == t2  = (size t1 == size t2) && (toAscList t1 == toAscList t2)
1292
1293 {--------------------------------------------------------------------
1294   Ord 
1295 --------------------------------------------------------------------}
1296
1297 instance (Ord k, Ord v) => Ord (Map k v) where
1298     compare m1 m2 = compare (toList m1) (toList m2)
1299
1300 {--------------------------------------------------------------------
1301   Monoid 
1302 --------------------------------------------------------------------}
1303
1304 instance (Ord k) => Monoid (Map k v) where
1305     mempty = empty
1306     mappend = union
1307     mconcat = unions
1308
1309 {--------------------------------------------------------------------
1310   Functor
1311 --------------------------------------------------------------------}
1312 instance Functor (Map k) where
1313   fmap f m  = map f m
1314
1315 {--------------------------------------------------------------------
1316   Show
1317 --------------------------------------------------------------------}
1318 instance (Show k, Show a) => Show (Map k a) where
1319   showsPrec d m  = showMap (toAscList m)
1320
1321 showMap :: (Show k,Show a) => [(k,a)] -> ShowS
1322 showMap []     
1323   = showString "{}" 
1324 showMap (x:xs) 
1325   = showChar '{' . showElem x . showTail xs
1326   where
1327     showTail []     = showChar '}'
1328     showTail (x:xs) = showChar ',' . showElem x . showTail xs
1329     
1330     showElem (k,x)  = shows k . showString ":=" . shows x
1331   
1332
1333 -- | /O(n)/. Show the tree that implements the map. The tree is shown
1334 -- in a compressed, hanging format.
1335 showTree :: (Show k,Show a) => Map k a -> String
1336 showTree m
1337   = showTreeWith showElem True False m
1338   where
1339     showElem k x  = show k ++ ":=" ++ show x
1340
1341
1342 {- | /O(n)/. The expression (@'showTreeWith' showelem hang wide map@) shows
1343  the tree that implements the map. Elements are shown using the @showElem@ function. If @hang@ is
1344  'True', a /hanging/ tree is shown otherwise a rotated tree is shown. If
1345  @wide@ is 'True', an extra wide version is shown.
1346
1347 >  Map> let t = fromDistinctAscList [(x,()) | x <- [1..5]]
1348 >  Map> putStrLn $ showTreeWith (\k x -> show (k,x)) True False t
1349 >  (4,())
1350 >  +--(2,())
1351 >  |  +--(1,())
1352 >  |  +--(3,())
1353 >  +--(5,())
1354 >
1355 >  Map> putStrLn $ showTreeWith (\k x -> show (k,x)) True True t
1356 >  (4,())
1357 >  |
1358 >  +--(2,())
1359 >  |  |
1360 >  |  +--(1,())
1361 >  |  |
1362 >  |  +--(3,())
1363 >  |
1364 >  +--(5,())
1365 >
1366 >  Map> putStrLn $ showTreeWith (\k x -> show (k,x)) False True t
1367 >  +--(5,())
1368 >  |
1369 >  (4,())
1370 >  |
1371 >  |  +--(3,())
1372 >  |  |
1373 >  +--(2,())
1374 >     |
1375 >     +--(1,())
1376
1377 -}
1378 showTreeWith :: (k -> a -> String) -> Bool -> Bool -> Map k a -> String
1379 showTreeWith showelem hang wide t
1380   | hang      = (showsTreeHang showelem wide [] t) ""
1381   | otherwise = (showsTree showelem wide [] [] t) ""
1382
1383 showsTree :: (k -> a -> String) -> Bool -> [String] -> [String] -> Map k a -> ShowS
1384 showsTree showelem wide lbars rbars t
1385   = case t of
1386       Tip -> showsBars lbars . showString "|\n"
1387       Bin sz kx x Tip Tip
1388           -> showsBars lbars . showString (showelem kx x) . showString "\n" 
1389       Bin sz kx x l r
1390           -> showsTree showelem wide (withBar rbars) (withEmpty rbars) r .
1391              showWide wide rbars .
1392              showsBars lbars . showString (showelem kx x) . showString "\n" .
1393              showWide wide lbars .
1394              showsTree showelem wide (withEmpty lbars) (withBar lbars) l
1395
1396 showsTreeHang :: (k -> a -> String) -> Bool -> [String] -> Map k a -> ShowS
1397 showsTreeHang showelem wide bars t
1398   = case t of
1399       Tip -> showsBars bars . showString "|\n" 
1400       Bin sz kx x Tip Tip
1401           -> showsBars bars . showString (showelem kx x) . showString "\n" 
1402       Bin sz kx x l r
1403           -> showsBars bars . showString (showelem kx x) . showString "\n" . 
1404              showWide wide bars .
1405              showsTreeHang showelem wide (withBar bars) l .
1406              showWide wide bars .
1407              showsTreeHang showelem wide (withEmpty bars) r
1408
1409
1410 showWide wide bars 
1411   | wide      = showString (concat (reverse bars)) . showString "|\n" 
1412   | otherwise = id
1413
1414 showsBars :: [String] -> ShowS
1415 showsBars bars
1416   = case bars of
1417       [] -> id
1418       _  -> showString (concat (reverse (tail bars))) . showString node
1419
1420 node           = "+--"
1421 withBar bars   = "|  ":bars
1422 withEmpty bars = "   ":bars
1423
1424 {--------------------------------------------------------------------
1425   Typeable
1426 --------------------------------------------------------------------}
1427
1428 #include "Typeable.h"
1429 INSTANCE_TYPEABLE2(Map,mapTc,"Map")
1430
1431 {--------------------------------------------------------------------
1432   Assertions
1433 --------------------------------------------------------------------}
1434 -- | /O(n)/. Test if the internal map structure is valid.
1435 valid :: Ord k => Map k a -> Bool
1436 valid t
1437   = balanced t && ordered t && validsize t
1438
1439 ordered t
1440   = bounded (const True) (const True) t
1441   where
1442     bounded lo hi t
1443       = case t of
1444           Tip              -> True
1445           Bin sz kx x l r  -> (lo kx) && (hi kx) && bounded lo (<kx) l && bounded (>kx) hi r
1446
1447 -- | Exported only for "Debug.QuickCheck"
1448 balanced :: Map k a -> Bool
1449 balanced t
1450   = case t of
1451       Tip              -> True
1452       Bin sz kx x l r  -> (size l + size r <= 1 || (size l <= delta*size r && size r <= delta*size l)) &&
1453                           balanced l && balanced r
1454
1455
1456 validsize t
1457   = (realsize t == Just (size t))
1458   where
1459     realsize t
1460       = case t of
1461           Tip             -> Just 0
1462           Bin sz kx x l r -> case (realsize l,realsize r) of
1463                               (Just n,Just m)  | n+m+1 == sz  -> Just sz
1464                               other            -> Nothing
1465
1466 {--------------------------------------------------------------------
1467   Utilities
1468 --------------------------------------------------------------------}
1469 foldlStrict f z xs
1470   = case xs of
1471       []     -> z
1472       (x:xx) -> let z' = f z x in seq z' (foldlStrict f z' xx)
1473
1474
1475 {-
1476 {--------------------------------------------------------------------
1477   Testing
1478 --------------------------------------------------------------------}
1479 testTree xs   = fromList [(x,"*") | x <- xs]
1480 test1 = testTree [1..20]
1481 test2 = testTree [30,29..10]
1482 test3 = testTree [1,4,6,89,2323,53,43,234,5,79,12,9,24,9,8,423,8,42,4,8,9,3]
1483
1484 {--------------------------------------------------------------------
1485   QuickCheck
1486 --------------------------------------------------------------------}
1487 qcheck prop
1488   = check config prop
1489   where
1490     config = Config
1491       { configMaxTest = 500
1492       , configMaxFail = 5000
1493       , configSize    = \n -> (div n 2 + 3)
1494       , configEvery   = \n args -> let s = show n in s ++ [ '\b' | _ <- s ]
1495       }
1496
1497
1498 {--------------------------------------------------------------------
1499   Arbitrary, reasonably balanced trees
1500 --------------------------------------------------------------------}
1501 instance (Enum k,Arbitrary a) => Arbitrary (Map k a) where
1502   arbitrary = sized (arbtree 0 maxkey)
1503             where maxkey  = 10000
1504
1505 arbtree :: (Enum k,Arbitrary a) => Int -> Int -> Int -> Gen (Map k a)
1506 arbtree lo hi n
1507   | n <= 0        = return Tip
1508   | lo >= hi      = return Tip
1509   | otherwise     = do{ x  <- arbitrary 
1510                       ; i  <- choose (lo,hi)
1511                       ; m  <- choose (1,30)
1512                       ; let (ml,mr)  | m==(1::Int)= (1,2)
1513                                      | m==2       = (2,1)
1514                                      | m==3       = (1,1)
1515                                      | otherwise  = (2,2)
1516                       ; l  <- arbtree lo (i-1) (n `div` ml)
1517                       ; r  <- arbtree (i+1) hi (n `div` mr)
1518                       ; return (bin (toEnum i) x l r)
1519                       }  
1520
1521
1522 {--------------------------------------------------------------------
1523   Valid tree's
1524 --------------------------------------------------------------------}
1525 forValid :: (Show k,Enum k,Show a,Arbitrary a,Testable b) => (Map k a -> b) -> Property
1526 forValid f
1527   = forAll arbitrary $ \t -> 
1528 --    classify (balanced t) "balanced" $
1529     classify (size t == 0) "empty" $
1530     classify (size t > 0  && size t <= 10) "small" $
1531     classify (size t > 10 && size t <= 64) "medium" $
1532     classify (size t > 64) "large" $
1533     balanced t ==> f t
1534
1535 forValidIntTree :: Testable a => (Map Int Int -> a) -> Property
1536 forValidIntTree f
1537   = forValid f
1538
1539 forValidUnitTree :: Testable a => (Map Int () -> a) -> Property
1540 forValidUnitTree f
1541   = forValid f
1542
1543
1544 prop_Valid 
1545   = forValidUnitTree $ \t -> valid t
1546
1547 {--------------------------------------------------------------------
1548   Single, Insert, Delete
1549 --------------------------------------------------------------------}
1550 prop_Single :: Int -> Int -> Bool
1551 prop_Single k x
1552   = (insert k x empty == singleton k x)
1553
1554 prop_InsertValid :: Int -> Property
1555 prop_InsertValid k
1556   = forValidUnitTree $ \t -> valid (insert k () t)
1557
1558 prop_InsertDelete :: Int -> Map Int () -> Property
1559 prop_InsertDelete k t
1560   = (lookup k t == Nothing) ==> delete k (insert k () t) == t
1561
1562 prop_DeleteValid :: Int -> Property
1563 prop_DeleteValid k
1564   = forValidUnitTree $ \t -> 
1565     valid (delete k (insert k () t))
1566
1567 {--------------------------------------------------------------------
1568   Balance
1569 --------------------------------------------------------------------}
1570 prop_Join :: Int -> Property 
1571 prop_Join k 
1572   = forValidUnitTree $ \t ->
1573     let (l,r) = split k t
1574     in valid (join k () l r)
1575
1576 prop_Merge :: Int -> Property 
1577 prop_Merge k
1578   = forValidUnitTree $ \t ->
1579     let (l,r) = split k t
1580     in valid (merge l r)
1581
1582
1583 {--------------------------------------------------------------------
1584   Union
1585 --------------------------------------------------------------------}
1586 prop_UnionValid :: Property
1587 prop_UnionValid
1588   = forValidUnitTree $ \t1 ->
1589     forValidUnitTree $ \t2 ->
1590     valid (union t1 t2)
1591
1592 prop_UnionInsert :: Int -> Int -> Map Int Int -> Bool
1593 prop_UnionInsert k x t
1594   = union (singleton k x) t == insert k x t
1595
1596 prop_UnionAssoc :: Map Int Int -> Map Int Int -> Map Int Int -> Bool
1597 prop_UnionAssoc t1 t2 t3
1598   = union t1 (union t2 t3) == union (union t1 t2) t3
1599
1600 prop_UnionComm :: Map Int Int -> Map Int Int -> Bool
1601 prop_UnionComm t1 t2
1602   = (union t1 t2 == unionWith (\x y -> y) t2 t1)
1603
1604 prop_UnionWithValid 
1605   = forValidIntTree $ \t1 ->
1606     forValidIntTree $ \t2 ->
1607     valid (unionWithKey (\k x y -> x+y) t1 t2)
1608
1609 prop_UnionWith :: [(Int,Int)] -> [(Int,Int)] -> Bool
1610 prop_UnionWith xs ys
1611   = sum (elems (unionWith (+) (fromListWith (+) xs) (fromListWith (+) ys))) 
1612     == (sum (Prelude.map snd xs) + sum (Prelude.map snd ys))
1613
1614 prop_DiffValid
1615   = forValidUnitTree $ \t1 ->
1616     forValidUnitTree $ \t2 ->
1617     valid (difference t1 t2)
1618
1619 prop_Diff :: [(Int,Int)] -> [(Int,Int)] -> Bool
1620 prop_Diff xs ys
1621   =  List.sort (keys (difference (fromListWith (+) xs) (fromListWith (+) ys))) 
1622     == List.sort ((List.\\) (nub (Prelude.map fst xs))  (nub (Prelude.map fst ys)))
1623
1624 prop_IntValid
1625   = forValidUnitTree $ \t1 ->
1626     forValidUnitTree $ \t2 ->
1627     valid (intersection t1 t2)
1628
1629 prop_Int :: [(Int,Int)] -> [(Int,Int)] -> Bool
1630 prop_Int xs ys
1631   =  List.sort (keys (intersection (fromListWith (+) xs) (fromListWith (+) ys))) 
1632     == List.sort (nub ((List.intersect) (Prelude.map fst xs)  (Prelude.map fst ys)))
1633
1634 {--------------------------------------------------------------------
1635   Lists
1636 --------------------------------------------------------------------}
1637 prop_Ordered
1638   = forAll (choose (5,100)) $ \n ->
1639     let xs = [(x,()) | x <- [0..n::Int]] 
1640     in fromAscList xs == fromList xs
1641
1642 prop_List :: [Int] -> Bool
1643 prop_List xs
1644   = (sort (nub xs) == [x | (x,()) <- toList (fromList [(x,()) | x <- xs])])
1645 -}