UNPACK the size field of SizedSeq
[ghc.git] / libraries / ghci / SizedSeq.hs
1 {-# LANGUAGE StandaloneDeriving, DeriveGeneric #-}
2 module SizedSeq
3 ( SizedSeq(..)
4 , emptySS
5 , addToSS
6 , addListToSS
7 , ssElts
8 , sizeSS
9 ) where
10
11 import Control.DeepSeq
12 import Data.Binary
13 import Data.List
14 import GHC.Generics
15
16 data SizedSeq a = SizedSeq {-# UNPACK #-} !Word [a]
17 deriving (Generic, Show)
18
19 instance Functor SizedSeq where
20 fmap f (SizedSeq sz l) = SizedSeq sz (fmap f l)
21
22 instance Foldable SizedSeq where
23 foldr f c ss = foldr f c (ssElts ss)
24
25 instance Traversable SizedSeq where
26 traverse f (SizedSeq sz l) = SizedSeq sz . reverse <$> traverse f (reverse l)
27
28 instance Binary a => Binary (SizedSeq a)
29
30 instance NFData a => NFData (SizedSeq a) where
31 rnf (SizedSeq _ xs) = rnf xs
32
33 emptySS :: SizedSeq a
34 emptySS = SizedSeq 0 []
35
36 addToSS :: SizedSeq a -> a -> SizedSeq a
37 addToSS (SizedSeq n r_xs) x = SizedSeq (n+1) (x:r_xs)
38
39 addListToSS :: SizedSeq a -> [a] -> SizedSeq a
40 addListToSS (SizedSeq n r_xs) xs
41 = SizedSeq (n + genericLength xs) (reverse xs ++ r_xs)
42
43 ssElts :: SizedSeq a -> [a]
44 ssElts (SizedSeq _ r_xs) = reverse r_xs
45
46 sizeSS :: SizedSeq a -> Word
47 sizeSS (SizedSeq n _) = n