@@ -56,12 +56,8 @@ module Streamly.Internal.Data.SmallArray.Type
57import GHC.Exts hiding (toList)
57import GHC.Exts hiding (toList)
58import qualified GHC.Exts
58import qualified GHC.Exts
60import Control.Applicative
59import Control.Applicative
61import Control.Monad
60import Control.Monad
62−#if MIN_VERSION_base(4,9,0)
63−import qualified Control.Monad.Fail as Fail
65import Control.Monad.Fix
61import Control.Monad.Fix
66import Control.Monad.Primitive
62import Control.Monad.Primitive
67import Control.Monad.ST
63import Control.Monad.ST
@@ -69,22 +65,12 @@ import Control.Monad.Zip
69import Data.Data
65import Data.Data
70import Data.Foldable as Foldable
66import Data.Foldable as Foldable
71import Data.Functor.Identity
67import Data.Functor.Identity
72−#if !(MIN_VERSION_base(4,10,0))
68+import Data.Functor.Classes (Eq1(..),Ord1(..),Show1(..),Read1(..))
75−#if MIN_VERSION_base(4,9,0)
76−import qualified GHC.ST as GHCST
77−import qualified Data.Semigroup as Sem
79−import Text.ParserCombinators.ReadP
81−#if MIN_VERSION_base(4,9,0) && !MIN_VERSION_base(4,10,0)
70+import qualified Control.Monad.Fail as Fail
82−import GHC.Base (runRW#)
71+import qualified GHC.ST as GHCST
85−#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,4,0)
73+import Text.ParserCombinators.ReadP
86−import Data.Functor.Classes (Eq1(..),Ord1(..),Show1(..),Read1(..))
89data SmallArray a = SmallArray (SmallArray# a)
75data SmallArray a = SmallArray (SmallArray# a)
90 deriving Typeable
76 deriving Typeable
@@ -92,6 +78,7 @@ data SmallArray a = SmallArray (SmallArray# a)
92data SmallMutableArray s a = SmallMutableArray (SmallMutableArray# s a)
78data SmallMutableArray s a = SmallMutableArray (SmallMutableArray# s a)
93 deriving Typeable
79 deriving Typeable
95-- | Create a new small mutable array.
82-- | Create a new small mutable array.
96newSmallArray
83newSmallArray
97 :: PrimMonad m
84 :: PrimMonad m
@@ -318,14 +305,6 @@ mapSmallArray' f sa = createSmallArray (length sa) (die "mapSmallArray'" "imposs
318 let !y = f x
305 let !y = f x
319 writeSmallArray smb i y *> go (i+1)
306 writeSmallArray smb i y *> go (i+1)
320{-# INLINE mapSmallArray' #-}
307{-# INLINE mapSmallArray' #-}
322−#if !MIN_VERSION_base(4,9,0)
324− :: (forall s. ST s (SmallMutableArray s a))
326−runSmallArray m = runST $ m >>= unsafeFreezeSmallArray
329-- This low-level business is designed to work with GHC's worker-wrapper
308-- This low-level business is designed to work with GHC's worker-wrapper
330-- transformation. A lot of the time, we don't actually need an Array
309-- transformation. A lot of the time, we don't actually need an Array
331-- constructor. By putting it on the outside, and being careful about
310-- constructor. By putting it on the outside, and being careful about
@@ -348,8 +327,6 @@ runSmallArray# m = case runRW# $ \s ->
348unST :: ST s a -> State# s -> (# State# s, a #)
327unST :: ST s a -> State# s -> (# State# s, a #)
349unST (GHCST.ST f) = f
328unST (GHCST.ST f) = f
353-- See the comment on runSmallArray for why we use emptySmallArray#.
330-- See the comment on runSmallArray for why we use emptySmallArray#.
354createSmallArray
331createSmallArray
@@ -393,20 +370,11 @@ smallArrayLiftEq p sa1 sa2 = length sa1 == length sa2 && loop (length sa1 - 1)
393 | (# x #) <- indexSmallArray## sa1 i
370 | (# x #) <- indexSmallArray## sa1 i
394 , (# y #) <- indexSmallArray## sa2 i
371 , (# y #) <- indexSmallArray## sa2 i
395 = p x y && loop (i-1)
372 = p x y && loop (i-1)
397−#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,4,0)
399instance Eq1 SmallArray where
373instance Eq1 SmallArray where
400−#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,5,0)
401 liftEq = smallArrayLiftEq
374 liftEq = smallArrayLiftEq
403− eq1 = smallArrayLiftEq (==)
407instance Eq a => Eq (SmallArray a) where
376instance Eq a => Eq (SmallArray a) where
408 sa1 == sa2 = smallArrayLiftEq (==) sa1 sa2
377 sa1 == sa2 = smallArrayLiftEq (==) sa1 sa2
410instance Eq (SmallMutableArray s a) where
378instance Eq (SmallMutableArray s a) where
411 SmallMutableArray sma1# == SmallMutableArray sma2# =
379 SmallMutableArray sma1# == SmallMutableArray sma2# =
412 isTrue# (sameSmallMutableArray# sma1# sma2#)
380 isTrue# (sameSmallMutableArray# sma1# sma2#)
@@ -422,15 +390,8 @@ smallArrayLiftCompare elemCompare a1 a2 = loop 0
422 = elemCompare x1 x2 `mappend` loop (i+1)
390 = elemCompare x1 x2 `mappend` loop (i+1)
423 | otherwise = compare (length a1) (length a2)
391 | otherwise = compare (length a1) (length a2)
425−#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,4,0)
427instance Ord1 SmallArray where
393instance Ord1 SmallArray where
428−#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,5,0)
429 liftCompare = smallArrayLiftCompare
394 liftCompare = smallArrayLiftCompare
431− compare1 = smallArrayLiftCompare compare
435-- | Lexicographic ordering. Subject to change between major versions.
396-- | Lexicographic ordering. Subject to change between major versions.
436instance Ord a => Ord (SmallArray a) where
397instance Ord a => Ord (SmallArray a) where
@@ -668,11 +629,8 @@ instance Monad SmallArray where
668#if !(MIN_VERSION_base(4,13,0)) && MIN_VERSION_base(4,9,0)
629#if !(MIN_VERSION_base(4,13,0)) && MIN_VERSION_base(4,9,0)
669 fail = Fail.fail
630 fail = Fail.fail
672−#if MIN_VERSION_base(4,9,0)
673instance Fail.MonadFail SmallArray where
632instance Fail.MonadFail SmallArray where
674 fail _ = emptySmallArray
633 fail _ = emptySmallArray
677instance MonadPlus SmallArray where
635instance MonadPlus SmallArray where
678 mzero = empty
636 mzero = empty
@@ -715,24 +673,19 @@ instance MonadFix SmallArray where
715 sz = sizeofSmallArray (f err)
673 sz = sizeofSmallArray (f err)
716 err = error "mfix for Data.Primitive.SmallArray applied to strict function."
674 err = error "mfix for Data.Primitive.SmallArray applied to strict function."
718−#if MIN_VERSION_base(4,9,0)
719-- | @since 0.6.3.0
676-- | @since 0.6.3.0
720−instance Sem.Semigroup (SmallArray a) where
677+instance Semigroup (SmallArray a) where
721 (<>) = (<|>)
678 (<>) = (<|>)
722− sconcat = mconcat . toList
725instance Monoid (SmallArray a) where
680instance Monoid (SmallArray a) where
726 mempty = empty
681 mempty = empty
727−#if !(MIN_VERSION_base(4,11,0))
728− mappend = (<|>)
682+ mappend = (<>)
730 mconcat l = createSmallArray n (die "mconcat" "impossible") $ \ma ->
683 mconcat l = createSmallArray n (die "mconcat" "impossible") $ \ma ->
731 let go !_ [ ] = return ()
684 let go !_ [ ] = return ()
732 go off (a:as) =
685 go off (a:as) =
733 copySmallArray ma off a 0 (sizeofSmallArray a) >> go (off + sizeofSmallArray a) as
686 copySmallArray ma off a 0 (sizeofSmallArray a) >> go (off + sizeofSmallArray a) as
734− in go 0 l
687+ in go 0 l
735− where n = sum . fmap length $ l
688+ where n = sum . fmap length $ l
737instance IsList (SmallArray a) where
690instance IsList (SmallArray a) where
738 type Item (SmallArray a) = a
691 type Item (SmallArray a) = a
@@ -751,16 +704,8 @@ listLiftShowsPrec _ sl _ = sl
752instance Show a => Show (SmallArray a) where
705instance Show a => Show (SmallArray a) where
753 showsPrec = smallArrayLiftShowsPrec showsPrec showList
706 showsPrec = smallArrayLiftShowsPrec showsPrec showList
755−#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,4,0)
757instance Show1 SmallArray where
707instance Show1 SmallArray where
758−#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,5,0)
759 liftShowsPrec = smallArrayLiftShowsPrec
708 liftShowsPrec = smallArrayLiftShowsPrec
761− showsPrec1 = smallArrayLiftShowsPrec showsPrec showList
765smallArrayLiftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (SmallArray a)
710smallArrayLiftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (SmallArray a)
766smallArrayLiftReadsPrec _ listReadsPrec p = readParen (p > 10) . readP_to_S $ do
711smallArrayLiftReadsPrec _ listReadsPrec p = readParen (p > 10) . readP_to_S $ do
@@ -774,17 +719,8 @@ smallArrayLiftReadsPrec _ listReadsPrec p = readParen (p > 10) . readP_to_S $ do
774instance Read a => Read (SmallArray a) where
719instance Read a => Read (SmallArray a) where
775 readsPrec = smallArrayLiftReadsPrec readsPrec readList
720 readsPrec = smallArrayLiftReadsPrec readsPrec readList
777−#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,4,0)
779instance Read1 SmallArray where
722instance Read1 SmallArray where
780−#if MIN_VERSION_base(4,9,0) || MIN_VERSION_transformers(0,5,0)
781 liftReadsPrec = smallArrayLiftReadsPrec
723 liftReadsPrec = smallArrayLiftReadsPrec
783− readsPrec1 = smallArrayLiftReadsPrec readsPrec readList
789smallArrayDataType :: DataType
725smallArrayDataType :: DataType
790smallArrayDataType =
726smallArrayDataType =