Ticket #1611: Map.diff
| File Map.diff, 90.8 KB (added by guest, 6 years ago) |
|---|
-
Data/IntMap.hs
diff -rN -u old-src/Data/IntMap.hs new-src/Data/IntMap.hs
old new 1 {- # OPTIONS -cpp -fglasgow-exts -fno-bang-patterns #-}1 {-TODO - enable # OPTIONS -cpp -fglasgow-exts -fno-bang-patterns #-} 2 2 ----------------------------------------------------------------------------- 3 3 -- | 4 4 -- Module : Data.IntMap … … 30 30 -- Information Coded In Alphanumeric/\", Journal of the ACM, 15(4), 31 31 -- October 1968, pages 514-534. 32 32 -- 33 -- Operation comments contain the operation time complexity in 34 -- the Big-O notation <http://en.wikipedia.org/wiki/Big_O_notation>. 33 35 -- Many operations have a worst-case complexity of /O(min(n,W))/. 34 36 -- This means that the operation can become linear in the number of 35 37 -- elements with a maximum of /W/ -- the number of bits in an 'Int' … … 215 217 Operators 216 218 --------------------------------------------------------------------} 217 219 218 -- | /O(min(n,W))/.Find the value at a key.219 -- Calls 'error' when the element can not be found. 220 -- | Find the value at a key. 221 -- Calls 'error' when the element can not be found. /O(min(n,W))/. 220 222 221 223 (!) :: IntMap a -> Key -> a 222 224 m ! k = find' k m 223 225 224 -- | /O(n+m)/. See 'difference'.226 -- | See 'difference'. /O(n+m)/. 225 227 (\\) :: IntMap a -> IntMap b -> IntMap a 226 228 m1 \\ m2 = difference m1 m2 227 229 … … 268 270 {-------------------------------------------------------------------- 269 271 Query 270 272 --------------------------------------------------------------------} 271 -- | /O(1)/. Is the map empty?273 -- | Is the map empty? /O(1)/. 272 274 null :: IntMap a -> Bool 273 275 null Nil = True 274 276 null other = False 275 277 276 -- | /O(n)/. Number of elements in the map.278 -- | Number of elements in the map. /O(n)/. 277 279 size :: IntMap a -> Int 278 280 size t 279 281 = case t of … … 281 283 Tip k x -> 1 282 284 Nil -> 0 283 285 284 -- | /O(min(n,W))/. Is the key a member of the map?286 -- | Is the key a member of the map? /O(min(n,W))/. 285 287 member :: Key -> IntMap a -> Bool 286 288 member k m 287 289 = case lookup k m of 288 290 Nothing -> False 289 291 Just x -> True 290 292 291 -- | /O(log n)/. Is the key not a member of the map?293 -- | Is the key not a member of the map? /O(log n)/. 292 294 notMember :: Key -> IntMap a -> Bool 293 295 notMember k m = not $ member k m 294 296 295 -- | /O(min(n,W))/. Lookup the value at a key in the map.297 -- | Lookup the value at a key in the map. /O(min(n,W))/. 296 298 lookup :: (Monad m) => Key -> IntMap a -> m a 297 299 lookup k t = case lookup' k t of 298 300 Just x -> return x … … 320 322 Just x -> x 321 323 322 324 323 -- | /O(min(n,W))/.The expression @('findWithDefault' def k map)@325 -- | The expression @('findWithDefault' def k map)@ 324 326 -- returns the value at key @k@ or returns @def@ when the key is not an 325 -- element of the map. 327 -- element of the map. /O(min(n,W))/. 326 328 findWithDefault :: a -> Key -> IntMap a -> a 327 329 findWithDefault def k m 328 330 = case lookup k m of … … 332 334 {-------------------------------------------------------------------- 333 335 Construction 334 336 --------------------------------------------------------------------} 335 -- | /O(1)/. The empty map.337 -- | The empty map. /O(1)/. 336 338 empty :: IntMap a 337 339 empty 338 340 = Nil 339 341 340 -- | /O(1)/. A map of one element.342 -- | A map of one element. /O(1)/. 341 343 singleton :: Key -> a -> IntMap a 342 344 singleton k x 343 345 = Tip k x … … 345 347 {-------------------------------------------------------------------- 346 348 Insert 347 349 --------------------------------------------------------------------} 348 -- | /O(min(n,W))/.Insert a new key\/value pair in the map.350 -- | Insert a new key\/value pair in the map. 349 351 -- If the key is already present in the map, the associated value is 350 352 -- replaced with the supplied value, i.e. 'insert' is equivalent to 351 -- @'insertWith' 'const'@. 353 -- @'insertWith' 'const'@. /O(min(n,W))/. 352 354 insert :: Key -> a -> IntMap a -> IntMap a 353 355 insert k x t 354 356 = case t of … … 362 364 Nil -> Tip k x 363 365 364 366 -- right-biased insertion, used by 'union' 365 -- | /O(min(n,W))/.Insert with a combining function.367 -- | Insert with a combining function. 366 368 -- @'insertWith' f key value mp@ 367 369 -- will insert the pair (key, value) into @mp@ if key does 368 370 -- not exist in the map. If the key does exist, the function will 369 -- insert @f new_value old_value@. 371 -- insert @f new_value old_value@. /O(min(n,W))/. 370 372 insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a 371 373 insertWith f k x t 372 374 = insertWithKey (\k x y -> f x y) k x t 373 375 374 -- | /O(min(n,W))/.Insert with a combining function.376 -- | Insert with a combining function. 375 377 -- @'insertWithKey' f key value mp@ 376 378 -- will insert the pair (key, value) into @mp@ if key does 377 379 -- not exist in the map. If the key does exist, the function will 378 -- insert @f key new_value old_value@. 380 -- insert @f key new_value old_value@. /O(min(n,W))/. 379 381 insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a 380 382 insertWithKey f k x t 381 383 = case t of … … 389 391 Nil -> Tip k x 390 392 391 393 392 -- | /O(min(n,W))/.The expression (@'insertLookupWithKey' f k x map@)394 -- | The expression (@'insertLookupWithKey' f k x map@) 393 395 -- is a pair where the first element is equal to (@'lookup' k map@) 394 -- and the second element equal to (@'insertWithKey' f k x map@). 396 -- and the second element equal to (@'insertWithKey' f k x map@). /O(min(n,W))/. 395 397 insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a) 396 398 insertLookupWithKey f k x t 397 399 = case t of … … 409 411 Deletion 410 412 [delete] is the inlined version of [deleteWith (\k x -> Nothing)] 411 413 --------------------------------------------------------------------} 412 -- | /O(min(n,W))/.Delete a key and its value from the map. When the key is not413 -- a member of the map, the original map is returned. 414 -- | Delete a key and its value from the map. When the key is not 415 -- a member of the map, the original map is returned. /O(min(n,W))/. 414 416 delete :: Key -> IntMap a -> IntMap a 415 417 delete k t 416 418 = case t of … … 423 425 | otherwise -> t 424 426 Nil -> Nil 425 427 426 -- | /O(min(n,W))/.Adjust a value at a specific key. When the key is not427 -- a member of the map, the original map is returned. 428 -- | Adjust a value at a specific key. When the key is not 429 -- a member of the map, the original map is returned. /O(min(n,W))/. 428 430 adjust :: (a -> a) -> Key -> IntMap a -> IntMap a 429 431 adjust f k m 430 432 = adjustWithKey (\k x -> f x) k m 431 433 432 -- | /O(min(n,W))/.Adjust a value at a specific key. When the key is not433 -- a member of the map, the original map is returned. 434 -- | Adjust a value at a specific key. When the key is not 435 -- a member of the map, the original map is returned. /O(min(n,W))/. 434 436 adjustWithKey :: (Key -> a -> a) -> Key -> IntMap a -> IntMap a 435 437 adjustWithKey f k m 436 438 = updateWithKey (\k x -> Just (f k x)) k m 437 439 438 -- | /O(min(n,W))/.The expression (@'update' f k map@) updates the value @x@440 -- | The expression (@'update' f k map@) updates the value @x@ 439 441 -- at @k@ (if it is in the map). If (@f x@) is 'Nothing', the element is 440 442 -- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@. 443 -- /O(min(n,W))/. 441 444 update :: (a -> Maybe a) -> Key -> IntMap a -> IntMap a 442 445 update f k m 443 446 = updateWithKey (\k x -> f x) k m 444 447 445 -- | /O(min(n,W))/.The expression (@'update' f k map@) updates the value @x@448 -- | The expression (@'update' f k map@) updates the value @x@ 446 449 -- at @k@ (if it is in the map). If (@f k x@) is 'Nothing', the element is 447 450 -- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@. 451 -- /O(min(n,W))/. 448 452 updateWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a 449 453 updateWithKey f k t 450 454 = case t of … … 459 463 | otherwise -> t 460 464 Nil -> Nil 461 465 462 -- | /O(min(n,W))/. Lookup and update.466 -- | Lookup and update. /O(min(n,W))/. 463 467 updateLookupWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a,IntMap a) 464 468 updateLookupWithKey f k t 465 469 = case t of … … 476 480 477 481 478 482 479 -- | /O(log n)/.The expression (@'alter' f k map@) alters the value @x@ at @k@, or absence thereof.483 -- | The expression (@'alter' f k map@) alters the value @x@ at @k@, or absence thereof. 480 484 -- 'alter' can be used to insert, delete, or update a value in a 'Map'. 481 -- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@ 485 -- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@. /O(log n)/. 482 486 alter f k t 483 487 = case t of 484 488 Bin p m l r … … 512 516 unionsWith f ts 513 517 = foldlStrict (unionWith f) empty ts 514 518 515 -- | /O(n+m)/.The (left-biased) union of two maps.519 -- | The (left-biased) union of two maps. 516 520 -- It prefers the first map when duplicate keys are encountered, 517 -- i.e. (@'union' == 'unionWith' 'const'@). 521 -- i.e. (@'union' == 'unionWith' 'const'@). /O(n+m)/. 518 522 union :: IntMap a -> IntMap a -> IntMap a 519 523 union t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2) 520 524 | shorter m1 m2 = union1 … … 535 539 union Nil t = t 536 540 union t Nil = t 537 541 538 -- | /O(n+m)/. The union with a combining function.542 -- | The union with a combining function. /O(n+m)/. 539 543 unionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a 540 544 unionWith f m1 m2 541 545 = unionWithKey (\k x y -> f x y) m1 m2 542 546 543 -- | /O(n+m)/. The union with a combining function.547 -- | The union with a combining function. /O(n+m)/. 544 548 unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a 545 549 unionWithKey f t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2) 546 550 | shorter m1 m2 = union1 … … 564 568 {-------------------------------------------------------------------- 565 569 Difference 566 570 --------------------------------------------------------------------} 567 -- | /O(n+m)/. Difference between two maps (based on keys).571 -- | Difference between two maps (based on keys). /O(n+m)/. 568 572 difference :: IntMap a -> IntMap b -> IntMap a 569 573 difference t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2) 570 574 | shorter m1 m2 = difference1 … … 588 592 difference t (Tip k x) = delete k t 589 593 difference t Nil = t 590 594 591 -- | /O(n+m)/. Difference with a combining function.595 -- | Difference with a combining function. /O(n+m)/. 592 596 differenceWith :: (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a 593 597 differenceWith f m1 m2 594 598 = differenceWithKey (\k x y -> f x y) m1 m2 595 599 596 -- | /O(n+m)/.Difference with a combining function. When two equal keys are600 -- | Difference with a combining function. When two equal keys are 597 601 -- encountered, the combining function is applied to the key and both values. 598 602 -- If it returns 'Nothing', the element is discarded (proper set difference). 599 603 -- If it returns (@'Just' y@), the element is updated with a new value @y@. 604 -- /O(n+m)/. 600 605 differenceWithKey :: (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a 601 606 differenceWithKey f t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2) 602 607 | shorter m1 m2 = difference1 … … 627 632 {-------------------------------------------------------------------- 628 633 Intersection 629 634 --------------------------------------------------------------------} 630 -- | /O(n+m)/. The (left-biased) intersection of two maps (based on keys).635 -- | The (left-biased) intersection of two maps (based on keys). /O(n+m)/. 631 636 intersection :: IntMap a -> IntMap b -> IntMap a 632 637 intersection t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2) 633 638 | shorter m1 m2 = intersection1 … … 653 658 intersection Nil t = Nil 654 659 intersection t Nil = Nil 655 660 656 -- | /O(n+m)/. The intersection with a combining function.661 -- | The intersection with a combining function. /O(n+m)/. 657 662 intersectionWith :: (a -> b -> a) -> IntMap a -> IntMap b -> IntMap a 658 663 intersectionWith f m1 m2 659 664 = intersectionWithKey (\k x y -> f x y) m1 m2 660 665 661 -- | /O(n+m)/. The intersection with a combining function.666 -- | The intersection with a combining function. /O(n+m)/. 662 667 intersectionWithKey :: (Key -> a -> b -> a) -> IntMap a -> IntMap b -> IntMap a 663 668 intersectionWithKey f t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2) 664 669 | shorter m1 m2 = intersection1 … … 690 695 Min\/Max 691 696 --------------------------------------------------------------------} 692 697 693 -- | /O(log n)/. Update the value at the minimal key.698 -- | Update the value at the minimal key. /O(log n)/. 694 699 updateMinWithKey :: (Key -> a -> a) -> IntMap a -> IntMap a 695 700 updateMinWithKey f t 696 701 = case t of … … 704 709 Bin p m l r -> let t' = updateMinWithKeyUnsigned f r in Bin p m l t' 705 710 Tip k y -> Tip k (f k y) 706 711 707 -- | /O(log n)/. Update the value at the maximal key.712 -- | Update the value at the maximal key. /O(log n)/. 708 713 updateMaxWithKey :: (Key -> a -> a) -> IntMap a -> IntMap a 709 714 updateMaxWithKey f t 710 715 = case t of … … 719 724 Tip k y -> Tip k (f k y) 720 725 721 726 722 -- | /O(log n)/.Retrieves the maximal (key,value) couple of the map, and the map stripped from that element.723 -- @fail@s (in the monad) when passed an empty map. 727 -- | Retrieves the maximal (key,value) couple of the map, and the map stripped from that element. 728 -- @fail@s (in the monad) when passed an empty map. /O(log n)/. 724 729 maxViewWithKey :: (Monad m) => IntMap a -> m ((Key, a), IntMap a) 725 730 maxViewWithKey t 726 731 = case t of … … 734 739 Bin p m l r -> let (result,t') = maxViewUnsigned r in (result,bin p m l t') 735 740 Tip k y -> ((k,y), Nil) 736 741 737 -- | /O(log n)/.Retrieves the minimal (key,value) couple of the map, and the map stripped from that element.738 -- @fail@s (in the monad) when passed an empty map. 742 -- | Retrieves the minimal (key,value) couple of the map, and the map stripped from that element. 743 -- @fail@s (in the monad) when passed an empty map. /O(log n)/. 739 744 minViewWithKey :: (Monad m) => IntMap a -> m ((Key, a), IntMap a) 740 745 minViewWithKey t 741 746 = case t of … … 750 755 Tip k y -> ((k,y),Nil) 751 756 752 757 753 -- | /O(log n)/. Update the value at the maximal key.758 -- | Update the value at the maximal key. /O(log n)/. 754 759 updateMax :: (a -> a) -> IntMap a -> IntMap a 755 760 updateMax f = updateMaxWithKey (const f) 756 761 757 -- | /O(log n)/. Update the value at the minimal key.762 -- | Update the value at the minimal key. /O(log n)/. 758 763 updateMin :: (a -> a) -> IntMap a -> IntMap a 759 764 updateMin f = updateMinWithKey (const f) 760 765 … … 768 773 first f (x,y) = (f x,y) 769 774 770 775 771 -- | /O(log n)/.Retrieves the maximal key of the map, and the map stripped from that element.772 -- @fail@s (in the monad) when passed an empty map. 776 -- | Retrieves the maximal key of the map, and the map stripped from that element. 777 -- @fail@s (in the monad) when passed an empty map. /O(log n)/. 773 778 maxView t = liftM (first snd) (maxViewWithKey t) 774 779 775 -- | /O(log n)/.Retrieves the minimal key of the map, and the map stripped from that element.780 -- | Retrieves the minimal key of the map, and the map stripped from that element. 776 781 -- @fail@s (in the monad) when passed an empty map. 782 -- /O(log n)/. 777 783 minView t = liftM (first snd) (minViewWithKey t) 778 784 779 -- | /O(log n)/. Delete and find the maximal element.785 -- | Delete and find the maximal element. /O(log n)/. 780 786 deleteFindMax = runIdentity . maxView 781 787 782 -- | /O(log n)/. Delete and find the minimal element.788 -- | Delete and find the minimal element. /O(log n)/. 783 789 deleteFindMin = runIdentity . minView 784 790 785 -- | /O(log n)/. The minimal key of the map.791 -- | The minimal key of the map. /O(log n)/. 786 792 findMin = fst . runIdentity . minView 787 793 788 -- | /O(log n)/. The maximal key of the map.794 -- | The maximal key of the map. /O(log n)/. 789 795 findMax = fst . runIdentity . maxView 790 796 791 -- | /O(log n)/. Delete the minimal key.797 -- | Delete the minimal key. /O(log n)/. 792 798 deleteMin = snd . runIdentity . minView 793 799 794 -- | /O(log n)/. Delete the maximal key.800 -- | Delete the maximal key. /O(log n)/. 795 801 deleteMax = snd . runIdentity . maxView 796 802 797 803 798 804 {-------------------------------------------------------------------- 799 805 Submap 800 806 --------------------------------------------------------------------} 801 -- | /O(n+m)/.Is this a proper submap? (ie. a submap but not equal).802 -- Defined as (@'isProperSubmapOf' = 'isProperSubmapOfBy' (==)@). 807 -- | Is this a proper submap? (ie. a submap but not equal). 808 -- Defined as (@'isProperSubmapOf' = 'isProperSubmapOfBy' (==)@). /O(n+m)/. 803 809 isProperSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool 804 810 isProperSubmapOf m1 m2 805 811 = isProperSubmapOfBy (==) m1 m2 806 812 807 {- | /O(n+m)/.Is this a proper submap? (ie. a submap but not equal).813 {- | Is this a proper submap? (ie. a submap but not equal). 808 814 The expression (@'isProperSubmapOfBy' f m1 m2@) returns 'True' when 809 815 @m1@ and @m2@ are not equal, 810 816 all keys in @m1@ are in @m2@, and when @f@ returns 'True' when … … 819 825 > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)]) 820 826 > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)]) 821 827 > isProperSubmapOfBy (<) (fromList [(1,1)]) (fromList [(1,1),(2,2)]) 828 829 /O(n+m)/. 822 830 -} 823 831 isProperSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool 824 832 isProperSubmapOfBy pred t1 t2 … … 852 860 submapCmp pred Nil Nil = EQ 853 861 submapCmp pred Nil t = LT 854 862 855 -- | /O(n+m)/.Is this a submap?856 -- Defined as (@'isSubmapOf' = 'isSubmapOfBy' (==)@). 863 -- | Is this a submap? 864 -- Defined as (@'isSubmapOf' = 'isSubmapOfBy' (==)@). /O(n+m)/. 857 865 isSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool 858 866 isSubmapOf m1 m2 859 867 = isSubmapOfBy (==) m1 m2 860 868 861 {- | /O(n+m)/. 862 The expression (@'isSubmapOfBy' f m1 m2@) returns 'True' if 869 {- | The expression (@'isSubmapOfBy' f m1 m2@) returns 'True' if 863 870 all keys in @m1@ are in @m2@, and when @f@ returns 'True' when 864 871 applied to their respective values. For example, the following 865 872 expressions are all 'True': … … 873 880 > isSubmapOfBy (==) (fromList [(1,2)]) (fromList [(1,1),(2,2)]) 874 881 > isSubmapOfBy (<) (fromList [(1,1)]) (fromList [(1,1),(2,2)]) 875 882 > isSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)]) 883 884 /O(n+m)/. 876 885 -} 877 886 878 887 isSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool … … 890 899 {-------------------------------------------------------------------- 891 900 Mapping 892 901 --------------------------------------------------------------------} 893 -- | /O(n)/. Map a function over all values in the map.902 -- | Map a function over all values in the map. /O(n)/. 894 903 map :: (a -> b) -> IntMap a -> IntMap b 895 904 map f m 896 905 = mapWithKey (\k x -> f x) m 897 906 898 -- | /O(n)/. Map a function over all values in the map.907 -- | Map a function over all values in the map. /O(n)/. 899 908 mapWithKey :: (Key -> a -> b) -> IntMap a -> IntMap b 900 909 mapWithKey f t 901 910 = case t of … … 903 912 Tip k x -> Tip k (f k x) 904 913 Nil -> Nil 905 914 906 -- | /O(n)/.The function @'mapAccum'@ threads an accumulating907 -- argument through the map in ascending order of keys. 915 -- | The function @'mapAccum'@ threads an accumulating 916 -- argument through the map in ascending order of keys. /O(n)/. 908 917 mapAccum :: (a -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c) 909 918 mapAccum f a m 910 919 = mapAccumWithKey (\a k x -> f a x) a m 911 920 912 -- | /O(n)/.The function @'mapAccumWithKey'@ threads an accumulating913 -- argument through the map in ascending order of keys. 921 -- | The function @'mapAccumWithKey'@ threads an accumulating 922 -- argument through the map in ascending order of keys. /O(n)/. 914 923 mapAccumWithKey :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c) 915 924 mapAccumWithKey f a t 916 925 = mapAccumL f a t 917 926 918 -- | /O(n)/.The function @'mapAccumL'@ threads an accumulating919 -- argument through the map in ascending order of keys. 927 -- | The function @'mapAccumL'@ threads an accumulating 928 -- argument through the map in ascending order of keys. /O(n)/. 920 929 mapAccumL :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c) 921 930 mapAccumL f a t 922 931 = case t of … … 927 936 Nil -> (a,Nil) 928 937 929 938 930 -- | /O(n)/.The function @'mapAccumR'@ threads an accumulating931 -- argument throught the map in descending order of keys. 939 -- | The function @'mapAccumR'@ threads an accumulating 940 -- argument throught the map in descending order of keys. /O(n)/. 932 941 mapAccumR :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c) 933 942 mapAccumR f a t 934 943 = case t of … … 941 950 {-------------------------------------------------------------------- 942 951 Filter 943 952 --------------------------------------------------------------------} 944 -- | /O(n)/. Filter all values that satisfy some predicate.953 -- | Filter all values that satisfy some predicate. /O(n)/. 945 954 filter :: (a -> Bool) -> IntMap a -> IntMap a 946 955 filter p m 947 956 = filterWithKey (\k x -> p x) m 948 957 949 -- | /O(n)/. Filter all keys\/values that satisfy some predicate.958 -- | Filter all keys\/values that satisfy some predicate. /O(n)/. 950 959 filterWithKey :: (Key -> a -> Bool) -> IntMap a -> IntMap a 951 960 filterWithKey pred t 952 961 = case t of … … 957 966 | otherwise -> Nil 958 967 Nil -> Nil 959 968 960 -- | /O(n)/. partition the map according to some predicate. The first969 -- | Partition the map according to some predicate. The first 961 970 -- map contains all elements that satisfy the predicate, the second all 962 -- elements that fail the predicate. See also 'split'. 971 -- elements that fail the predicate. See also 'split'. /O(n)/. 963 972 partition :: (a -> Bool) -> IntMap a -> (IntMap a,IntMap a) 964 973 partition p m 965 974 = partitionWithKey (\k x -> p x) m 966 975 967 -- | /O(n)/. partition the map according to some predicate. The first976 -- | Partition the map according to some predicate. The first 968 977 -- map contains all elements that satisfy the predicate, the second all 969 -- elements that fail the predicate. See also 'split'. 978 -- elements that fail the predicate. See also 'split'. /O(n)/. 970 979 partitionWithKey :: (Key -> a -> Bool) -> IntMap a -> (IntMap a,IntMap a) 971 980 partitionWithKey pred t 972 981 = case t of … … 979 988 | otherwise -> (Nil,t) 980 989 Nil -> (Nil,Nil) 981 990 982 -- | /O(n)/. Map values and collect the 'Just' results.991 -- | Map values and collect the 'Just' results. /O(n)/. 983 992 mapMaybe :: (a -> Maybe b) -> IntMap a -> IntMap b 984 993 mapMaybe f m 985 994 = mapMaybeWithKey (\k x -> f x) m 986 995 987 -- | /O(n)/. Map keys\/values and collect the 'Just' results.996 -- | Map keys\/values and collect the 'Just' results. /O(n)/. 988 997 mapMaybeWithKey :: (Key -> a -> Maybe b) -> IntMap a -> IntMap b 989 998 mapMaybeWithKey f (Bin p m l r) 990 999 = bin p m (mapMaybeWithKey f l) (mapMaybeWithKey f r) … … 993 1002 Nothing -> Nil 994 1003 mapMaybeWithKey f Nil = Nil 995 1004 996 -- | /O(n)/. Map values and separate the 'Left' and 'Right' results.1005 -- | Map values and separate the 'Left' and 'Right' results. /O(n)/. 997 1006 mapEither :: (a -> Either b c) -> IntMap a -> (IntMap b, IntMap c) 998 1007 mapEither f m 999 1008 = mapEitherWithKey (\k x -> f x) m 1000 1009 1001 -- | /O(n)/. Map keys\/values and separate the 'Left' and 'Right' results.1010 -- | Map keys\/values and separate the 'Left' and 'Right' results. /O(n)/. 1002 1011 mapEitherWithKey :: (Key -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c) 1003 1012 mapEitherWithKey f (Bin p m l r) 1004 1013 = (bin p m l1 r1, bin p m l2 r2) … … 1010 1019 Right z -> (Nil, Tip k z) 1011 1020 mapEitherWithKey f Nil = (Nil, Nil) 1012 1021 1013 -- | /O(log n)/.The expression (@'split' k map@) is a pair @(map1,map2)@1022 -- | The expression (@'split' k map@) is a pair @(map1,map2)@ 1014 1023 -- where all keys in @map1@ are lower than @k@ and all keys in 1015 1024 -- @map2@ larger than @k@. Any key equal to @k@ is found in neither @map1@ nor @map2@. 1025 -- /O(log n)/. 1016 1026 split :: Key -> IntMap a -> (IntMap a,IntMap a) 1017 1027 split k t 1018 1028 = case t of … … 1040 1050 | otherwise -> (Nil,Nil) 1041 1051 Nil -> (Nil,Nil) 1042 1052 1043 -- | /O(log n)/.Performs a 'split' but also returns whether the pivot1044 -- key was found in the original map. 1053 -- | Performs a 'split' but also returns whether the pivot 1054 -- key was found in the original map. /O(log n)/. 1045 1055 splitLookup :: Key -> IntMap a -> (IntMap a,Maybe a,IntMap a) 1046 1056 splitLookup k t 1047 1057 = case t of … … 1072 1082 {-------------------------------------------------------------------- 1073 1083 Fold 1074 1084 --------------------------------------------------------------------} 1075 -- | /O(n)/.Fold the values in the map, such that1085 -- | Fold the values in the map, such that 1076 1086 -- @'fold' f z == 'Prelude.foldr' f z . 'elems'@. 1077 1087 -- For example, 1078 1088 -- 1079 1089 -- > elems map = fold (:) [] map 1080 1090 -- 1091 -- /O(n)/. 1081 1092 fold :: (a -> b -> b) -> b -> IntMap a -> b 1082 1093 fold f z t 1083 1094 = foldWithKey (\k x y -> f x y) z t 1084 1095 1085 -- | /O(n)/.Fold the keys and values in the map, such that1096 -- | Fold the keys and values in the map, such that 1086 1097 -- @'foldWithKey' f z == 'Prelude.foldr' ('uncurry' f) z . 'toAscList'@. 1087 1098 -- For example, 1088 1099 -- 1089 1100 -- > keys map = foldWithKey (\k x ks -> k:ks) [] map 1090 1101 -- 1102 -- /O(n)/. 1091 1103 foldWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> b 1092 1104 foldWithKey f z t 1093 1105 = foldr f z t … … 1112 1124 {-------------------------------------------------------------------- 1113 1125 List variations 1114 1126 --------------------------------------------------------------------} 1115 -- | /O(n)/.1116 -- Return all elements of the map in the ascending order of their keys.1127 -- | Return all elements of the map in the ascending order of their keys. 1128 -- /O(n)/. 1117 1129 elems :: IntMap a -> [a] 1118 1130 elems m 1119 1131 = foldWithKey (\k x xs -> x:xs) [] m 1120 1132 1121 -- | /O(n)/. Return all keys of the map in ascending order.1133 -- | Return all keys of the map in ascending order. /O(n)/. 1122 1134 keys :: IntMap a -> [Key] 1123 1135 keys m 1124 1136 = foldWithKey (\k x ks -> k:ks) [] m 1125 1137 1126 -- | /O(n*min(n,W))/. The set of all keys of the map.1138 -- | The set of all keys of the map. /O(n*min(n,W))/. 1127 1139 keysSet :: IntMap a -> IntSet.IntSet 1128 1140 keysSet m = IntSet.fromDistinctAscList (keys m) 1129 1141 1130 1142 1131 -- | /O(n)/. Return all key\/value pairs in the map in ascending key order.1143 -- | Return all key\/value pairs in the map in ascending key order. /O(n)/. 1132 1144 assocs :: IntMap a -> [(Key,a)] 1133 1145 assocs m 1134 1146 = toList m … … 1137 1149 {-------------------------------------------------------------------- 1138 1150 Lists 1139 1151 --------------------------------------------------------------------} 1140 -- | /O(n)/. Convert the map to a list of key\/value pairs.1152 -- | Convert the map to a list of key\/value pairs. /O(n)/. 1141 1153 toList :: IntMap a -> [(Key,a)] 1142 1154 toList t 1143 1155 = foldWithKey (\k x xs -> (k,x):xs) [] t 1144 1156 1145 -- | /O(n)/.Convert the map to a list of key\/value pairs where the1146 -- keys are in ascending order. 1157 -- | Convert the map to a list of key\/value pairs where the 1158 -- keys are in ascending order. /O(n)/. 1147 1159 toAscList :: IntMap a -> [(Key,a)] 1148 1160 toAscList t 1149 1161 = -- NOTE: the following algorithm only works for big-endian trees 1150 1162 let (pos,neg) = span (\(k,x) -> k >=0) (foldr (\k x xs -> (k,x):xs) [] t) in neg ++ pos 1151 1163 1152 -- | /O(n*min(n,W))/. Create a map from a list of key\/value pairs.1164 -- | Create a map from a list of key\/value pairs. /O(n*min(n,W))/. 1153 1165 fromList :: [(Key,a)] -> IntMap a 1154 1166 fromList xs 1155 1167 = foldlStrict ins empty xs 1156 1168 where 1157 1169 ins t (k,x) = insert k x t 1158 1170 1159 -- | /O(n*min(n,W))/. Create a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'. 1171 -- | Create a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'. 1172 -- /O(n*min(n,W))/. 1160 1173 fromListWith :: (a -> a -> a) -> [(Key,a)] -> IntMap a 1161 1174 fromListWith f xs 1162 1175 = fromListWithKey (\k x y -> f x y) xs 1163 1176 1164 -- | /O(n*min(n,W))/. Build a map from a list of key\/value pairs with a combining function. See also fromAscListWithKey'. 1177 -- | Build a map from a list of key\/value pairs with a combining function. See also fromAscListWithKey'. 1178 -- /O(n*min(n,W))/. 1165 1179 fromListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a 1166 1180 fromListWithKey f xs 1167 1181 = foldlStrict ins empty xs 1168 1182 where 1169 1183 ins t (k,x) = insertWithKey f k x t 1170 1184 1171 -- | /O(n*min(n,W))/.Build a map from a list of key\/value pairs where1172 -- the keys are in ascending order. 1185 -- | Build a map from a list of key\/value pairs where 1186 -- the keys are in ascending order. /O(n*min(n,W))/. 1173 1187 fromAscList :: [(Key,a)] -> IntMap a 1174 1188 fromAscList xs 1175 1189 = fromList xs 1176 1190 1177 -- | /O(n*min(n,W))/.Build a map from a list of key\/value pairs where1191 -- | Build a map from a list of key\/value pairs where 1178 1192 -- the keys are in ascending order, with a combining function on equal keys. 1193 -- /O(n*min(n,W))/. 1179 1194 fromAscListWith :: (a -> a -> a) -> [(Key,a)] -> IntMap a 1180 1195 fromAscListWith f xs 1181 1196 = fromListWith f xs 1182 1197 1183 -- | /O(n*min(n,W))/.Build a map from a list of key\/value pairs where1198 -- | Build a map from a list of key\/value pairs where 1184 1199 -- the keys are in ascending order, with a combining function on equal keys. 1200 -- /O(n*min(n,W))/. 1185 1201 fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a 1186 1202 fromAscListWithKey f xs 1187 1203 = fromListWithKey f xs 1188 1204 1189 -- | /O(n*min(n,W))/.Build a map from a list of key\/value pairs where1190 -- the keys are in ascending order and all distinct. 1205 -- | Build a map from a list of key\/value pairs where 1206 -- the keys are in ascending order and all distinct. /O(n*min(n,W))/. 1191 1207 fromDistinctAscList :: [(Key,a)] -> IntMap a 1192 1208 fromDistinctAscList xs 1193 1209 = fromList xs … … 1277 1293 {-------------------------------------------------------------------- 1278 1294 Debugging 1279 1295 --------------------------------------------------------------------} 1280 -- | /O(n)/.Show the tree that implements the map. The tree is shown1281 -- in a compressed, hanging format. 1296 -- | Show the tree that implements the map. The tree is shown 1297 -- in a compressed, hanging format. /O(n)/. 1282 1298 showTree :: Show a => IntMap a -> String 1283 1299 showTree s 1284 1300 = showTreeWith True False s 1285 1301 1286 1302 1287 {- | /O(n)/.The expression (@'showTreeWith' hang wide map@) shows1303 {- | The expression (@'showTreeWith' hang wide map@) shows 1288 1304 the tree that implements the map. If @hang@ is 1289 1305 'True', a /hanging/ tree is shown otherwise a rotated tree is shown. If 1290 @wide@ is 'True', an extra wide version is shown. 1306 @wide@ is 'True', an extra wide version is shown. /O(n)/. 1291 1307 -} 1292 1308 showTreeWith :: Show a => Bool -> Bool -> IntMap a -> String 1293 1309 showTreeWith hang wide t -
Data/Map.hs
diff -rN -u old-src/Data/Map.hs new-src/Data/Map.hs
old new 1 {- # OPTIONS_GHC -fno-bang-patterns #-}1 {-TODO - enable # OPTIONS_GHC -fno-bang-patterns #-} 2 2 3 3 ----------------------------------------------------------------------------- 4 4 -- | 5 5 -- Module : Data.Map 6 6 -- Copyright : (c) Daan Leijen 2002 7 -- (c) Andriy Palamarchuk 2007 7 8 -- License : BSD-style 8 9 -- Maintainer : libraries@haskell.org 9 10 -- Stability : provisional … … 31 32 -- Note that the implementation is /left-biased/ -- the elements of a 32 33 -- first argument are always preferred to the second, for example in 33 34 -- 'union' or 'insert'. 35 -- 36 -- Operation comments contain the operation time complexity in 37 -- the Big-O notation <http://en.wikipedia.org/wiki/Big_O_notation>. 34 38 ----------------------------------------------------------------------------- 35 39 36 40 module Data.Map ( … … 194 198 --------------------------------------------------------------------} 195 199 infixl 9 !,\\ -- 196 200 197 -- | /O(log n)/. Find the value at a key. 198 -- Calls 'error' when the element can not be found. 201 -- | Find the value at a key. 202 -- Calls 'error' when the element can not be found. /O(log n)/. 203 -- 204 -- > let m = fromList [(5,'a'), (3,'b'), (7,'c')] 205 -- > m 206 -- > {3:='b',5:='a',7:='c'} 207 -- > m!1 208 -- > Error: element not in the map 209 -- > m!5 210 -- > 'a' 211 199 212 (!) :: Ord k => Map k a -> k -> a 200 213 m ! k = find k m 201 214 202 -- | /O(n+m)/. See'difference'.215 -- | Same as 'difference'. 203 216 (\\) :: Ord k => Map k a -> Map k b -> Map k a 204 217 m1 \\ m2 = difference m1 m2 205 218 … … 238 251 {-------------------------------------------------------------------- 239 252 Query 240 253 --------------------------------------------------------------------} 241 -- | /O(1)/. Is the map empty? 254 -- | Is the map empty? /O(1)/. 255 -- 256 -- > Data.Map.null (fromList([])) 257 -- > True 258 -- > Data.Map.null (fromList([('a',1)])) 259 -- > False 260 -- > Data.Map.null (empty) 261 -- > True 262 -- > Data.Map.null (singleton 'a' 1) 263 -- > False 264 242 265 null :: Map k a -> Bool 243 266 null t 244 267 = case t of 245 268 Tip -> True 246 269 Bin sz k x l r -> False 247 270 248 -- | /O(1)/. The number of elements in the map. 271 -- | The number of elements in the map. /O(1)/. 272 -- 273 -- > size empty 274 -- > 0 275 -- > size (singleton 'a' 1) 276 -- > 1 277 -- > size (fromList([('a',1), ('c', 2), ('b', 3)])) 278 -- > 3 279 249 280 size :: Map k a -> Int 250 281 size t 251 282 = case t of … … 253 284 Bin sz k x l r -> sz 254 285 255 286 256 -- | /O(log n)/. Lookup the value at a key in the map.287 -- | Lookup the value at a key in the map. 257 288 -- 258 289 -- The function will 259 290 -- @return@ the result in the monad or @fail@ in it the key isn't in the 260 291 -- map. Often, the monad to use is 'Maybe', so you get either 261 -- @('Just' result)@ or @'Nothing'@. 292 -- @('Just' result)@ or @'Nothing'@. /O(log n)/. 293 -- 294 -- > let m = fromList [(5,'a'), (3,'b'), (7,'c')] 295 -- > m 296 -- > {3:='b',5:='a',7:='c'} 297 -- > value1 <- Data.Map.lookup 5 m 298 -- > value1 299 -- > 'a' 300 -- > value2 <- Data.Map.lookup 1 m 301 -- > Error: Key not found 302 -- 303 -- An example of using @lookup@ with @Maybe@ monad: 304 -- 305 -- > import Prelude hiding (lookup) 306 -- > import Data.Map 307 -- > 308 -- > employeeDept = fromList([("John","Sales"), ("Bob","IT")]) 309 -- > deptCountry = fromList([("IT","USA"), ("Sales","France")]) 310 -- > countryCurrency = fromList([("USA", "Dollar"), ("France", "Euro")]) 311 -- > 312 -- > employeeCurrency :: String -> Maybe String 313 -- > employeeCurrency name = do 314 -- > dept <- lookup name employeeDept 315 -- > country <- lookup dept deptCountry 316 -- > lookup country countryCurrency 317 -- > 318 -- > main = do 319 -- > putStrLn $ "John's currency: " ++ (show (employeeCurrency "John")) 320 -- > putStrLn $ "Pete's currency: " ++ (show (employeeCurrency "Pete")) 321 -- 322 -- The output of this program: 323 -- 324 -- > John's currency: Just "Euro" 325 -- > Pete's currency: Nothing 326 262 327 lookup :: (Monad m,Ord k) => k -> Map k a -> m a 263 328 lookup k t = case lookup' k t of 264 329 Just x -> return x … … 283 348 GT -> lookupAssoc k r 284 349 EQ -> Just (kx,x) 285 350 286 -- | /O(log n)/. Is the key a member of the map? 351 -- | Is the key a member of the map? See also 'notMember'. /O(log n)/. 352 -- 353 -- > let m = fromList [(5,'a'), (3,'b'), (7,'c')] 354 -- > m 355 -- > {3:='b',5:='a',7:='c'} 356 -- > member 5 m 357 -- > True 358 -- > member 1 m 359 -- > False 360 287 361 member :: Ord k => k -> Map k a -> Bool 288 362 member k m 289 363 = case lookup k m of 290 364 Nothing -> False 291 365 Just x -> True 292 366 293 -- | /O(log n)/. Is the key not a member of the map? 367 -- | Is the key not a member of the map? See also 'member'. /O(log n)/. 368 -- 369 -- > let m = fromList [(5,'a'), (3,'b'), (7,'c')] 370 -- > m 371 -- > {3:='b',5:='a',7:='c'} 372 -- > notMember 5 m 373 -- > False 374 -- > notMember 1 m 375 -- > True 376 294 377 notMember :: Ord k => k -> Map k a -> Bool 295 378 notMember k m = not $ member k m 296 379 297 -- | /O(log n)/. Find the value at a key.380 -- | Find the value at a key. /O(log n)/. 298 381 -- Calls 'error' when the element can not be found. 299 382 find :: Ord k => k -> Map k a -> a 300 383 find k m … … 302 385 Nothing -> error "Map.find: element not in the map" 303 386 Just x -> x 304 387 305 -- | /O(log n)/. The expression @('findWithDefault' def k map)@ returns 306 -- the value at key @k@ or returns @def@ when the key is not in the map. 388 -- | The expression @('findWithDefault' def k map)@ returns 389 -- the value at key @k@ or returns default value @def@ 390 -- when the key is not in the map. 391 -- /O(log n)/. 392 -- 393 -- > let m = fromList [(5,'a'), (3,'b'), (7,'c')] 394 -- > m 395 -- > {3:='b',5:='a',7:='c'} 396 -- > findWithDefault '?' 1 m 397 -- > '?' 398 -- > findWithDefault '?' 5 m 399 -- > 'a' 400 307 401 findWithDefault :: Ord k => a -> k -> Map k a -> a 308 402 findWithDefault def k m 309 403 = case lookup k m of … … 315 409 {-------------------------------------------------------------------- 316 410 Construction 317 411 --------------------------------------------------------------------} 318 -- | /O(1)/. The empty map. 412 -- | The empty map. /O(1)/. 413 -- 414 -- > empty 415 -- > {} 416 -- > size empty 417 -- > 0 418 319 419 empty :: Map k a 320 420 empty 321 421 = Tip 322 422 323 -- | /O(1)/. A map with a single element. 423 -- | A map with a single element. /O(1)/. 424 -- 425 -- > singleton 1 'a' 426 -- > {1:='a'} 427 -- > size (singleton 1 'a') 428 -- > 1 429 324 430 singleton :: k -> a -> Map k a 325 431 singleton k x 326 432 = Bin 1 k x Tip Tip … … 328 434 {-------------------------------------------------------------------- 329 435 Insertion 330 436 --------------------------------------------------------------------} 331 -- | /O(log n)/.Insert a new key and value in the map.437 -- | Insert a new key and value in the map. 332 438 -- If the key is already present in the map, the associated value is 333 -- replaced with the supplied value, i.e. 'insert' is equivalent to 334 -- @'insertWith' 'const'@. 439 -- replaced with the supplied value. 'insert' is equivalent to 440 -- @'insertWith' 'const'@. /O(log n)/. 441 -- 442 -- > let m = fromList [(5,'a'), (3,'b')] 443 -- > m 444 -- > {3:='b',5:='a'} 445 -- > insert 5 'x' m 446 -- > {3:='b',5:='x'} 447 -- > insert 10 'x' m 448 -- > {3:='b',5:='a',10:='x'} 449 -- > insert 5 'x' empty 450 -- > {5:='x'} 451 335 452 insert :: Ord k => k -> a -> Map k a -> Map k a 336 453 insert kx x t 337 454 = case t of … … 342 459 GT -> balance ky y l (insert kx x r) 343 460 EQ -> Bin sz kx x l r 344 461 345 -- | /O(log n)/. Insert with a combining function.462 -- | Insert with a function, combining new value and old value. 346 463 -- @'insertWith' f key value mp@ 347 464 -- will insert the pair (key, value) into @mp@ if key does 348 465 -- not exist in the map. If the key does exist, the function will 349 -- insert the pair @(key, f new_value old_value)@. 466 -- insert the pair @(key, f new_value old_value)@. /O(log n)/. 467 -- 468 -- > let m = fromList [(5,"a"), (3,"b")] 469 -- > m 470 -- > {3:="b",5:="a"} 471 -- > insertWith (++) 5 "xxx" m 472 -- > {3:="b",5:="xxxa"} 473 -- > insertWith (++) 10 "xxx" m 474 -- > {3:="b",5:="a",10:="xxx"} 475 -- > insertWith (++) 5 "xxx" empty 476 -- > {5:="xxx"} 477 350 478 insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a 351 479 insertWith f k x m 352 480 = insertWithKey (\k x y -> f x y) k x m … … 357 485 = insertWithKey' (\k x y -> f x y) k x m 358 486 359 487 360 -- | /O(log n)/. Insert with a combining function.488 -- | Insert with a function, combining key, new value and old value. 361 489 -- @'insertWithKey' f key value mp@ 362 490 -- will insert the pair (key, value) into @mp@ if key does 363 491 -- not exist in the map. If the key does exist, the function will 364 492 -- insert the pair @(key,f key new_value old_value)@. 365 493 -- Note that the key passed to f is the same key passed to 'insertWithKey'. 494 -- /O(log n)/. 495 -- 496 -- > let m = fromList [(5,"a"), (3,"b")] 497 -- > m 498 -- > {3:="b",5:="a"} 499 -- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value 500 -- > insertWithKey f 5 "xxx" m 501 -- > {3:="b",5:="5:xxx|a"} 502 -- > insertWithKey f 10 "xxx" m 503 -- > {3:="b",5:="a",10:="xxx"} 504 -- > insertWithKey f 5 "xxx" empty 505 -- > {5:="xxx"} 506 366 507 insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a 367 508 insertWithKey f kx x t 368 509 = case t of … … 385 526 EQ -> let x' = f kx x y in seq x' (Bin sy kx x' l r) 386 527 387 528 388 -- | /O(log n)/. The expression (@'insertLookupWithKey' f k x map@) 529 -- | Combines insert operation with old value retrieval. 530 -- The expression (@'insertLookupWithKey' f k x map@) 389 531 -- is a pair where the first element is equal to (@'lookup' k map@) 390 -- and the second element equal to (@'insertWithKey' f k x map@). 532 -- and the second element equal to (@'insertWithKey' f k x map@). /O(log n)/. 533 -- 534 -- > let m = fromList [(5,"a"), (3,"b")] 535 -- > m 536 -- > {3:="b",5:="a"} 537 -- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value 538 -- > insertLookupWithKey f 5 "xxx" m 539 -- > (Just "a",{3:="b",5:="5:xxx|a"}) 540 -- > insertLookupWithKey f 10 "xxx" m 541 -- > (Nothing,{3:="b",5:="a",10:="xxx"}) 542 -- > insertLookupWithKey f 5 "xxx" empty 543 -- > (Nothing,{5:="xxx"}) 544 -- 545 -- This is how to define @insertLookup@ using @insertLookupWithKey@: 546 -- 547 -- > let m = fromList [(5,"a"), (3,"b")] 548 -- > m 549 -- > {3:="b",5:="a"} 550 -- > let insertLookup kx x t = insertLookupWithKey (\_ a _ -> a) kx x t 551 -- > insertLookup 5 "x" m 552 -- > (Just "a",{3:="b",5:="x"}) 553 -- > insertLookup 10 "x" m 554 -- > (Nothing,{3:="b",5:="a",10:="x"}) 555 391 556 insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a,Map k a) 392 557 insertLookupWithKey f kx x t 393 558 = case t of … … 402 567 Deletion 403 568 [delete] is the inlined version of [deleteWith (\k x -> Nothing)] 404 569 --------------------------------------------------------------------} 405 -- | /O(log n)/. Delete a key and its value from the map. When the key is not 406 -- a member of the map, the original map is returned. 570 -- | Delete a key and its value from the map. When the key is not 571 -- a member of the map, the original map is returned. /O(log n)/. 572 -- 573 -- > let m = fromList [(5,"a"), (3,"b")] 574 -- > m 575 -- > {3:="b",5:="a"} 576 -- > delete 5 m 577 -- > {3:="b"} 578 -- > delete 10 m 579 -- > {3:="b",5:="a"} 580 -- > delete 5 empty 581 -- > {} 582 407 583 delete :: Ord k => k -> Map k a -> Map k a 408 584 delete k t 409 585 = case t of … … 414 590 GT -> balance kx x l (delete k r) 415 591 EQ -> glue l r 416 592 417 -- | /O(log n)/. Adjust a value at a specific key. When the key is not 418 -- a member of the map, the original map is returned. 593 -- | Update a value at a specific key with the result of the provided function. 594 -- When the key is not 595 -- a member of the map, the original map is returned. /O(log n)/. 596 -- 597 -- > let m = fromList [(5,"a"), (3,"b")] 598 -- > m 599 -- > {3:="b",5:="a"} 600 -- > adjust ("new " ++) 5 m 601 -- > {3:="b",5:="new a"} 602 -- > adjust ("new " ++) 10 m 603 -- > {3:="b",5:="a"} 604 -- > adjust ("new " ++) 10 empty 605 -- > {} 606 419 607 adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a 420 608 adjust f k m 421 609 = adjustWithKey (\k x -> f x) k m 422 610 423 -- | /O(log n)/. Adjust a value at a specific key. When the key is not 424 -- a member of the map, the original map is returned. 611 -- | Adjust a value at a specific key. When the key is not 612 -- a member of the map, the original map is returned. /O(log n)/. 613 -- 614 -- > let m = fromList [(5,"a"), (3,"b")] 615 -- > m 616 -- > {3:="b",5:="a"} 617 -- > let f key x = (show key) ++ ":new " ++ x 618 -- > adjustWithKey f 5 m 619 -- > {3:="b",5:="5:new a"} 620 -- > adjustWithKey f 10 m 621 -- > {3:="b",5:="a"} 622 -- > adjustWithKey f 10 empty 623 -- > {} 624 425 625 adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a 426 626 adjustWithKey f k m 427 627 = updateWithKey (\k x -> Just (f k x)) k m 428 628 429 -- | /O(log n)/.The expression (@'update' f k map@) updates the value @x@629 -- | The expression (@'update' f k map@) updates the value @x@ 430 630 -- at @k@ (if it is in the map). If (@f x@) is 'Nothing', the element is 431 631 -- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@. 632 -- /O(log n)/. 633 -- 634 -- > let m = fromList [(5,"a"), (3,"b")] 635 -- > m 636 -- > {3:="b",5:="a"} 637 -- > let f x = if x == "a" then Just "new a" else Nothing 638 -- > 639 -- > -- f "a" returns Just "new a" 640 -- > update f 5 m 641 -- > {3:="b",5:="new a"} 642 -- > 643 -- > -- key does not exist 644 -- > update f 10 m 645 -- > {3:="b",5:="a"} 646 -- > 647 -- > -- f "b" returns Nothing 648 -- > update f 3 m 649 -- > {5:="a"} 650 432 651 update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a 433 652 update f k m 434 653 = updateWithKey (\k x -> f x) k m 435 654 436 -- | /O(log n)/.The expression (@'updateWithKey' f k map@) updates the655 -- | The expression (@'updateWithKey' f k map@) updates the 437 656 -- value @x@ at @k@ (if it is in the map). If (@f k x@) is 'Nothing', 438 657 -- the element is deleted. If it is (@'Just' y@), the key @k@ is bound 439 -- to the new value @y@. 658 -- to the new value @y@. /O(log n)/. 659 -- 660 -- > let m = fromList [(5,"a"), (3,"b")] 661 -- > m 662 -- > {3:="b",5:="a"} 663 -- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing 664 -- > 665 -- > -- f 5 "a" returns Just "5:new a" 666 -- > updateWithKey f 5 m 667 -- > {3:="b",5:="5:new a"} 668 -- > 669 -- > -- key does not exist 670 -- > updateWithKey f 10 m 671 -- > {3:="b",5:="a"} 672 -- > 673 -- > -- f 3 "b" returns Nothing 674 -- > updateWithKey f 3 m 675 -- > {5:="a"} 676 440 677 updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a 441 678 updateWithKey f k t 442 679 = case t of … … 449 686 Just x' -> Bin sx kx x' l r 450 687 Nothing -> glue l r 451 688 452 -- | /O(log n)/. Lookup and update. 689 -- | Lookup and update. See also 'updateWithKey'. /O(log n)/. 690 -- 691 -- > let m = fromList [(5,"a"), (3,"b")] 692 -- > m 693 -- > {3:="b",5:="a"} 694 -- > let f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing 695 -- > 696 -- > -- f 5 "a" returns Just "5:new a" 697 -- > updateLookupWithKey f 5 m 698 -- > (Just "5:new a",{3:="b",5:="5:new a"}) 699 -- > 700 -- > -- key does not exist 701 -- > updateLookupWithKey f 10 m 702 -- > (Nothing,{3:="b",5:="a"}) 703 -- > 704 -- > -- f 3 "b" returns Nothing 705 -- > updateLookupWithKey f 3 m 706 -- > (Just "b",{5:="a"}) 707 453 708 updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a,Map k a) 454 709 updateLookupWithKey f k t 455 710 = case t of … … 462 717 Just x' -> (Just x',Bin sx kx x' l r) 463 718 Nothing -> (Just x,glue l r) 464 719 465 -- | /O(log n)/.The expression (@'alter' f k map@) alters the value @x@ at @k@, or absence thereof.720 -- | The expression (@'alter' f k map@) alters the value @x@ at @k@, or absence thereof. 466 721 -- 'alter' can be used to insert, delete, or update a value in a 'Map'. 467 -- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@ 722 -- In short : @'lookup' k ('alter' f k m) = f ('lookup' k m)@. /O(log n)/. 723 -- 724 -- > let m = fromList [(5,"a"), (3,"b")] 725 -- > m 726 -- > {3:="b",5:="a"} 727 -- > 728 -- > -- always returns Nothing 729 -- > let f _ = Nothing 730 -- > alter f 10 m 731 -- > {3:="b",5:="a"} 732 -- > alter f 5 m 733 -- > {3:="b"} 734 -- > 735 -- > -- always returns Just "c" 736 -- > let f _ = Just "c" 737 -- > alter f 10 m 738 -- > {3:="b",5:="a",10:="c")} 739 -- > alter f 5 m 740 -- > {3:="b",5:="c")} 741 468 742 alter :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a 469 743 alter f k t 470 744 = case t of … … 482 756 {-------------------------------------------------------------------- 483 757 Indexing 484 758 --------------------------------------------------------------------} 485 -- | /O(log n)/.Return the /index/ of a key. The index is a number from759 -- | Return the /index/ of a key. The index is a number from 486 760 -- /0/ up to, but not including, the 'size' of the map. Calls 'error' when 487 -- the key is not a 'member' of the map. 761 -- the key is not a 'member' of the map. /O(log n)/. 762 -- 763 -- > let m = fromList [(5,"a"), (3,"b")] 764 -- > m 765 -- > {3:="b",5:="a"} 766 -- > findIndex 2 m 767 -- > Exception: Key not found. 768 -- > findIndex 3 m 769 -- > 0 770 -- > findIndex 4 m 771 -- > Exception: Key not found. 772 -- > findIndex 5 m 773 -- > 1 774 -- > findIndex 6 m 775 -- > Exception: Key not found. 776 488 777 findIndex :: Ord k => k -> Map k a -> Int 489 778 findIndex k t 490 779 = case lookupIndex k t of 491 780 Nothing -> error "Map.findIndex: element is not in the map" 492 781 Just idx -> idx 493 782 494 -- | /O(log n)/. Lookup the /index/ of a key. The index is a number from 495 -- /0/ up to, but not including, the 'size' of the map. 783 -- | Lookup the /index/ of a key. The index is a number from 784 -- /0/ up to, but not including, the 'size' of the map. /O(log n)/. 785 -- 786 -- > let m = fromList [(5,"a"), (3,"b")] 787 -- > m 788 -- > {3:="b",5:="a"} 789 -- > isJust (lookupIndex 2 m) 790 -- > False 791 -- > isJust (lookupIndex 3 m) 792 -- > True 793 -- > fromJust (lookupIndex 3 m) 794 -- > 0 795 -- > isJust (lookupIndex 4 m) 796 -- > False 797 -- > isJust (lookupIndex 5 m) 798 -- > True 799 -- > fromJust (lookupIndex 5 m) 800 -- > 1 801 -- > isJust (lookupIndex 6 m) 802 -- > False 803 496 804 lookupIndex :: (Monad m,Ord k) => k -> Map k a -> m Int 497 805 lookupIndex k t = case lookup 0 t of 498 806 Nothing -> fail "Data.Map.lookupIndex: Key not found." … … 505 813 GT -> lookup (idx + size l + 1) r 506 814 EQ -> Just (idx + size l) 507 815 508 -- | /O(log n)/. Retrieve an element by /index/. Calls 'error' when an 509 -- invalid index is used. 816 -- | Retrieve an element by /index/. Calls 'error' when an 817 -- invalid index is used. /O(log n)/. 818 -- 819 -- > let m = fromList [(5,"a"), (3,"b")] 820 -- > m 821 -- > {3:="b",5:="a"} 822 -- > elemAt 0 m 823 -- > (3,"b") 824 -- > elemAt 1 m 825 -- > (5,"a") 826 -- > elemAt 2 m 827 -- > Exception: Map.elemAt: index out of range 828 510 829 elemAt :: Int -> Map k a -> (k,a) 511 830 elemAt i Tip = error "Map.elemAt: index out of range" 512 831 elemAt i (Bin _ kx x l r) … … 517 836 where 518 837 sizeL = size l 519 838 520 -- | /O(log n)/. Update the element at /index/. Calls 'error' when an 521 -- invalid index is used. 839 -- | Update the element at /index/. Calls 'error' when an 840 -- invalid index is used. /O(log n)/. 841 -- 842 -- > let m = fromList [(5,"a"), (3,"b")] 843 -- > m 844 -- > {3:="b",5:="a"} 845 -- > 846 -- > -- always returns Just "x" 847 -- > let f _ _ = Just "x" 848 -- > updateAt f 0 m 849 -- > {3:="x",5:="a"} 850 -- > updateAt f 1 m 851 -- > {3:="b",5:="x"} 852 -- > updateAt f 2 m 853 -- > Exception: Map.updateAt: index out of range 854 -- > updateAt f (-1) m 855 -- > Exception: Map.updateAt: index out of range 856 -- > 857 -- > -- always returns Nothing 858 -- > let f _ _ = Nothing 859 -- > updateAt f 0 m 860 -- > {5:="a"} 861 -- > updateAt f 1 m 862 -- > {3:="b"} 863 -- > updateAt f 2 m 864 -- > Exception: Map.updateAt: index out of range 865 -- > updateAt f (-1) m 866 -- > Exception: Map.updateAt: index out of range 867 522 868 updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a 523 869 updateAt f i Tip = error "Map.updateAt: index out of range" 524 870 updateAt f i (Bin sx kx x l r) … … 531 877 where 532 878 sizeL = size l 533 879 534 -- | /O(log n)/.Delete the element at /index/.880 -- | Delete the element at /index/. 535 881 -- Defined as (@'deleteAt' i map = 'updateAt' (\k x -> 'Nothing') i map@). 882 -- /O(log n)/. 883 -- 884 -- > let m = fromList [(5,"a"), (3,"b")] 885 -- > m 886 -- > {3:="b",5:="a"} 887 -- > deleteAt 0 m 888 -- > {5:="a"} 889 -- > deleteAt 1 m 890 -- > {3:="b"} 891 -- > deleteAt 2 m 892 -- > Exception: Map.updateAt: index out of range 893 -- > deleteAt (-1) m 894 -- > Exception: Map.updateAt: index out of range 895 536 896 deleteAt :: Int -> Map k a -> Map k a 537 897 deleteAt i map 538 898 = updateAt (\k x -> Nothing) i map … … 541 901 {-------------------------------------------------------------------- 542 902 Minimal, Maximal 543 903 --------------------------------------------------------------------} 544 -- | /O(log n)/. The minimal key of the map. 904 -- | The minimal key of the map. /O(log n)/. 905 -- 906 -- > let m = fromList [(5,"a"), (3,"b")] 907 -- > m 908 -- > {3:="b",5:="a"} 909 -- > findMin m 910 -- > (3,"b") 911 -- > findMin empty 912 -- > Exception: Map.findMin: empty map has no minimal element 913 545 914 findMin :: Map k a -> (k,a) 546 915 findMin (Bin _ kx x Tip r) = (kx,x) 547 916 findMin (Bin _ kx x l r) = findMin l 548 917 findMin Tip = error "Map.findMin: empty map has no minimal element" 549 918 550 -- | /O(log n)/. The maximal key of the map. 919 -- | The maximal key of the map. /O(log n)/. 920 -- 921 -- > let m = fromList [(5,"a"), (3,"b")] 922 -- > m 923 -- > {3:="b",5:="a"} 924 -- > findMax m 925 -- > (5,"a") 926 -- > findMax empty 927 -- > Exception: Map.findMax: empty map has no maximal element 928 551 929 findMax :: Map k a -> (k,a) 552 930 findMax (Bin _ kx x l Tip) = (kx,x) 553 931 findMax (Bin _ kx x l r) = findMax r 554 932 findMax Tip = error "Map.findMax: empty map has no maximal element" 555 933 556 -- | /O(log n)/. Delete the minimal key. 934 -- | Delete the minimal key. /O(log n)/. 935 -- 936 -- > let m = fromList [(5,"a"), (3,"b"), (10,"c")] 937 -- > m 938 -- > {3:="b",5:="a",10:="c"} 939 -- > deleteMin m 940 -- > {5:="a",10:="c"} 941 -- > deleteMin empty 942 -- > {} 943 557 944 deleteMin :: Map k a -> Map k a 558 945 deleteMin (Bin _ kx x Tip r) = r 559 946 deleteMin (Bin _ kx x l r) = balance kx x (deleteMin l) r 560 947 deleteMin Tip = Tip 561 948 562 -- | /O(log n)/. Delete the maximal key. 949 -- | Delete the maximal key. /O(log n)/. 950 -- 951 -- > let m = fromList [(5,"a"), (3,"b"), (10,"c")] 952 -- > m 953 -- > {3:="b",5:="a",10:="c"} 954 -- > deleteMax m 955 -- > {3:="b",5:="a"} 956 -- > deleteMax empty 957 -- > {} 958 563 959 deleteMax :: Map k a -> Map k a 564 960 deleteMax (Bin _ kx x l Tip) = l 565 961 deleteMax (Bin _ kx x l r) = balance kx x l (deleteMax r) 566 962 deleteMax Tip = Tip 567 963 568 -- | /O(log n)/. Update the value at the minimal key. 964 -- | Update the value at the minimal key. /O(log n)/. 965 -- 966 -- > let m = fromList [(5,"a"), (3,"b"), (10,"c")] 967 -- > m 968 -- > {3:="b",5:="a",10:="c"} 969 -- > updateMin (\ a -> Just ("X" ++ a)) m 970 -- > {3:="Xb",5:="a",10:="c"} 971 -- > updateMin (\ _ -> Nothing) m 972 -- > {5:="a",10:="c"} 973 569 974 updateMin :: (a -> Maybe a) -> Map k a -> Map k a 570 975 updateMin f m 571 976 = updateMinWithKey (\k x -> f x) m 572 977 573 -- | /O(log n)/. Update the value at the maximal key. 978 -- | Update the value at the maximal key. /O(log n)/. 979 -- 980 -- > let m = fromList [(5,"a"), (3,"b"), (10,"c")] 981 -- > m 982 -- > {3:="b",5:="a",10:="c"} 983 -- > updateMax (\ a -> Just ("X" ++ a)) m 984 -- > {3:="b",5:="a",10:="Xc"} 985 -- > updateMax (\ _ -> Nothing) m 986 -- > {3:="b",5,"a"} 987 574 988 updateMax :: (a -> Maybe a) -> Map k a -> Map k a 575 989 updateMax f m 576 990 = updateMaxWithKey (\k x -> f x) m 577 991 578 992 579 -- | /O(log n)/. Update the value at the minimal key. 993 -- | Update the value at the minimal key. /O(log n)/. 994 -- 995 -- > let m = fromList [(5,"a"), (3,"b"), (10,"c")] 996 -- > m 997 -- > {3:="b",5:="a",10:="c"} 998 -- > updateMinWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) m 999 -- > {3:="3:b",5:="a",10:="c"} 1000 -- > updateMinWithKey (\ _ _ -> Nothing) m 1001 -- > {5:="a",10:="c"} 1002 580 1003 updateMinWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a 581 1004 updateMinWithKey f t 582 1005 = case t of … … 586 1009 Bin sx kx x l r -> balance kx x (updateMinWithKey f l) r 587 1010 Tip -> Tip 588 1011 589 -- | /O(log n)/. Update the value at the maximal key. 1012 -- | Update the value at the maximal key. /O(log n)/. 1013 -- 1014 -- > let m = fromList [(5,"a"), (3,"b"), (10,"c")] 1015 -- > m 1016 -- > {3:="b",5:="a",10:="c"} 1017 -- > updateMaxWithKey (\ k a -> Just ((show k) ++ ":" ++ a)) m 1018 -- > {3:="b",5:="a",10:="10:c"} 1019 -- > updateMaxWithKey (\ _ _ -> Nothing) m 1020 -- > {3:="b",5:="a"} 1021 590 1022 updateMaxWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a 591 1023 updateMaxWithKey f t 592 1024 = case t of … … 596 1028 Bin sx kx x l r -> balance kx x l (updateMaxWithKey f r) 597 1029 Tip -> Tip 598 1030 599 -- | /O(log n)/.Retrieves the minimal (key,value) pair of the map, and the map stripped from that element1031 -- | Retrieves the minimal (key,value) pair of the map, and the map stripped from that element 600 1032 -- @fail@s (in the monad) when passed an empty map. 1033 -- /O(log n)/. 1034 -- 1035 -- > let m = fromList [(5,"a"), (3,"b")] 1036 -- > m 1037 -- > {3:="b",5:="a"} 1038 -- > minViewWithKey m 1039 -- > ((3,"b"), {5:="a"}) 1040 -- > minViewWithKey empty 1041 -- > Exception: user error (Map.minViewWithKey: empty map) 1042 601 1043 minViewWithKey :: Monad m => Map k a -> m ((k,a), Map k a) 602 minViewWithKey Tip = fail "Map.minView : empty map"1044 minViewWithKey Tip = fail "Map.minViewWithKey: empty map" 603 1045 minViewWithKey x = return (deleteFindMin x) 604 1046 605 -- | /O(log n)/. Retrieves the maximal (key,value) pair of the map, and the map stripped from that element 606 -- @fail@s (in the monad) when passed an empty map. 1047 -- | Retrieves the maximal (key,value) pair of the map, and the map stripped from that element 1048 -- @fail@s (in the monad) when passed an empty map. /O(log n)/. 1049 -- 1050 -- > let m = fromList [(5,"a"), (3,"b")] 1051 -- > m 1052 -- > {3:="b",5:="a"} 1053 -- > maxViewWithKey m 1054 -- > ((5,"a"), {3:="b"}) 1055 -- > maxViewWithKey empty 1056 -- > Exception: user error (Map.maxViewWithKey: empty map) 1057 607 1058 maxViewWithKey :: Monad m => Map k a -> m ((k,a), Map k a) 608 maxViewWithKey Tip = fail "Map.maxView : empty map"1059 maxViewWithKey Tip = fail "Map.maxViewWithKey: empty map" 609 1060 maxViewWithKey x = return (deleteFindMax x) 610 1061 611 -- | /O(log n)/. Retrieves the minimal key\'s value of the map, and the map stripped from that element 612 -- @fail@s (in the monad) when passed an empty map. 1062 -- | Retrieves the minimal key\'s value of the map, and the map stripped from that element 1063 -- @fail@s (in the monad) when passed an empty map. /O(log n)/. 1064 -- 1065 -- > let m = fromList [(5,"a"), (3,"b")] 1066 -- > m 1067 -- > {3:="b",5:="a"} 1068 -- > minView m 1069 -- > ("b", {5:="a"}) 1070 -- > minView empty 1071 -- > Exception: user error (Map.minView: empty map) 1072 613 1073 minView :: Monad m => Map k a -> m (a, Map k a) 614 1074 minView Tip = fail "Map.minView: empty map" 615 1075 minView x = return (first snd $ deleteFindMin x) 616 1076 617 -- | /O(log n)/. Retrieves the maximal key\'s value of the map, and the map stripped from that element 618 -- @fail@s (in the monad) when passed an empty map. 1077 -- | Retrieves the maximal key\'s value of the map, and the map stripped from that element 1078 -- @fail@s (in the monad) when passed an empty map. /O(log n)/. 1079 -- 1080 -- > let m = fromList [(5,"a"), (3,"b")] 1081 -- > m 1082 -- > {3:="b",5:="a"} 1083 -- > maxView m 1084 -- > ("a", {3:="b"}) 1085 -- > maxView empty 1086 -- > Exception: user error (Map.maxView: empty map) 1087 619 1088 maxView :: Monad m => Map k a -> m (a, Map k a) 620 1089 maxView Tip = fail "Map.maxView: empty map" 621 1090 maxView x = return (first snd $ deleteFindMax x) … … 629 1098 --------------------------------------------------------------------} 630 1099 -- | The union of a list of maps: 631 1100 -- (@'unions' == 'Prelude.foldl' 'union' 'empty'@). 1101 -- 1102 -- > let map1 = fromList [(5, "a"), (3, "b")] 1103 -- > let map2 = fromList [(5, "A"), (10, "C")] 1104 -- > let map3 = fromList [(5, "A3"), (3, "B3")] 1105 -- > unions [map1, map2, map3] 1106 -- > {3:="b",5:="a",10:="C"} 1107 -- > unions [map3, map2, map1] 1108 -- > {3:="B3",5:="A3",10:="C"} 1109 632 1110 unions :: Ord k => [Map k a] -> Map k a 633 1111 unions ts 634 1112 = foldlStrict union empty ts 635 1113 636 1114 -- | The union of a list of maps, with a combining operation: 637 1115 -- (@'unionsWith' f == 'Prelude.foldl' ('unionWith' f) 'empty'@). 1116 -- 1117 -- > let map1 = fromList [(5, "a"), (3, "b")] 1118 -- > let map2 = fromList [(5, "A"), (10, "C")] 1119 -- > let map3 = fromList [(5, "A3"), (3, "B3")] 1120 -- > unionsWith (++) [map1, map2, map3] 1121 -- > {3:="bB3",5:="aAA3",10:="C"} 1122 638 1123 unionsWith :: Ord k => (a->a->a) -> [Map k a] -> Map k a 639 1124 unionsWith f ts 640 1125 = foldlStrict (unionWith f) empty ts 641 1126 642 -- | /O(n+m)/. 643 -- The expression (@'union' t1 t2@) takes the left-biased union of @t1@ and @t2@. 1127 -- | The expression (@'union' t1 t2@) takes the left-biased union of @t1@ and @t2@. 644 1128 -- It prefers @t1@ when duplicate keys are encountered, 645 1129 -- i.e. (@'union' == 'unionWith' 'const'@). 646 1130 -- The implementation uses the efficient /hedge-union/ algorithm. 647 -- Hedge-union is more efficient on (bigset `union` smallset) 1131 -- Hedge-union is more efficient on (bigset \``union`\` smallset). /O(n+m)/. 1132 -- 1133 -- > let map1 = fromList [(5, "a"), (3, "b")] 1134 -- > let map2 = fromList [(5, "A"), (10, "C")] 1135 -- > union map1 map2 1136 -- > {3:="b",5:="a",10:="C"} 1137 648 1138 union :: Ord k => Map k a -> Map k a -> Map k a 649 1139 union Tip t2 = t2 650 1140 union t1 Tip = t1 … … 680 1170 {-------------------------------------------------------------------- 681 1171 Union with a combining function 682 1172 --------------------------------------------------------------------} 683 -- | /O(n+m)/. Union with a combining function. The implementation uses the efficient /hedge-union/ algorithm. 1173 -- | Union with a combining function. The implementation uses the efficient /hedge-union/ algorithm. 1174 -- /O(n+m)/. 1175 -- 1176 -- > let map1 = fromList [(5, "a"), (3, "b")] 1177 -- > let map2 = fromList [(5, "A"), (10, "C")] 1178 -- > unionWith (++) map1 map2 1179 -- > {3:="b",5:="aA",10:="C"} 1180 684 1181 unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a 685 1182 unionWith f m1 m2 686 1183 = unionWithKey (\k x y -> f x y) m1 m2 687 1184 688 -- | /O(n+m)/. 689 -- Union with a combining function. The implementation uses the efficient /hedge-union/ algorithm. 690 -- Hedge-union is more efficient on (bigset `union` smallset). 1185 -- | Union with a combining function. The implementation uses the efficient /hedge-union/ algorithm. 1186 -- Hedge-union is more efficient on (bigset \``union`\` smallset). 1187 -- /O(n+m)/. 1188 -- 1189 -- > let map1 = fromList [(5, "a"), (3, "b")] 1190 -- > let map2 = fromList [(5, "A"), (10, "C")] 1191 -- > let f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value 1192 -- > unionWithKey f map1 map2 1193 -- > {3:="b",5:="5:a|A",10:="C"} 1194 691 1195 unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a 692 1196 unionWithKey f Tip t2 = t2 693 1197 unionWithKey f t1 Tip = t1 … … 711 1215 {-------------------------------------------------------------------- 712 1216 Difference 713 1217 --------------------------------------------------------------------} 714 -- | /O(n+m)/. Difference of two maps. 1218 -- | Difference of two maps. 1219 -- Return elements of the first map not existing in the second map. 715 1220 -- The implementation uses an efficient /hedge/ algorithm comparable with /hedge-union/. 1221 -- /O(n+m)/. 1222 -- 1223 -- > let map1 = fromList [(5, "a"), (3, "b")] 1224 -- > let map2 = fromList [(5, "A"), (10, "C")] 1225 -- > difference map1 map2 1226 -- > {3:="b"} 1227 716 1228 difference :: Ord k => Map k a -> Map k b -> Map k a 717 1229 difference Tip t2 = Tip 718 1230 difference t1 Tip = t1 … … 728 1240 where 729 1241 cmpkx k = compare kx k 730 1242 731 -- | /O(n+m)/. Difference with a combining function. 1243 -- | Difference with a combining function. 1244 -- When two equal keys are 1245 -- encountered, the combining function is applied to the values of these keys. 1246 -- If it returns 'Nothing', the element is discarded (proper set difference). If 1247 -- it returns (@'Just' y@), the element is updated with a new value @y@. 732 1248 -- The implementation uses an efficient /hedge/ algorithm comparable with /hedge-union/. 1249 -- /O(n+m)/. 1250 -- 1251 -- > -- 3 and 5 are common keys 1252 -- > let map1 = fromList [(5, "a"), (3, "b")] 1253 -- > let map2 = fromList [(5, "A"), (3, "B"), (10, "C")] 1254 -- > map1 1255 -- > {3:="b",5:="a"} 1256 -- > map2 1257 -- > {3:="B",5:="A",10:="C"} 1258 -- > let f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing 1259 -- > 1260 -- > -- f returns Nothing for 5, Just "b:B" for 3 1261 -- > differenceWith f map1 map2 1262 -- > {3:="b:B"} 1263 733 1264 differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a 734 1265 differenceWith f m1 m2 735 1266 = differenceWithKey (\k x y -> f x y) m1 m2 736 1267 737 -- | /O(n+m)/.Difference with a combining function. When two equal keys are1268 -- | Difference with a combining function. When two equal keys are 738 1269 -- encountered, the combining function is applied to the key and both values. 739 1270 -- If it returns 'Nothing', the element is discarded (proper set difference). If 740 1271 -- it returns (@'Just' y@), the element is updated with a new value @y@. 741 1272 -- The implementation uses an efficient /hedge/ algorithm comparable with /hedge-union/. 1273 -- /O(n+m)/. 1274 -- 1275 -- > -- 3 and 5 are common keys 1276 -- > let map1 = fromList [(5, "a"), (3, "b")] 1277 -- > let map2 = fromList [(5, "A"), (3, "B"), (10, "C")] 1278 -- > map1 1279 -- > {3:="b",5:="a"} 1280 -- > map2 1281 -- > {3:="B",5:="A",10:="C"} 1282 -- > let f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing 1283 -- > 1284 -- > -- f returns Nothing for 5, Just "3:b|B" for 3 1285 -- > differenceWithKey f map1 map2 1286 -- > {3:="3:b|B"} 1287 742 1288 differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a 743 1289 differenceWithKey f Tip t2 = Tip 744 1290 differenceWithKey f t1 Tip = t1 … … 767 1313 {-------------------------------------------------------------------- 768 1314 Intersection 769 1315 --------------------------------------------------------------------} 770 -- | /O(n+m)/. Intersection of two maps. The values in the first 771 -- map are returned, i.e. (@'intersection' m1 m2 == 'intersectionWith' 'const' m1 m2@). 1316 -- | Intersection of two maps. 1317 -- Return data in the first map for the keys existing in both maps. 1318 -- (@'intersection' m1 m2 == 'intersectionWith' 'const' m1 m2@). 1319 -- /O(n+m)/. 1320 -- 1321 -- > let map1 = fromList [(5, "a"), (3, "b")] 1322 -- > let map2 = fromList [(5, "A"), (10, "C")] 1323 -- > intersection map1 map2 1324 -- > {5:="a"} 1325 772 1326 intersection :: Ord k => Map k a -> Map k b -> Map k a 773 1327 intersection m1 m2 774 1328 = intersectionWithKey (\k x y -> x) m1 m2 775 1329 776 -- | /O(n+m)/. Intersection with a combining function. 1330 -- | Intersection with a combining function. 1331 -- /O(n+m)/. 1332 -- 1333 -- > let map1 = fromList [(5, "a"), (3, "b")] 1334 -- > let map2 = fromList [(5, "A"), (10, "C")] 1335 -- > intersectionWith (++) map1 map2 1336 -- > {5:="aA"} 1337 777 1338 intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c 778 1339 intersectionWith f m1 m2 779 1340 = intersectionWithKey (\k x y -> f x y) m1 m2 780 1341 781 -- | /O(n+m)/. Intersection with a combining function. 782 -- Intersection is more efficient on (bigset `intersection` smallset) 1342 -- | Intersection with a combining function. 1343 -- Intersection is more efficient on (bigset \``intersection`\` smallset). /O(n+m)/. 1344 -- 1345 -- > let map1 = fromList [(5, "a"), (3, "b")] 1346 -- > let map2 = fromList [(5, "A"), (10, "C")] 1347 -- > let f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar 1348 -- > intersectionWithKey f map1 map2 1349 -- > {5:="5:a|A"} 1350 783 1351 --intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c 784 1352 --intersectionWithKey f Tip t = Tip 785 1353 --intersectionWithKey f t Tip = Tip … … 796 1364 -- tl = intersectWithKey f lt l 797 1365 -- tr = intersectWithKey f gt r 798 1366 799 800 1367 intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c 801 1368 intersectionWithKey f Tip t = Tip 802 1369 intersectionWithKey f t Tip = Tip … … 820 1387 {-------------------------------------------------------------------- 821 1388 Submap 822 1389 --------------------------------------------------------------------} 823 -- | /O(n+m)/.824 -- This function is defined as (@'isSubmapOf' = 'isSubmapOfBy' (==)@).1390 -- | This function is defined as (@'isSubmapOf' = 'isSubmapOfBy' (==)@). 1391 -- /O(n+m)/. 825 1392 isSubmapOf :: (Ord k,Eq a) => Map k a -> Map k a -> Bool 826 1393 isSubmapOf m1 m2 827 1394 = isSubmapOfBy (==) m1 m2 828 1395 829 {- | /O(n+m)/. 830 The expression (@'isSubmapOfBy' f t1 t2@) returns 'True' if 1396 {- | The expression (@'isSubmapOfBy' f t1 t2@) returns 'True' if 831 1397 all keys in @t1@ are in tree @t2@, and when @f@ returns 'True' when 832 1398 applied to their respective values. For example, the following 833 1399 expressions are all 'True': … … 841 1407 > isSubmapOfBy (==) (fromList [('a',2)]) (fromList [('a',1),('b',2)]) 842 1408 > isSubmapOfBy (<) (fromList [('a',1)]) (fromList [('a',1),('b',2)]) 843 1409 > isSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1)]) 1410 1411 /O(n+m)/. 844 1412 -} 845 1413 isSubmapOfBy :: Ord k => (a->b->Bool) -> Map k a -> Map k b -> Bool 846 1414 isSubmapOfBy f t1 t2 … … 855 1423 where 856 1424 (lt,found,gt) = splitLookup kx t 857 1425 858 -- | /O(n+m)/.Is this a proper submap? (ie. a submap but not equal).859 -- Defined as (@'isProperSubmapOf' = 'isProperSubmapOfBy' (==)@). 1426 -- | Is this a proper submap? (ie. a submap but not equal). 1427 -- Defined as (@'isProperSubmapOf' = 'isProperSubmapOfBy' (==)@). /O(n+m)/. 860 1428 isProperSubmapOf :: (Ord k,Eq a) => Map k a -> Map k a -> Bool 861 1429 isProperSubmapOf m1 m2 862 1430 = isProperSubmapOfBy (==) m1 m2 863 1431 864 {- | /O(n+m)/.Is this a proper submap? (ie. a submap but not equal).1432 {- | Is this a proper submap? (ie. a submap but not equal). 865 1433 The expression (@'isProperSubmapOfBy' f m1 m2@) returns 'True' when 866 1434 @m1@ and @m2@ are not equal, 867 1435 all keys in @m1@ are in @m2@, and when @f@ returns 'True' when … … 876 1444 > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)]) 877 1445 > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)]) 878 1446 > isProperSubmapOfBy (<) (fromList [(1,1)]) (fromList [(1,1),(2,2)]) 1447 1448 /O(n+m)/. 879 1449 -} 880 1450 isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool 881 1451 isProperSubmapOfBy f t1 t2 … … 884 1454 {-------------------------------------------------------------------- 885 1455 Filter and partition 886 1456 --------------------------------------------------------------------} 887 -- | /O(n)/. Filter all values that satisfy the predicate. 1457 -- | Filter all values that satisfy the predicate. /O(n)/. 1458 -- 1459 -- > let m = fromList [(5,"a"), (3,"b")] 1460 -- > m 1461 -- > {3:="b",5:="a"} 1462 -- > filter (> "a") m 1463 -- > {3:="b"} 1464 -- > filter (> "x") m 1465 -- > {} 1466 -- > filter (< "a") m 1467 -- > {} 1468 888 1469 filter :: Ord k => (a -> Bool) -> Map k a -> Map k a 889 1470 filter p m 890 1471 = filterWithKey (\k x -> p x) m 891 1472 892 -- | /O(n)/. Filter all keys\/values that satisfy the predicate. 1473 -- | Filter all keys\/values that satisfy the predicate. /O(n)/. 1474 -- 1475 -- > let m = fromList [(5,"a"), (3,"b")] 1476 -- > m 1477 -- > {3:="b",5:="a"} 1478 -- > let f k a = k > 4 1479 -- > filterWithKey f m 1480 -- > {5:="a"} 1481 893 1482 filterWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> Map k a 894 1483 filterWithKey p Tip = Tip 895 1484 filterWithKey p (Bin _ kx x l r) … … 897 1486 | otherwise = merge (filterWithKey p l) (filterWithKey p r) 898 1487 899 1488 900 -- | /O(n)/. partition the map according to a predicate. The first1489 -- | Partition the map according to a predicate. The first 901 1490 -- map contains all elements that satisfy the predicate, the second all 902 1491 -- elements that fail the predicate. See also 'split'. 1492 -- /O(n)/. 1493 -- 1494 -- > let m = fromList [(5,"a"), (3,"b")] 1495 -- > m 1496 -- > {3:="b",5:="a"} 1497 -- > partition (> "a") m 1498 -- > ({3:="b"},{5:="a"}) 1499 -- > partition (< "x") m 1500 -- > ({3:="b",5:="a"},{}) 1501 -- > partition (> "x") m 1502 -- > ({},{3:="b",5:="a"}) 1503 903 1504 partition :: Ord k => (a -> Bool) -> Map k a -> (Map k a,Map k a) 904 1505 partition p m 905 1506 = partitionWithKey (\k x -> p x) m 906 1507 907 -- | /O(n)/. partition the map according to a predicate. The first1508 -- | Partition the map according to a predicate. The first 908 1509 -- map contains all elements that satisfy the predicate, the second all 909 -- elements that fail the predicate. See also 'split'. 1510 -- elements that fail the predicate. See also 'split'. /O(n)/. 1511 -- 1512 -- > let m = fromList [(5,"a"), (3,"b")] 1513 -- > m 1514 -- > {3:="b",5:="a"} 1515 -- > partitionWithKey (\ k _ -> k > 3) m 1516 -- > ({5:="a"},{3:="b"}) 1517 -- > partitionWithKey (\ k _ -> k < 10) m 1518 -- > ({3:="b",5:="a"},{}) 1519 -- > partitionWithKey (\ k _ -> k > 10) m 1520 -- > ({},{3:="b",5:="a"}) 1521 910 1522 partitionWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> (Map k a,Map k a) 911 1523 partitionWithKey p Tip = (Tip,Tip) 912 1524 partitionWithKey p (Bin _ kx x l r) … … 916 1528 (l1,l2) = partitionWithKey p l 917 1529 (r1,r2) = partitionWithKey p r 918 1530 919 -- | /O(n)/. Map values and collect the 'Just' results. 1531 -- | Map values and collect the 'Just' results. /O(n)/. 1532 -- 1533 -- > let m = fromList [(5,"a"), (3,"b")] 1534 -- > m 1535 -- > {3:="b",5:="a"} 1536 -- > let f x = if x == "a" then Just "new a" else Nothing 1537 -- > mapMaybe f m 1538 -- > {5:="new a"} 1539 920 1540 mapMaybe :: Ord k => (a -> Maybe b) -> Map k a -> Map k b 921 1541 mapMaybe f m 922 1542 = mapMaybeWithKey (\k x -> f x) m 923 1543 924 -- | /O(n)/. Map keys\/values and collect the 'Just' results. 1544 -- | Map keys\/values and collect the 'Just' results. /O(n)/. 1545 -- 1546 -- > let m = fromList [(5,"a"), (3,"b")] 1547 -- > m 1548 -- > {3:="b",5:="a"} 1549 -- > let f k _ = if k < 5 then Just ("key : " ++ (show k)) else Nothing 1550 -- > mapMaybeWithKey f m 1551 -- > {3,"key : 3"} 1552 925 1553 mapMaybeWithKey :: Ord k => (k -> a -> Maybe b) -> Map k a -> Map k b 926 1554 mapMaybeWithKey f Tip = Tip 927 1555 mapMaybeWithKey f (Bin _ kx x l r) = case f kx x of 928 1556 Just y -> join kx y (mapMaybeWithKey f l) (mapMaybeWithKey f r) 929 1557 Nothing -> merge (mapMaybeWithKey f l) (mapMaybeWithKey f r) 930 1558 931 -- | /O(n)/. Map values and separate the 'Left' and 'Right' results. 1559 -- | Map values and separate the 'Left' and 'Right' results. /O(n)/. 1560 -- 1561 -- > let m = fromList [(5,"a"), (3,"b"), (1, "x"), (10, "z")] 1562 -- > m 1563 -- > {1:="x",3:="b",5:="a",10:="z"} 1564 -- > let f a = if a < "c" then Left a else Right a 1565 -- > mapEither f m 1566 -- > ({3:="b",5:="a"},{1:="x",10:="z"}) 1567 -- > 1568 -- > let f a = Right a 1569 -- > mapEither f m 1570 -- > ({},{1:="x",3:="b",5:="a",10:="z"}) 1571 932 1572 mapEither :: Ord k => (a -> Either b c) -> Map k a -> (Map k b, Map k c) 933 1573 mapEither f m 934 1574 = mapEitherWithKey (\k x -> f x) m 935 1575 936 -- | /O(n)/. Map keys\/values and separate the 'Left' and 'Right' results. 1576 -- | Map keys\/values and separate the 'Left' and 'Right' results. /O(n)/. 1577 -- 1578 -- > let m = fromList [(5,"a"), (3,"b"), (1, "x"), (10, "z")] 1579 -- > m 1580 -- > {1:="x",3:="b",5:="a",10:="z"} 1581 -- > let f k a = if k < 5 then Left k else Right a 1582 -- > mapEitherWithKey f m 1583 -- > ({1:=2,3:=6},{5:="a",10:="z"}) 1584 -- > 1585 -- > let f _ a = Right a 1586 -- > mapEitherWithKey f m 1587 -- > ({},{1:="x",3:="b",5:="a",10:="z"}) 1588 937 1589 mapEitherWithKey :: Ord k => 938 1590 (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c) 939 1591 mapEitherWithKey f Tip = (Tip, Tip) … … 947 1599 {-------------------------------------------------------------------- 948 1600 Mapping 949 1601 --------------------------------------------------------------------} 950 -- | /O(n)/. Map a function over all values in the map. 1602 -- | Map a function over all values in the map. /O(n)/. 1603 -- 1604 -- > let m = fromList [(5,"a"), (3,"b")] 1605 -- > m 1606 -- > {3:="b",5:="a"} 1607 -- > map (++ "x") m 1608 -- > {3:="bx",5:="ax"} 1609 951 1610 map :: (a -> b) -> Map k a -> Map k b 952 1611 map f m 953 1612 = mapWithKey (\k x -> f x) m 954 1613 955 -- | /O(n)/. Map a function over all values in the map. 1614 -- | Map a function over all values in the map. /O(n)/. 1615 -- 1616 -- > let m = fromList [(5,"a"), (3,"b")] 1617 -- > m 1618 -- > {3:="b",5:="a"} 1619 -- > let f key x = (show key) ++ ":" ++ x 1620 -- > mapWithKey f m 1621 -- > {3:="3:b",5:="5:a"} 1622 956 1623 mapWithKey :: (k -> a -> b) -> Map k a -> Map k b 957 1624 mapWithKey f Tip = Tip 958 1625 mapWithKey f (Bin sx kx x l r) 959 1626 = Bin sx kx (f kx x) (mapWithKey f l) (mapWithKey f r) 960 1627 961 -- | /O(n)/. The function 'mapAccum' threads an accumulating 962 -- argument through the map in ascending order of keys. 1628 -- | The function 'mapAccum' threads an accumulating 1629 -- argument through the map in ascending order of keys. /O(n)/. 1630 -- 1631 -- > let m = fromList [(5,"a"), (3,"b")] 1632 -- > m 1633 -- > {3:="b",5:="a"} 1634 -- > let f a b = (a ++ b, b ++ "X") 1635 -- > mapAccum f "Everything: " m 1636 -- > ("Everything: ba",{3:="bX",5:="aX"}) 1637 963 1638 mapAccum :: (a -> b -> (a,c)) -> a -> Map k b -> (a,Map k c) 964 1639 mapAccum f a m 965 1640 = mapAccumWithKey (\a k x -> f a x) a m 966 1641 967 -- | /O(n)/. The function 'mapAccumWithKey' threads an accumulating 968 -- argument through the map in ascending order of keys. 1642 -- | The function 'mapAccumWithKey' threads an accumulating 1643 -- argument through the map in ascending order of keys. /O(n)/. 1644 -- 1645 -- > let m = fromList [(5,"a"), (3,"b")] 1646 -- > m 1647 -- > {3:="b",5:="a"} 1648 -- > let f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X") 1649 -- > mapAccumWithKey f "Everything:" m 1650 -- > ("Everything: 3-b 5-a",{3:="bX",5:="aX"}) 1651 969 1652 mapAccumWithKey :: (a -> k -> b -> (a,c)) -> a -> Map k b -> (a,Map k c) 970 1653 mapAccumWithKey f a t 971 1654 = mapAccumL f a t 972 1655 973 -- | /O(n)/.The function 'mapAccumL' threads an accumulating974 -- argument throught the map in ascending order of keys. 1656 -- | The function 'mapAccumL' threads an accumulating 1657 -- argument throught the map in ascending order of keys. /O(n)/. 975 1658 mapAccumL :: (a -> k -> b -> (a,c)) -> a -> Map k b -> (a,Map k c) 976 1659 mapAccumL f a t 977 1660 = case t of … … 982 1665 (a3,r') = mapAccumL f a2 r 983 1666 in (a3,Bin sx kx x' l' r') 984 1667 985 -- | /O(n)/.The function 'mapAccumR' threads an accumulating986 -- argument throught the map in descending order of keys. 1668 -- | The function 'mapAccumR' threads an accumulating 1669 -- argument throught the map in descending order of keys. /O(n)/. 987 1670 mapAccumR :: (a -> k -> b -> (a,c)) -> a -> Map k b -> (a,Map k c) 988 1671 mapAccumR f a t 989 1672 = case t of … … 994 1677 (a3,l') = mapAccumR f a2 l 995 1678 in (a3,Bin sx kx x' l' r') 996 1679 997 -- | /O(n*log n)/. 998 -- @'mapKeys' f s@ is the map obtained by applying @f@ to each key of @s@. 1680 -- | @'mapKeys' f s@ is the map obtained by applying @f@ to each key of @s@. 999 1681 -- 1000 1682 -- The size of the result may be smaller if @f@ maps two or more distinct 1001 1683 -- keys to the same new key. In this case the value at the smallest of 1002 -- these keys is retained. 1684 -- these keys is retained. /O(n*log n)/. 1685 -- 1686 -- > let m = fromList [(5,"a"), (3,"b")] 1687 -- > m 1688 -- > {3:="b",5:="a"} 1689 -- > mapKeys (+ 1) m 1690 -- > {4:="b",6:="a"} 1691 -- > 1692 -- > -- overlapping resulting keys 1693 -- > let m = fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")] 1694 -- > m 1695 -- > {1:="b",2:="a",3:="d",4:="c"} 1696 -- > mapKeys (\ _ -> 1) m 1697 -- > {1:="c"} 1698 -- > mapKeys (\ _ -> 3) m 1699 -- > {3:="c"} 1003 1700 1004 1701 mapKeys :: Ord k2 => (k1->k2) -> Map k1 a -> Map k2 a 1005 1702 mapKeys = mapKeysWith (\x y->x) 1006 1703 1007 -- | /O(n*log n)/. 1008 -- @'mapKeysWith' c f s@ is the map obtained by applying @f@ to each key of @s@. 1704 -- | @'mapKeysWith' c f s@ is the map obtained by applying @f@ to each key of @s@. 1009 1705 -- 1010 1706 -- The size of the result may be smaller if @f@ maps two or more distinct 1011 1707 -- keys to the same new key. In this case the associated values will be 1012 -- combined using @c@. 1708 -- combined using @c@. /O(n*log n)/. 1709 -- 1710 -- > let m = fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")] 1711 -- > m 1712 -- > {1:="b",2:="a",3:="d",4:="c"} 1713 -- > mapKeysWith (++) (\ _ -> 1) m 1714 -- > {1:="cdab"} 1715 -- > mapKeysWith (++) (\ _ -> 3) m 1716 -- > {3:="cdab"} 1013 1717 1014 1718 mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1->k2) -> Map k1 a -> Map k2 a 1015 1719 mapKeysWith c f = fromListWith c . List.map fFirst . toList 1016 1720 where fFirst (x,y) = (f x, y) 1017 1721 1018 1722 1019 -- | /O(n)/. 1020 -- @'mapKeysMonotonic' f s == 'mapKeys' f s@, but works only when @f@ 1723 -- | @'mapKeysMonotonic' f s == 'mapKeys' f s@, but works only when @f@ 1021 1724 -- is strictly monotonic. 1725 -- That is, for any values @x@ and @y@, if @x@ < @y@ then @f x@ < @f y@. 1022 1726 -- /The precondition is not checked./ 1023 1727 -- Semi-formally, we have: 1024 1728 -- 1025 1729 -- > and [x < y ==> f x < f y | x <- ls, y <- ls] 1026 1730 -- > ==> mapKeysMonotonic f s == mapKeys f s 1027 1731 -- > where ls = keys s 1732 -- 1733 -- This means that @f@ maps distinct original keys to distinct resulting keys. 1734 -- This function has better performance than 'mapKeys'. /O(n)/. 1735 -- 1736 -- > let m = fromList [(5,"a"), (3,"b")] 1737 -- > m 1738 -- > {3:="b",5:="a"} 1739 -- > let result = mapKeysMonotonic (\ k -> k * 2) m 1740 -- > result 1741 -- > {6:="b",10:="a"} 1742 -- > valid result 1743 -- > True 1744 -- > 1745 -- > -- resulting map is invalid if the function is non-monotonic 1746 -- > let result = mapKeysMonotonic (\ _ -> 1) m 1747 -- > result 1748 -- > {1:="b",1:="a"} 1749 -- > valid result 1750 -- > False 1028 1751 1029 1752 mapKeysMonotonic :: (k1->k2) -> Map k1 a -> Map k2 a 1030 1753 mapKeysMonotonic f Tip = Tip … … 1035 1758 Folds 1036 1759 --------------------------------------------------------------------} 1037 1760 1038 -- | /O(n)/.Fold the values in the map, such that1761 -- | Fold the values in the map, such that 1039 1762 -- @'fold' f z == 'Prelude.foldr' f z . 'elems'@. 1040 1763 -- For example, 1041 1764 -- 1042 1765 -- > elems map = fold (:) [] map 1043 1766 -- 1767 -- /O(n)/. 1768 -- 1769 -- > let m = fromList [(5,"a"), (3,"bbb")] 1770 -- > m 1771 -- > {3:="bbb",5:="a"} 1772 -- > 1773 -- > let f a len = len + (length a) 1774 -- > -- sum of values lengths 1775 -- > fold f 0 m 1776 -- > 4 1777 1044 1778 fold :: (a -> b -> b) -> b -> Map k a -> b 1045 1779 fold f z m 1046 1780 = foldWithKey (\k x z -> f x z) z m 1047 1781 1048 -- | /O(n)/.Fold the keys and values in the map, such that1782 -- | Fold the keys and values in the map, such that 1049 1783 -- @'foldWithKey' f z == 'Prelude.foldr' ('uncurry' f) z . 'toAscList'@. 1050 1784 -- For example, 1051 1785 -- 1052 1786 -- > keys map = foldWithKey (\k x ks -> k:ks) [] map 1053 1787 -- 1788 -- /O(n)/. 1789 -- 1790 -- > let m = fromList [(5,"a"), (3,"b")] 1791 -- > m 1792 -- > {3:="b",5:="a"} 1793 -- > let f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")" 1794 -- > foldWithKey f "Map: " m 1795 -- > "Map: (5:a)(3:b)" 1796 1054 1797 foldWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b 1055 1798 foldWithKey f z t 1056 1799 = foldr f z t 1057 1800 1058 -- | /O(n)/. In-order fold.1801 -- | In-order fold. /O(n)/. 1059 1802 foldi :: (k -> a -> b -> b -> b) -> b -> Map k a -> b 1060 1803 foldi f z Tip = z 1061 1804 foldi f z (Bin _ kx x l r) = f kx x (foldi f z l) (foldi f z r) 1062 1805 1063 -- | /O(n)/. Post-order fold.1806 -- | Post-order fold. /O(n)/. 1064 1807 foldr :: (k -> a -> b -> b) -> b -> Map k a -> b 1065 1808 foldr f z Tip = z 1066 1809 foldr f z (Bin _ kx x l r) = foldr f (f kx x (foldr f z r)) l 1067 1810 1068 -- | /O(n)/. Pre-order fold.1811 -- | Pre-order fold. /O(n)/. 1069 1812 foldl :: (b -> k -> a -> b) -> b -> Map k a -> b 1070 1813 foldl f z Tip = z 1071 1814 foldl f z (Bin _ kx x l r) = foldl f (f (foldl f z l) kx x) r … … 1073 1816 {-------------------------------------------------------------------- 1074 1817 List variations 1075 1818 --------------------------------------------------------------------} 1076 -- | /O(n)/. 1077 -- Return all elements of the map in the ascending order of their keys. 1819 -- | Return all elements of the map in the ascending order of their keys. 1820 -- /O(n)/. 1821 -- 1822 -- > let m = fromList [(5,"a"), (3,"b")] 1823 -- > m 1824 -- > {3:="b",5:="a"} 1825 -- > elems m 1826 -- > ["b","a"] 1827 -- > elems empty 1828 -- > [] 1829 1078 1830 elems :: Map k a -> [a] 1079 1831 elems m 1080 1832 = [x | (k,x) <- assocs m] 1081 1833 1082 -- | /O(n)/. Return all keys of the map in ascending order. 1834 -- | Return all keys of the map in ascending order. /O(n)/. 1835 -- 1836 -- > let m = fromList [(5,"a"), (3,"b")] 1837 -- > m 1838 -- > {3:="b",5:="a"} 1839 -- > keys m 1840 -- > [3,5] 1841 -- > keys empty 1842 -- > [] 1843 1083 1844 keys :: Map k a -> [k] 1084 1845 keys m 1085 1846 = [k | (k,x) <- assocs m] 1086 1847 1087 -- | /O(n)/. The set of all keys of the map. 1848 -- | The set of all keys of the map. /O(n)/. 1849 -- 1850 -- > let m = fromList [(5,"a"), (3,"b")] 1851 -- > m 1852 -- > {3:="b",5:="a"} 1853 -- > keysSet m 1854 -- > {3,5} 1855 -- > keysSet empty 1856 -- > {} 1857 1088 1858 keysSet :: Map k a -> Set.Set k 1089 1859 keysSet m = Set.fromDistinctAscList (keys m) 1090 1860 1091 -- | /O(n)/. Return all key\/value pairs in the map in ascending key order. 1861 -- | Return all key\/value pairs in the map in ascending key order. /O(n)/. 1862 -- 1863 -- > let m = fromList [(5,"a"), (3,"b")] 1864 -- > m 1865 -- > {3:="b",5:="a"} 1866 -- > assocs m 1867 -- > [(3,"b"),(5,"a")] 1868 -- > assocs empty 1869 -- > [] 1870 1092 1871 assocs :: Map k a -> [(k,a)] 1093 1872 assocs m 1094 1873 = toList m … … 1097 1876 Lists 1098 1877 use [foldlStrict] to reduce demand on the control-stack 1099 1878 --------------------------------------------------------------------} 1100 -- | /O(n*log n)/. Build a map from a list of key\/value pairs. See also 'fromAscList'. 1879 -- | Build a map from a list of key\/value pairs. See also 'fromAscList'. 1880 -- If the list contains more than one value for the same key, the last value 1881 -- for the key is retained. 1882 -- /O(n*log n)/. 1883 -- 1884 -- > fromList [(5,"a"), (3,"b")] 1885 -- > {3:="b",5:="a"} 1886 -- > fromList [] 1887 -- > {} 1888 -- > fromList [(5,"a"), (3,"b"), (5, "b")] 1889 -- > {3:="b",5:="b"} 1890 -- > fromList [(5,"b"), (3,"b"), (5, "a")] 1891 -- > {3:="b",5:="a"} 1892 1101 1893 fromList :: Ord k => [(k,a)] -> Map k a 1102 1894 fromList xs 1103 1895 = foldlStrict ins empty xs 1104 1896 where 1105 1897 ins t (k,x) = insert k x t 1106 1898 1107 -- | /O(n*log n)/. Build a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'. 1899 -- | Build a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'. 1900 -- /O(n*log n)/. 1901 -- 1902 -- > fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] 1903 -- > {3:="ab",5:="aba"} 1904 -- > fromListWith (++) [] 1905 -- > {} 1906 1108 1907 fromListWith :: Ord k => (a -> a -> a) -> [(k,a)] -> Map k a 1109 1908 fromListWith f xs 1110 1909 = fromListWithKey (\k x y -> f x y) xs 1111 1910 1112 -- | /O(n*log n)/. Build a map from a list of key\/value pairs with a combining function. See also 'fromAscListWithKey'. 1911 -- | Build a map from a list of key\/value pairs with a combining function. See also 'fromAscListWithKey'. 1912 -- /O(n*log n)/. 1913 -- 1914 -- > let f k a1 a2 = (show k) ++ a1 ++ a2 1915 -- > fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] 1916 -- > {3:="3ab",5:="5a5ba"} 1917 -- > fromListWithKey f [] 1918 -- > {} 1919 1113 1920 fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k,a)] -> Map k a 1114 1921 fromListWithKey f xs 1115 1922 = foldlStrict ins empty xs 1116 1923 where 1117 1924 ins t (k,x) = insertWithKey f k x t 1118 1925 1119 -- | /O(n)/. Convert to a list of key\/value pairs. 1926 -- | Convert to a list of key\/value pairs. /O(n)/. 1927 -- 1928 -- > let m = fromList [(5,"a"), (3,"b")] 1929 -- > m 1930 -- > {3:="b",5:="a"} 1931 -- > toList m 1932 -- > [(3,"b"),(5,"a")] 1933 1120 1934 toList :: Map k a -> [(k,a)] 1121 1935 toList t = toAscList t 1122 1936 1123 -- | /O(n)/. Convert to an ascending list. 1937 -- | Convert to an ascending list. /O(n)/. 1938 -- 1939 -- > let m = fromList [(5,"a"), (3,"b")] 1940 -- > m 1941 -- > {3:="b",5:="a"} 1942 -- > 1943 -- > toAscList m 1944 -- > [(3,"b"),(5,"a")] 1945 1124 1946 toAscList :: Map k a -> [(k,a)] 1125 1947 toAscList t = foldr (\k x xs -> (k,x):xs) [] t 1126 1948 1127 1949 -- | /O(n)/. 1950 1128 1951 toDescList :: Map k a -> [(k,a)] 1129 1952 toDescList t = foldl (\xs k x -> (k,x):xs) [] t 1130 1953 … … 1136 1959 fromAscList xs == fromList xs 1137 1960 fromAscListWith f xs == fromListWith f xs 1138 1961 --------------------------------------------------------------------} 1139 -- | /O(n)/. Build a map from an ascending list in linear time. 1140 -- /The precondition (input list is ascending) is not checked./ 1962 -- | Build a map from an ascending list in linear time. 1963 -- /The precondition (input list is ascending) is not checked./ /O(n)/. 1964 -- 1965 -- > fromAscList [(3,"b"), (5,"a")] 1966 -- > {3:="b",5:="a"} 1967 -- > 1968 -- > let result = fromAscList [(3,"b"), (5,"a")] 1969 -- > result 1970 -- > {3:="b",5:="a"} 1971 -- > let result = fromAscList [(3,"b"), (5,"a"), (5,"b")] 1972 -- > result 1973 -- > {3:="b",5:="b"} 1974 -- > valid result 1975 -- > True 1976 -- > 1977 -- > -- invalid map is generated if the list is not ordered 1978 -- > let result = fromAscList [(5,"a"), (3,"b"), (5,"b")] 1979 -- > result 1980 -- > {5:="a",3:="b",5:="b"} 1981 -- > valid result 1982 -- > False 1983 1141 1984 fromAscList :: Eq k => [(k,a)] -> Map k a 1142 1985 fromAscList xs 1143 1986 = fromAscListWithKey (\k x y -> x) xs 1144 1987 1145 -- | /O(n)/. Build a map from an ascending list in linear time with a combining function for equal keys. 1146 -- /The precondition (input list is ascending) is not checked./ 1988 -- | Build a map from an ascending list in linear time with a combining function for equal keys. 1989 -- /The precondition (input list is ascending) is not checked./ /O(n)/. 1990 -- 1991 -- > let result = fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] 1992 -- > result 1993 -- > {3:="b",5:="ba"} 1994 -- > valid result 1995 -- > True 1996 -- > 1997 -- > -- invalid map is generated if the list is not ordered 1998 -- > let result = fromAscListWith (++) [(5,"a"), (3,"b"), (5,"b")] 1999 -- > result 2000 -- > {5:="a",3:="b",5:="b"} 2001 -- > valid result 2002 -- > False 2003 1147 2004 fromAscListWith :: Eq k => (a -> a -> a) -> [(k,a)] -> Map k a 1148 2005 fromAscListWith f xs 1149 2006 = fromAscListWithKey (\k x y -> f x y) xs 1150 2007 1151 -- | /O(n)/.Build a map from an ascending list in linear time with a2008 -- | Build a map from an ascending list in linear time with a 1152 2009 -- combining function for equal keys. 1153 -- /The precondition (input list is ascending) is not checked./ 2010 -- /The precondition (input list is ascending) is not checked./ /O(n)/. 2011 -- 2012 -- > let f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2 2013 -- > let result = fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")] 2014 -- > result 2015 -- > {3:="b",5:="5:b5:ba"} 2016 -- > valid result 2017 -- > True 2018 -- > 2019 -- > -- invalid map is generated if the list is not ordered 2020 -- > let result = fromAscListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")] 2021 -- > result 2022 -- > {5:="a",3:="b",5:="5:bb"} 2023 -- > valid result 2024 -- > False 2025 1154 2026 fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k,a)] -> Map k a 1155 2027 fromAscListWithKey f xs 1156 2028 = fromDistinctAscList (combineEq f xs) … … 1168 2040 | otherwise = z:combineEq' x xs 1169 2041 1170 2042 1171 -- | /O(n)/. Build a map from an ascending list of distinct elements in linear time. 1172 -- /The precondition is not checked./ 2043 -- | Build a map from an ascending list of distinct elements in linear time. 2044 -- /The precondition is not checked./ /O(n)/. 2045 -- 2046 -- > let result = fromDistinctAscList [(3,"b"), (5,"a")] 2047 -- > result 2048 -- > {3:="b",5:="a"} 2049 -- > valid result 2050 -- > True 2051 -- > 2052 -- > -- invalid map is generated if the list had non-distinct elements 2053 -- > let result = fromDistinctAscList [(3,"b"), (5,"a"), (5,"b")] 2054 -- > result 2055 -- > {3:="b",5:="a",5:="b"} 2056 -- > valid result 2057 -- > False 2058 1173 2059 fromDistinctAscList :: [(k,a)] -> Map k a 1174 2060 fromDistinctAscList xs 1175 2061 = build const (length xs) xs … … 1255 2141 {-------------------------------------------------------------------- 1256 2142 Split 1257 2143 --------------------------------------------------------------------} 1258 -- | /O(log n)/. The expression (@'split' k map@) is a pair @(map1,map2)@ where 1259 -- 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@. 2144 -- | The expression (@'split' k map@) is a pair @(map1,map2)@ where 2145 -- the keys in @map1@ are smaller than @k@ and the keys in @map2@ larger than @k@. 2146 -- Any key equal to @k@ is found in neither @map1@ nor @map2@. 2147 -- /O(log n)/. 2148 -- 2149 -- > let m = fromList [(5,"a"), (3,"b")] 2150 -- > m 2151 -- > {3:="b",5:="a"} 2152 -- > split 2 m 2153 -- > ({},{3:="b",5:="a"}) 2154 -- > split 3 m 2155 -- > ({},{5:="a"}) 2156 -- > split 4 m 2157 -- > ({3:="b"},{5:="a"}) 2158 -- > split 5 m 2159 -- > ({3:="b"},{}) 2160 -- > split 6 m 2161 -- > ({3:="b",5:="a"},{}) 2162 1260 2163 split :: Ord k => k -> Map k a -> (Map k a,Map k a) 1261 2164 split k Tip = (Tip,Tip) 1262 2165 split k (Bin sx kx x l r) … … 1265 2168 GT -> let (lt,gt) = split k r in (join kx x l lt,gt) 1266 2169 EQ -> (l,r) 1267 2170 1268 -- | /O(log n)/.The expression (@'splitLookup' k map@) splits a map just2171 -- | The expression (@'splitLookup' k map@) splits a map just 1269 2172 -- like 'split' but also returns @'lookup' k map@. 2173 -- /O(log n)/. 2174 -- 2175 -- > let m = fromList [(5,"a"), (3,"b")] 2176 -- > m 2177 -- > {3:="b",5:="a"} 2178 -- > splitLookup 2 m 2179 -- > ({},Nothing,{3:="b",5:="a"}) 2180 -- > splitLookup 3 m 2181 -- > ({},Just "b",{5:="a"}) 2182 -- > splitLookup 4 m 2183 -- > ({3:="b"},Nothing,{5,"a"}) 2184 -- > splitLookup 5 m 2185 -- > ({3,"b"},Just "a",{}) 2186 -- > splitLookup 6 m 2187 -- > ({3:="b",5:="a"},Nothing,{}) 2188 1270 2189 splitLookup :: Ord k => k -> Map k a -> (Map k a,Maybe a,Map k a) 1271 2190 splitLookup k Tip = (Tip,Nothing,Tip) 1272 2191 splitLookup k (Bin sx kx x l r) … … 1284 2203 GT -> let (lt,z,gt) = splitLookupWithKey k r in (join kx x l lt,z,gt) 1285 2204 EQ -> (l,Just (kx, x),r) 1286 2205 1287 -- | /O(log n)/.Performs a 'split' but also returns whether the pivot2206 -- | Performs a 'split' but also returns whether the pivot 1288 2207 -- element was found in the original set. 2208 -- /O(log n)/. 1289 2209 splitMember :: Ord k => k -> Map k a -> (Map k a,Bool,Map k a) 1290 2210 splitMember x t = let (l,m,r) = splitLookup x t in 1291 2211 (l,maybe False (const True) m,r) … … 1369 2289 | otherwise = let ((km,m),r') = deleteFindMin r in balance km m l r' 1370 2290 1371 2291 1372 -- | /O(log n)/. Delete and find the minimal element. 2292 -- | Delete and find the minimal element. /O(log n)/. 2293 -- 2294 -- > let m = fromList [(5,"a"), (3,"b"), (10,"c")] 2295 -- > m 2296 -- > {3:="b",5:="a",10:="c"} 2297 -- > deleteFindMin m 2298 -- > ((3,"b"),{5:="a",10:="c"}) 2299 -- > deleteFindMin empty 2300 -- > Exception: Map.deleteFindMin: can not return the minimal element of an empty map 2301 1373 2302 deleteFindMin :: Map k a -> ((k,a),Map k a) 1374 2303 deleteFindMin t 1375 2304 = case t of … … 1377 2306 Bin _ k x l r -> let (km,l') = deleteFindMin l in (km,balance k x l' r) 1378 2307 Tip -> (error "Map.deleteFindMin: can not return the minimal element of an empty map", Tip) 1379 2308 1380 -- | /O(log n)/. Delete and find the maximal element. 2309 -- | Delete and find the maximal element. /O(log n)/. 2310 -- 2311 -- > let m = fromList [(5,"a"), (3,"b"), (10,"c")] 2312 -- > m 2313 -- > {3:="b",5:="a",10:="c"} 2314 -- > deleteFindMax m 2315 -- > ((10,"c"),{3:="b",5:="a"}) 2316 -- > deleteFindMax empty 2317 -- > Exception: Map.deleteFindMax: can not return the maximal element of an empty map 2318 1381 2319 deleteFindMax :: Map k a -> ((k,a),Map k a) 1382 2320 deleteFindMax t 1383 2321 = case t of … … 1531 2469 showElem (k,x) = shows k . showString " := " . shows x 1532 2470 1533 2471 1534 -- | /O(n)/.Show the tree that implements the map. The tree is shown1535 -- in a compressed, hanging format. 2472 -- | Show the tree that implements the map. The tree is shown 2473 -- in a compressed, hanging format. See 'showTreeWith'. /O(n)/. 1536 2474 showTree :: (Show k,Show a) => Map k a -> String 1537 2475 showTree m 1538 2476 = showTreeWith showElem True False m … … 1540 2478 showElem k x = show k ++ ":=" ++ show x 1541 2479 1542 2480 1543 {- | /O(n)/.The expression (@'showTreeWith' showelem hang wide map@) shows2481 {- | The expression (@'showTreeWith' showelem hang wide map@) shows 1544 2482 the tree that implements the map. Elements are shown using the @showElem@ function. If @hang@ is 1545 2483 'True', a /hanging/ tree is shown otherwise a rotated tree is shown. If 1546 @wide@ is 'True', an extra wide version is shown. 2484 @wide@ is 'True', an extra wide version is shown. /O(n)/. 1547 2485 1548 2486 > Map> let t = fromDistinctAscList [(x,()) | x <- [1..5]] 1549 2487 > Map> putStrLn $ showTreeWith (\k x -> show (k,x)) True False t … … 1632 2570 {-------------------------------------------------------------------- 1633 2571 Assertions 1634 2572 --------------------------------------------------------------------} 1635 -- | /O(n)/. Test if the internal map structure is valid. 2573 -- | Test if the internal map structure is valid. /O(n)/. 2574 -- 2575 -- > let result = fromAscList [(3,"b"), (5,"a")] 2576 -- > result 2577 -- > {3:="b",5:="a"} 2578 -- > let result = fromAscList [(3,"b"), (5,"a"), (5,"b")] 2579 -- > result 2580 -- > {3:="b",5:="b"} 2581 -- > valid result 2582 -- > True 2583 -- > 2584 -- > -- invalid map is generated if the list is not ordered 2585 -- > let result = fromAscList [(5,"a"), (3,"b"), (5,"b")] 2586 -- > result 2587 -- > {5:="a",3:="b",5:="b"} 2588 -- > valid result 2589 -- > False 2590 1636 2591 valid :: Ord k => Map k a -> Bool 1637 2592 valid t 1638 2593 = balanced t && ordered t && validsize t
