{-# OPTIONS_GHC -fglasgow-exts -fno-warn-orphans #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Trie.General.CollectionsInstances.IntGT -- 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 'IntGT' type. ----------------------------------------------------------------------------- module Data.Trie.General.CollectionsInstances.IntGT ( ) where import Data.Trie.General.IntGT 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 (IntGT (Int,a)) (Int,a) where -- fold :: Monoid (Int,a) => IntGT (Int,a) -> (Int,a) fold igt = foldrElemsAscendingIntGT (\assoc b -> M.mappend assoc b) igt M.mempty -- foldMap :: Monoid m => ((Int,a) -> m) -> IntGT (Int,a) -> m foldMap f igt = foldrElemsAscendingIntGT (\assoc b -> M.mappend (f assoc) b) igt M.mempty -- foldr :: ((Int,a) -> b -> b) -> b -> IntGT (Int,a) -> b foldr f b0 igt = foldrElemsAscendingIntGT (\assoc b -> f assoc b) igt b0 -- foldl :: (b -> (Int,a) -> b) -> b -> IntGT (Int,a) -> b foldl f b0 igt = foldrElemsDescendingIntGT (\assoc b -> f b assoc) igt b0 {- ToDo: Implement properly. Meantime Foldable class has suitable defaults via lists. -- foldr1 :: ((Int,a) -> (Int,a) -> (Int,a)) -> IntGT (Int,a) -> (Int,a) -- foldl1 :: ((Int,a) -> (Int,a) -> (Int,a)) -> IntGT (Int,a) -> (Int,a) -} -- null :: IntGT (Int,a) -> Int null = isEmptyIntGT -- size :: IntGT (Int,a) -> Int size igt = ASINT(addSizeIntGT igt L(0)) -- isSingleton :: IntGT (Int,a) -> Bool isSingleton = isSingletonIntGT ------------------------------- --------------------------------- -- Data.Collections.Unfoldable -- --------------------------------- instance Coll.Unfoldable (IntGT (Int,a)) (Int,a) where -- insert :: (Int,a) -> IntGT (Int,a) -> IntGT (Int,a) insert assoc@(ASINT(k),_) igt = insertIntGT' (const assoc) k assoc igt -- Note use of strict insertIntGT' -- empty :: IntGT (Int,a) empty = emptyIntGT -- singleton :: (Int,a) -> IntGT (Int,a) singleton assoc@(ASINT(k),_) = singletonIntGT k assoc -- insertMany :: Foldable c' (Int,a) => c' -> IntGT (Int,a) -> IntGT (Int,a) insertMany c mp0 = Coll.foldr (\assoc@(ASINT(k),_) igt -> insertIntGT' (const assoc) k assoc igt) mp0 c -- ?? stricness?? l/r?? -- insertManySorted :: Foldable c' (Int,a) => c' -> IntGT (Int,a) -> IntGT (Int,a) insertManySorted c mp0 = Coll.foldr (\assoc@(ASINT(k),_) igt -> insertIntGT' (const assoc) k assoc igt) mp0 c -- How to implement efficiently ?? --------------------------------- --------------------------------- -- Data.Collections.Collection -- --------------------------------- instance Coll.Collection (IntGT (Int,a)) (Int,a) where -- filter :: ((Int,a) -> Bool) -> IntGT (Int,a) -> IntGT (Int,a) filter = filterIntGT --------------------------------- -------------------------- -- Data.Collections.Map -- -------------------------- instance (M.Monoid a) => Coll.Map (IntGT a) Int a where -- delete :: Int -> IntGT a -> IntGT a delete ASINT(k) igt = deleteIntGT k igt -- member :: Int -> IntGT a -> Bool member ASINT(k) igt = MB.isJust (lookupIntGT k igt) -- union :: IntGT a -> IntGT a -> IntGT a union = unionIntGT' (\x _ -> x) -- Note use of strict unionIntGT' -- intersection :: IntGT a -> IntGT a -> IntGT a intersection = intersectionIntGT' (\x _ -> x) -- Note use of strict intersectionIntGT' -- difference :: IntGT a -> IntGT a -> IntGT a difference = differenceIntGT -- isSubset :: IntGT a -> IntGT a -> Bool isSubset = isSubsetOfIntGT -- lookup :: Monad m => Int -> IntGT a -> m a lookup ASINT(k) igt = case lookupIntGT k igt of Just a -> return a Nothing -> fail "Data.Collections.Map.lookup: Key not found in IntGT." -- alter :: (Maybe a -> Maybe a) -> Int -> IntGT a -> IntGT a alter f ASINT(k) igt = alterIntGT f k igt -- insertWith :: (a -> a -> a) -> Int -> a -> IntGT a -> IntGT a insertWith f ASINT(k) a igt = insertIntGT (f a) k a igt -- fromFoldableWith :: Foldable l (Int,a) => (a -> a -> a) -> l -> IntGT a fromFoldableWith f l = Coll.foldr insrt emptyIntGT l -- Strictness ?? where insrt (ASINT(k),a) igt = insertIntGT (f a) k a igt -- foldGroups :: Foldable l (Int,b) => (b -> a -> a) -> a -> l -> IntGT a foldGroups f a0 l = Coll.foldr' insrt emptyIntGT l where insrt (ASINT(k),b) igt = insertIntGT (f b) k (f b a0) igt -- mapWithKey :: (Int -> a -> a) -> IntGT a -> IntGT a #ifdef __GLASGOW_HASKELL__ mapWithKey f igt = mapWithKeyIntGT (\i a -> f ASINT(i) a) igt #else mapWithKey = mapWithKeyIntGT #endif -- unionWith :: (a -> a -> a) -> IntGT a -> IntGT a -> IntGT a unionWith = unionIntGT -- intersectionWith :: (a -> a -> a) -> IntGT a -> IntGT a -> IntGT a intersectionWith = intersectionIntGT -- differenceWith :: (a -> a -> Maybe a) -> IntGT a -> IntGT a -> IntGT a differenceWith = differenceMaybeIntGT -- isSubmapBy :: (a -> a -> Bool) -> IntGT a -> IntGT a -> Bool isSubmapBy = isSubmapOfIntGT --------------------------