X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=Data%2FIntSet.hs;h=622082796a9eb459df059b02503b7c0677286d66;hb=68937167ecaa5ddaeb0420ad8c204902e09c508b;hp=5023df677c40390394a818b678b9561d3835b362;hpb=a7d561a002efa30619c88faa3f929bdc1967c206;p=haskell-directory.git diff --git a/Data/IntSet.hs b/Data/IntSet.hs index 5023df6..6220827 100644 --- a/Data/IntSet.hs +++ b/Data/IntSet.hs @@ -94,7 +94,7 @@ import Data.Bits import Data.Int import qualified Data.List as List -import Data.Monoid +import Data.Typeable {- -- just for testing @@ -103,6 +103,12 @@ import List (nub,sort) import qualified List -} +#if __GLASGOW_HASKELL__ +import Text.Read +import Data.Generics.Basics +import Data.Generics.Instances +#endif + #if __GLASGOW_HASKELL__ >= 503 import GHC.Word import GHC.Exts ( Word(..), Int(..), shiftRL# ) @@ -115,20 +121,8 @@ import Data.Word infixl 9 \\{-This comment teaches CPP correct behaviour -} -#if __HUGS__ -{-------------------------------------------------------------------- - Hugs: - * Older Hugs doesn't define 'Word'. - * Newer Hugs defines 'Word' in the Prelude but no operations. ---------------------------------------------------------------------} -type Nat = Word32 -- illegal on 64-bit platforms! -#else -{-------------------------------------------------------------------- - 'Standard' Haskell - * A "Nat" is a natural machine word (an unsigned Int) ---------------------------------------------------------------------} +-- A "Nat" is a natural machine word (an unsigned Int) type Nat = Word -#endif natFromInt :: Int -> Nat natFromInt i = fromIntegral i @@ -165,6 +159,23 @@ data IntSet = Nil type Prefix = Int type Mask = Int +#if __GLASGOW_HASKELL__ + +{-------------------------------------------------------------------- + A Data instance +--------------------------------------------------------------------} + +-- This instance preserves data abstraction at the cost of inefficiency. +-- We omit reflection services for the sake of data abstraction. + +instance Data IntSet where + gfoldl f z is = z fromList `f` (toList is) + toConstr _ = error "toConstr" + gunfold _ _ = error "gunfold" + dataTypeOf _ = mkNorepType "Data.IntSet.IntSet" + +#endif + {-------------------------------------------------------------------- Query --------------------------------------------------------------------} @@ -394,7 +405,7 @@ subsetCmp Nil Nil = EQ subsetCmp Nil t = LT -- | /O(n+m)/. Is this a subset? --- @(s1 `isSubsetOf` s2)@ tells whether s1 is a subset of s2. +-- @(s1 `isSubsetOf` s2)@ tells whether @s1@ is a subset of @s2@. isSubsetOf :: IntSet -> IntSet -> Bool isSubsetOf t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2) @@ -435,7 +446,7 @@ partition pred t Nil -> (Nil,Nil) --- | /O(log n)/. The expression (@split x set@) is a pair @(set1,set2)@ +-- | /O(log n)/. The expression (@'split' x set@) is a pair @(set1,set2)@ -- where all elements in @set1@ are lower than @x@ and all elements in -- @set2@ larger than @x@. -- @@ -454,24 +465,24 @@ split x t -- | /O(log n)/. Performs a 'split' but also returns whether the pivot -- element was found in the original set. -splitMember :: Int -> IntSet -> (Bool,IntSet,IntSet) +splitMember :: Int -> IntSet -> (IntSet,Bool,IntSet) splitMember x t = case t of Bin p m l r - | zero x m -> let (found,lt,gt) = splitMember x l in (found,lt,union gt r) - | otherwise -> let (found,lt,gt) = splitMember x r in (found,union l lt,gt) + | zero x m -> let (lt,found,gt) = splitMember x l in (lt,found,union gt r) + | otherwise -> let (lt,found,gt) = splitMember x r in (union l lt,found,gt) Tip y - | x>y -> (False,t,Nil) - | x (False,Nil,t) - | otherwise -> (True,Nil,Nil) - Nil -> (False,Nil,Nil) + | x>y -> (t,False,Nil) + | x (Nil,False,t) + | otherwise -> (Nil,True,Nil) + Nil -> (Nil,False,Nil) {---------------------------------------------------------------------- Map ----------------------------------------------------------------------} -- | /O(n*min(n,W))/. --- @map f s@ is the set obtained by applying @f@ to each element of @s@. +-- @'map' f s@ is the set obtained by applying @f@ to each element of @s@. -- -- It's worth noting that the size of the result may be smaller if, -- for some @(x,y)@, @x \/= y && f x == f y@ @@ -569,19 +580,11 @@ instance Ord IntSet where -- tentative implementation. See if more efficient exists. {-------------------------------------------------------------------- - Monoid ---------------------------------------------------------------------} - -instance Monoid IntSet where - mempty = empty - mappend = union - mconcat = unions - -{-------------------------------------------------------------------- Show --------------------------------------------------------------------} instance Show IntSet where - showsPrec d s = showSet (toList s) + showsPrec p xs = showParen (p > 10) $ + showString "fromList " . shows (toList xs) showSet :: [Int] -> ShowS showSet [] @@ -593,6 +596,31 @@ showSet (x:xs) showTail (x:xs) = showChar ',' . shows x . showTail xs {-------------------------------------------------------------------- + Read +--------------------------------------------------------------------} +instance Read IntSet where +#ifdef __GLASGOW_HASKELL__ + readPrec = parens $ prec 10 $ do + Ident "fromList" <- lexP + xs <- readPrec + return (fromList xs) + + readListPrec = readListPrecDefault +#else + readsPrec p = readParen (p > 10) $ \ r -> do + ("fromList",s) <- lex r + (xs,t) <- reads s + return (fromList xs,t) +#endif + +{-------------------------------------------------------------------- + Typeable +--------------------------------------------------------------------} + +#include "Typeable.h" +INSTANCE_TYPEABLE0(IntSet,intSetTc,"IntSet") + +{-------------------------------------------------------------------- Debugging --------------------------------------------------------------------} -- | /O(n)/. Show the tree that implements the set. The tree is shown @@ -602,10 +630,10 @@ showTree s = showTreeWith True False s -{- | /O(n)/. The expression (@showTreeWith hang wide map@) shows +{- | /O(n)/. The expression (@'showTreeWith' hang wide map@) shows the tree that implements the set. If @hang@ is - @True@, a /hanging/ tree is shown otherwise a rotated tree is shown. If - @wide@ is true, an extra wide version is shown. + 'True', a /hanging/ tree is shown otherwise a rotated tree is shown. If + @wide@ is 'True', an extra wide version is shown. -} showTreeWith :: Bool -> Bool -> IntSet -> String showTreeWith hang wide t