module Data.Map.AVL (
Map
, (!)
, (\\)
, null
, size
, member
, lookup
, findWithDefault
, empty
, singleton
, insert
, insertWith, insertWithKey, insertLookupWithKey
, delete
, adjust
, alter
, adjustWithKey
, update
, updateWithKey
, updateLookupWithKey
, union
, unionWith
, unionWithKey
, unions
, unionsWith
, difference
, differenceWith
, differenceWithKey
, intersection
, intersectionWith
, intersectionWithKey
, map
, mapWithKey
, mapAccum
, fold
, foldWithKey
, elems
, keys
, keysSet
, liftKeysSet
, assocs
, unsafeFromTree
, toTree
, toList
, fromList
, fromListWith
, fromListWithKey
, toAscList
, fromAscList
, fromAscListWith
, fromAscListWithKey
, fromDistinctAscList
, filter
, filterWithKey
, partition
, partitionWithKey
, split
, splitLookup
, isSubmapOf
, isSubmapOfBy
, findMin
, findMax
, deleteMin
, deleteMax
, deleteFindMin
, deleteFindMax
) where
import Prelude hiding (lookup,map,filter,foldr,foldl,null)
import qualified Data.List as List
import Data.Monoid
import qualified Data.Set.AVL as Set
import Data.Foldable hiding (toList, find, fold)
import qualified Data.COrdering as COrdering
import qualified Data.Tree.AVL as AVL
import Data.Typeable
#include "Typeable.h"
INSTANCE_TYPEABLE2(Map,mapTc,"Data.Map.AVL")
readValCC :: Ord k => k -> (k, a) -> COrdering.COrdering a
readValCC k (k', a) = case compare k k' of
LT -> COrdering.Lt
EQ -> COrdering.Eq a
GT -> COrdering.Gt
mcmp :: Ord a => (a, b) -> (a, c) -> COrdering.COrdering (a, b)
mcmp (k, a) (k', _) = case compare k k' of
LT -> COrdering.Lt
EQ -> COrdering.Eq (k, a)
GT -> COrdering.Gt
mfcmp :: Ord k => (k -> a -> b -> c) -> (k, a) -> (k, b)
-> COrdering.COrdering (k, c)
mfcmp f (k, a) (k', b) = case compare k k' of
LT -> COrdering.Lt
EQ -> COrdering.Eq (k, f k a b)
GT -> COrdering.Gt
mmfcmp :: (Functor f, Ord k) => (k -> a -> b -> f c) -> (k, a)
-> (k, b) -> COrdering.COrdering (f (k, c))
mmfcmp f (k, a) (k', b) = case compare k k' of
LT -> COrdering.Lt
EQ -> COrdering.Eq $ fmap (\c -> (k, c)) $ f k a b
GT -> COrdering.Gt
infixl 9 !, \\
toOrdering :: COrdering.COrdering a -> Ordering
toOrdering c = case c of
COrdering.Lt -> LT
COrdering.Eq _ -> EQ
COrdering.Gt -> GT
toOrd :: (a -> b -> COrdering.COrdering c) -> a -> b -> Ordering
toOrd f a = toOrdering . f a
newtype Map k a = Map (AVL.AVL (k, a))
instance (Eq k, Eq a) => Eq (Map k a) where
m1 == m2 = toList m1 == toList m2
instance (Ord k, Ord a) => Ord (Map k a) where
compare m1 m2 = compare (toList m1) (toList m2)
showSet :: (Show a) => [a] -> ShowS
showSet []
= showString "{}"
showSet (x:xs)
= showChar '{' . shows x . showTail xs
where
showTail [] = showChar '}'
showTail (x':xs') = showString ", " . shows x' . showTail xs'
instance (Show k, Show a) => Show (Map k a) where
showsPrec _ (Map t) = showSet (AVL.asListL t)
empty :: Map k a
empty = Map (AVL.empty)
singleton :: k -> a -> Map k a
singleton k a = k `seq` Map (AVL.singleton (k, a))
null :: Map k a -> Bool
null (Map t) = AVL.isEmpty t
size :: Map k a -> Int
size (Map t) = AVL.size t
member :: Ord k => k -> Map k a -> Bool
member k (Map t) = k `seq` AVL.genContains t (compare k . fst)
(!) :: Ord k => Map k a -> k -> a
(!) m k = find k m
find :: Ord k => k -> Map k a -> a
find = findWithDefault (error "Map.find: element not in the map")
lookup :: (Monad m,Ord k) => k -> Map k a -> m a
lookup k (Map t) = k `seq` maybe (fail "AvlMap.lookup: Key not found")
return (AVL.genTryRead t (readValCC k))
findWithDefault :: Ord k => a -> k -> Map k a -> a
findWithDefault def k (Map t) = k `seq` AVL.genDefaultRead def t (readValCC k)
insert :: Ord k => k -> a -> Map k a -> Map k a
insert k a (Map t) = k `seq` Map (AVL.genPush (mcmp (k, a)) (k, a) t)
insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWith f = insertWithKey (\_ z y -> f z y)
insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a
insertWithKey f k a (Map t) =
k `seq` Map (AVL.genPush (mfcmp f (k, a)) (k, a) t)
insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a,Map k a)
insertLookupWithKey f k a m = (lookup k m, insertWithKey f k a m)
delete :: Ord k => k -> Map k a -> Map k a
delete k (Map t) = k `seq` Map (AVL.genDel (compare k . fst) t)
map :: (a -> b) -> Map k a -> Map k b
map f = mapWithKey (\_ x -> f x)
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
mapWithKey f (Map t) = Map (AVL.mapAVL mf t)
where mf (k', a') = (k', f k' a')
mapAccum :: Ord k => (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)
mapAccum f a = foldWithKey ( \ k b (s, m) ->
let (r, c) = f s b in (r, insert k c m)) (a, empty)
filter :: Ord k => (a -> Bool) -> Map k a -> Map k a
filter p (Map t) = Map (AVL.filterViaList (p . snd) t)
filterWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> Map k a
filterWithKey p (Map t) = Map (AVL.filterViaList (mp p) t)
mp :: (k -> a -> Bool) -> (k, a) -> Bool
mp p (k, a) = p k a
partition :: Ord k => (a -> Bool) -> Map k a -> (Map k a,Map k a)
partition p = partitionWithKey (\_ x -> p x)
partitionWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> (Map k a,Map k a)
partitionWithKey p (Map t) = let (t1, t2) = AVL.partitionAVL (mp p) t in
(Map t1, Map t2)
split :: Ord k => k -> Map k a -> (Map k a,Map k a)
split k (Map t) = (Map lessT, Map greaterT)
where (lessT, _, greaterT) = AVL.genFork (readValCC k) t
splitLookup :: Ord k => k -> Map k a -> (Map k a,Maybe a,Map k a)
splitLookup k (Map t) = (Map lessT, a, Map greaterT)
where (lessT, a, greaterT) = AVL.genFork (readValCC k) t
findMin :: Map k a -> (k,a)
findMin (Map t) = AVL.assertReadL t
deleteMin :: Map k a -> Map k a
deleteMin (Map t) = Map $ maybe (error "Set.deleteMin") id $ AVL.tryDelL t
deleteFindMin :: Map k a -> ((k,a),Map k a)
deleteFindMin (Map t) = let ((m, v), s) = AVL.assertPopL t in ((m, v), Map s)
deleteFindMax :: Map k a -> ((k,a),Map k a)
deleteFindMax (Map t) = let (s, (m, v)) = AVL.assertPopR t in ((m, v), Map s)
findMax :: Map k a -> (k,a)
findMax (Map t) = AVL.assertReadR t
deleteMax :: Map k a -> Map k a
deleteMax (Map t) = Map $ maybe (error "Set.deleteMax") id $ AVL.tryDelR t
intersection :: Ord k => Map k a -> Map k b -> Map k a
intersection (Map t1) (Map t2) = Map (AVL.genIntersection mcmp t1 t2)
intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c
intersectionWith f = intersectionWithKey (\_ x y -> f x y)
intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b
-> Map k c
intersectionWithKey f (Map t1) (Map t2) =
Map (AVL.genIntersection (mfcmp f) t1 t2)
toList :: Map k a -> [(k,a)]
toList (Map t) = AVL.asListL t
toAscList :: Map k a -> [(k,a)]
toAscList = toList
assocs :: Map k a -> [(k,a)]
assocs = toList
keys :: Map k a -> [k]
keys = List.map fst . toList
keysSet :: Map k a -> Set.Set k
keysSet = Set.unsafeFromTree . fmap fst . toTree
liftKeysSet :: (k -> b) -> Set.Set k -> Map k b
liftKeysSet f = unsafeFromTree . fmap (\k -> (k,f k)) . Set.toTree
elems :: Map k a -> [a]
elems (Map t) = List.map snd (AVL.asListL t)
fold :: (a -> b -> b) -> b -> Map k a -> b
fold f = foldWithKey (\_ x c -> f x c)
foldWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b
foldWithKey f z (Map t) = AVL.foldlAVL' (\ c (k, a) -> f k a c) z t
(\\) :: Ord k => Map k a -> Map k b -> Map k a
m1 \\ m2 = difference m1 m2
difference :: Ord k => Map k a -> Map k b -> Map k a
difference (Map t1) (Map t2) = Map (AVL.genDifference (toOrd mcmp) t1 t2)
differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
differenceWith f = differenceWithKey (\_ x y -> f x y)
differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b
-> Map k a
differenceWithKey f (Map t1) (Map t2) =
Map (AVL.genDifferenceMaybe (mmfcmp f) t1 t2)
fromDistinctAscList :: [(k,a)] -> Map k a
fromDistinctAscList = Map . AVL.asTreeL
fromAscList :: Eq k => [(k,a)] -> Map k a
fromAscList = fromAscListWithKey (\_ x _ -> x)
fromAscListWith :: Eq k => (a -> a -> a) -> [(k,a)] -> Map k a
fromAscListWith f = fromAscListWithKey (\_ x y -> f x y)
fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k,a)] -> Map k a
fromAscListWithKey f = fromDistinctAscList . combineEq
where
combineEq xs
= case xs of
[] -> []
[x] -> [x]
(x:xx) -> combineEq' x xx
combineEq' z [] = [z]
combineEq' z@(kz,zz) (x@(kx,xx):xs)
| kx==kz = let yy = f kx xx zz in combineEq' (kx,yy) xs
| otherwise = z:combineEq' x xs
fromList :: Ord k => [(k,a)] -> Map k a
fromList l = Map (AVL.genAsTree mcmp l)
unions :: Ord k => [Map k a] -> Map k a
unions ts
= foldlStrict union empty ts
unionsWith :: Ord k => (a->a->a) -> [Map k a] -> Map k a
unionsWith f ts
= foldlStrict (unionWith f) empty ts
union :: Ord k => Map k a -> Map k a -> Map k a
union = unionWith const
unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith f = unionWithKey (\_ x y -> f x y)
unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWithKey f (Map t1) (Map t2) = Map (AVL.genUnion (mfcmp f) t1 t2)
isSubmapOf :: (Ord k,Eq a) => Map k a -> Map k a -> Bool
isSubmapOf = isSubmapOfBy (==)
isSubmapOfBy :: Ord k => (a->b->Bool) -> Map k a -> Map k b -> Bool
isSubmapOfBy f (Map s) (Map t) = AVL.genIsSubsetOf
(\ (k, a) (k', b) -> case compare k k' of
LT -> LT
GT -> GT
EQ -> if f a b then EQ else LT) s t
alter :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
alter f k m = case f (lookup k m) of
Just a -> insert k a m
Nothing -> delete k m
adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a
adjust f = adjustWithKey (\_ x -> f x)
adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a
adjustWithKey f = updateWithKey (\k x -> Just (f k x))
update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a
update f = updateWithKey (\_ x -> f x)
updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a
updateWithKey f k (Map t) = let
cc (k', a) = case compare k k' of
LT -> COrdering.Lt
EQ -> COrdering.Eq $ fmap ( \ c -> (k', c)) $ f k' a
GT -> COrdering.Gt
in Map (AVL.genDelMaybe cc t)
updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a,Map k a)
updateLookupWithKey f k m = (lookup k m, updateWithKey f k m)
fromListWith :: Ord k => (a -> a -> a) -> [(k,a)] -> Map k a
fromListWith f xs
= fromListWithKey (\_k x y -> f x y) xs
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k,a)] -> Map k a
fromListWithKey f xs
= foldlStrict ins empty xs
where
ins t (k,x) = insertWithKey f k x t
unsafeFromTree :: AVL.AVL (k,a) -> Map k a
unsafeFromTree = Map
toTree :: Map k a -> AVL.AVL (k,a)
toTree (Map t) = t
instance Foldable (Map k) where
foldMap f (Map t) = foldMap (f . snd) t
instance Ord k => Monoid (Map k a) where
mempty = empty
mappend = union
instance Functor (Map k) where
fmap f (Map t) = Map (fmap f' t)
where f' (k,a) = (k,f a)
foldlStrict :: (a -> b -> a) -> a -> [b] -> a
foldlStrict f z xs
= case xs of
[] -> z
(x:xx) -> let z' = f z x in seq z' (foldlStrict f z' xx)