module Biobase.Primary.Letter where
import Control.DeepSeq (NFData)
import Data.Aeson
import Data.Binary
import Data.Hashable (Hashable)
import Data.Ix (Ix(..))
import Data.Serialize (Serialize(..))
import Data.String (IsString(..))
import Data.Vector.Fusion.Stream.Monadic (map,Step(..))
import Data.Vector.Unboxed.Deriving
import GHC.Base (remInt,quotInt)
import GHC.Generics (Generic)
import Prelude hiding (map)
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Vector.Unboxed as VU
import Data.PrimitiveArray hiding (map)
newtype Letter t = Letter { getLetter :: Int }
deriving (Eq,Ord,Generic,Ix)
instance Binary (Letter t)
instance Serialize (Letter t)
instance NFData (Letter t)
type Primary t = VU.Vector (Letter t)
class LetterChar t where
letterChar :: Letter t -> Char
charLetter :: Char -> Letter t
class MkPrimary n t where
primary :: n -> Primary t
instance (MkPrimary (VU.Vector Char) t) => MkPrimary String t where
primary = primary . VU.fromList
instance MkPrimary (VU.Vector Char) t => MkPrimary T.Text t where
primary = primary . VU.fromList . T.unpack
instance MkPrimary (VU.Vector Char) t => MkPrimary TL.Text t where
primary = primary . VU.fromList . TL.unpack
instance MkPrimary (VU.Vector Char) t => MkPrimary BS.ByteString t where
primary = primary . VU.fromList . BS.unpack
instance MkPrimary (VU.Vector Char) t => MkPrimary BSL.ByteString t where
primary = primary . VU.fromList . BSL.unpack
instance (VU.Unbox (Letter t), IsString [Letter t]) => IsString (VU.Vector (Letter t)) where
fromString = VU.fromList . fromString
derivingUnbox "Letter"
[t| forall a . Letter a -> Int |] [| getLetter |] [| Letter |]
instance Hashable (Letter t)
instance Index (Letter l) where
linearIndex _ _ (Letter i) = i
smallestLinearIndex _ = error "still needed?"
largestLinearIndex (Letter h) = h
size _ (Letter h) = h+1
inBounds (Letter l) (Letter h) (Letter i) = l <= i && i <= h
instance IndexStream z => IndexStream (z:.Letter l) where
streamUp (ls:.Letter l) (hs:.Letter h) = flatten mk step $ streamUp ls hs
where mk z = return (z,l)
step (z,k)
| k > h = return $ Done
| otherwise = return $ Yield (z:.Letter k) (z,k+1)
streamDown (ls:.Letter l) (hs:.Letter h) = flatten mk step $ streamDown ls hs
where mk z = return (z,h)
step (z,k)
| k < l = return $ Done
| otherwise = return $ Yield (z:.Letter k) (z,k1)
instance IndexStream (Letter l) where
streamUp l h = map (\(Z:.k) -> k) $ streamUp (Z:.l) (Z:.h)
streamDown l h = map (\(Z:.k) -> k) $ streamDown (Z:.l) (Z:.h)