{-# OPTIONS_GHC -fglasgow-exts -fno-warn-orphans #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Trie.General.CollectionsInstances.OrdGT -- Copyright : (c) Adrian Hey 2007 -- License : BSD3 -- -- Maintainer : http://homepages.nildram.co.uk/~ahey/em.png -- Stability : provisional -- Portability : Multi-parameter type classes, Functional dependencies -- -- Instances of the Collections package Classes for the 'OrdGT' type. ----------------------------------------------------------------------------- module Data.Trie.General.CollectionsInstances.OrdGT ( ) where import Data.Trie.General.OrdGT import qualified Data.Monoid as M (Monoid(..)) import qualified Data.Collections as Coll (Foldable(..),foldr',Unfoldable(..),Collection(..),Map(..)) import qualified Data.Maybe as MB (isJust) #ifdef __GLASGOW_HASKELL__ import GHC.Base #include "ghcdefs.h" #else #include "h98defs.h" #endif ------------------------------- -- Data.Collections.Foldable -- ------------------------------- instance Coll.Foldable (OrdGT k (k,a)) (k,a) where -- fold :: Monoid (k,a) => OrdGT k (k,a) -> (k,a) fold mp = foldrElemsAscendingOrdGT (\assoc b -> M.mappend assoc b) mp M.mempty -- foldMap :: Monoid m => ((k,a) -> m) -> OrdGT k (k,a) -> m foldMap f mp = foldrElemsAscendingOrdGT (\assoc b -> M.mappend (f assoc) b) mp M.mempty -- foldr :: ((k,a) -> b -> b) -> b -> OrdGT k (k,a) -> b foldr f b0 mp = foldrElemsAscendingOrdGT (\assoc b -> f assoc b) mp b0 -- foldl :: (b -> (k,a) -> b) -> b -> OrdGT k (k,a) -> b foldl f b0 mp = foldrElemsDescendingOrdGT (\assoc b -> f b assoc) mp b0 {- ToDo: Implement properly. Meantime Foldable class has suitable defaults via lists. -- foldr1 :: ((k,a) -> (k,a) -> (k,a)) -> OrdGT k (k,a) -> (k,a) -- foldl1 :: ((k,a) -> (k,a) -> (k,a)) -> OrdGT k (k,a) -> (k,a) -} -- null :: OrdGT k (k,a) -> Bool null = isEmptyOrdGT -- size :: OrdGT k (k,a) -> Int size mp = ASINT(addSizeOrdGT mp L(0)) -- isSingleton :: OrdGT k (k,a) -> Bool isSingleton = isSingletonOrdGT ------------------------------- --------------------------------- -- Data.Collections.Unfoldable -- --------------------------------- instance Ord k => Coll.Unfoldable (OrdGT k (k,a)) (k,a) where -- insert :: (k,a) -> OrdGT k (k,a) -> OrdGT k (k,a) insert assoc@(k,_) mp = insertOrdGT' (const assoc) k assoc mp -- Note use of strict insertOrdGT' -- empty :: OrdGT k (k,a) empty = emptyOrdGT -- singleton :: (k,a) -> OrdGT k (k,a) singleton assoc@(k,_) = singletonOrdGT k assoc -- insertMany :: Foldable c' (k,a) => c' -> OrdGT k (k,a) -> OrdGT k (k,a) insertMany c mp0 = Coll.foldr (\assoc@(k,_) mp -> insertOrdGT' (const assoc) k assoc mp) mp0 c -- ?? stricness?? l/r?? -- insertManySorted :: Foldable c' (k,a) => c' -> OrdGT k (k,a) -> OrdGT k (k,a) insertManySorted c mp0 = Coll.foldr (\assoc@(k,_) mp -> insertOrdGT' (const assoc) k assoc mp) mp0 c -- How to implement efficiently ?? --------------------------------- --------------------------------- -- Data.Collections.Collection -- --------------------------------- instance Ord k => Coll.Collection (OrdGT k (k,a)) (k,a) where -- filter :: ((k,a) -> Bool) -> OrdGT k (k,a) -> OrdGT k (k,a) filter = filterOrdGT --------------------------------- -------------------------- -- Data.Collections.Map -- -------------------------- instance (Ord k, M.Monoid a) => Coll.Map (OrdGT k a) k a where -- delete :: k -> OrdGT k a -> OrdGT k a delete = deleteOrdGT -- member :: k -> OrdGT k a -> Bool member k mp = MB.isJust (lookupOrdGT k mp) -- union :: OrdGT k a -> OrdGT k a -> OrdGT k a union = unionOrdGT' (\x _ -> x) -- Note use of strict unionOrdGT' -- intersection :: OrdGT k a -> OrdGT k a -> OrdGT k a intersection = intersectionOrdGT' (\x _ -> x) -- Note use of strict intersectionOrdGT' -- difference :: OrdGT k a -> OrdGT k a -> OrdGT k a difference = differenceOrdGT -- isSubset :: OrdGT k a -> OrdGT k a -> Bool isSubset = isSubsetOfOrdGT -- lookup :: Monad m => k -> OrdGT k a -> m a lookup k mp = case lookupOrdGT k mp of Just a -> return a Nothing -> fail "Data.Collections.Map.lookup: Key not found in OrdGT." -- alter :: (Maybe a -> Maybe a) -> k -> OrdGT k a -> OrdGT k a alter = alterOrdGT -- insertWith :: (a -> a -> a) -> k -> a -> OrdGT k a -> OrdGT k a insertWith f k a ogt = insertOrdGT (f a) k a ogt -- fromFoldableWith :: Foldable l (k,a) => (a -> a -> a) -> l -> OrdGT k a fromFoldableWith f l = Coll.foldr insrt emptyOrdGT l -- Strictness ?? where insrt (k,a) ogt = insertOrdGT (f a) k a ogt -- foldGroups :: Foldable l (k,b) => (b -> a -> a) -> a -> l -> OrdGT k a foldGroups f a0 l = Coll.foldr' insrt emptyOrdGT l where insrt (k,b) ogt = insertOrdGT (f b) k (f b a0) ogt -- mapWithKey :: (k -> a -> a) -> OrdGT k a -> OrdGT k a mapWithKey = mapWithKeyOrdGT -- unionWith :: (a -> a -> a) -> OrdGT k a -> OrdGT k a -> OrdGT k a unionWith = unionOrdGT -- intersectionWith :: (a -> a -> a) -> OrdGT k a -> OrdGT k a -> OrdGT k a intersectionWith = intersectionOrdGT -- differenceWith :: (a -> a -> Maybe a) -> OrdGT k a -> OrdGT k a -> OrdGT k a differenceWith = differenceMaybeOrdGT -- isSubmapBy :: (a -> a -> Bool) -> OrdGT k a -> OrdGT k a -> Bool isSubmapBy = isSubmapOfOrdGT --------------------------