{-# LANGUAGE Trustworthy, MagicHash, UnboxedTuples, BangPatterns, TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, RoleAnnotations #-}

{- |
    Module      :  SDP.Prim.SBytes
    Copyright   :  (c) Andrey Mulik 2019
    License     :  BSD-style
    Maintainer  :  work.a.mulik@gmail.com
    Portability :  non-portable (GHC extensions)
    
    "SDP.Prim.SBytes" provides strict unboxed array pseudo-primitive types
    'SBytes#', 'STBytes#' and 'IOBytes#'.
-}
module SDP.Prim.SBytes
(
  -- * Exports
  module SDP.IndexedM,
  module SDP.Unboxed,
  module SDP.SortM,
  module SDP.Sort,
  
  -- * Preudo-primitive types
  MIOBytes# (..), IOBytes#, STBytes#, SBytes#,
  
  -- ** Unpack unboxed arrays
  fromSBytes#, packSBytes#, unpackSBytes#, offsetSBytes#,
  fromSTBytes#, packSTBytes#, unpackSTBytes#, offsetSTBytes#,
  
  -- ** Coerce unboxed arrays
  unsafeCoerceSBytes#, unsafeCoerceSTBytes#,
  
  -- ** Unsafe pointer conversions
  unsafeSBytesToPtr#, unsafePtrToSBytes#,
  
  -- ** Hash
  hashSBytesWith#
)
where

import Prelude ()
import SDP.SafePrelude
import SDP.IndexedM
import SDP.Unboxed
import SDP.SortM
import SDP.Sort
import SDP.Scan

import SDP.SortM.Tim

import qualified GHC.Exts as E
import GHC.Exts
  (
    ByteArray#, MutableByteArray#, State#, Int#, (+#), (-#), (==#),
    newByteArray#, unsafeFreezeByteArray#, sameMutableByteArray#
  )

import GHC.Types
import GHC.ST ( ST (..), STRep )

import Data.Default.Class
import Data.Typeable
import Data.Coerce
import Data.String
import Text.Read

import Foreign
  (
    Ptr, Storable, peekByteOff, peekElemOff, pokeByteOff, pokeElemOff,
    mallocBytes, callocArray
  )

import Control.Exception.SDP

default ()

--------------------------------------------------------------------------------

{- |
  'SBytes#' is immutable pseudo-primitive 'Int'-indexed strict unboxed array
  type.
  
  'SBytes#' isn't real Haskell primitive (like "GHC.Exts" types) but for
  reliability and stability, I made it inaccessible to direct work.
-}
data SBytes# e = SBytes#
          {-# UNPACK #-} !Int -- ^ Element count (not a real size)
          {-# UNPACK #-} !Int -- ^ Offset (in elements)
          !(ByteArray#)       -- ^ Real primitive byte array
  deriving ( Typeable )

type role SBytes# representational

--------------------------------------------------------------------------------

{- Eq instance. -}

instance (Unboxed e) => Eq (SBytes# e)
  where
    xs :: SBytes# e
xs@(SBytes# Int
c1 Int
_ ByteArray#
_) == :: SBytes# e -> SBytes# e -> Bool
== ys :: SBytes# e
ys@(SBytes# Int
c2 Int
_ ByteArray#
_) =
      let eq' :: Int -> Bool
eq' Int
i = Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
c1 Bool -> Bool -> Bool
|| (SBytes# e
xsSBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^Int
i) e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== (SBytes# e
ysSBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^Int
i) Bool -> Bool -> Bool
&& Int -> Bool
eq' (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      in  Int
c1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
c2 Bool -> Bool -> Bool
&& Int -> Bool
eq' Int
0

--------------------------------------------------------------------------------

{- Ord and Ord1 instances. -}

instance (Unboxed e, Ord e) => Ord (SBytes# e)
  where
    compare :: SBytes# e -> SBytes# e -> Ordering
compare xs :: SBytes# e
xs@(SBytes# Int
c1 Int
_ ByteArray#
_) ys :: SBytes# e
ys@(SBytes# Int
c2 Int
_ ByteArray#
_) = Int -> Ordering
cmp' Int
0
      where
        cmp' :: Int -> Ordering
cmp' Int
i = Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
c Bool -> Ordering -> Ordering -> Ordering
forall a. Bool -> a -> a -> a
? Int
c1 Compare Int
forall o. Ord o => Compare o
<=> Int
c2 (Ordering -> Ordering) -> Ordering -> Ordering
forall a b. (a -> b) -> a -> b
$ (SBytes# e
xsSBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^Int
i Compare e
forall o. Ord o => Compare o
<=> SBytes# e
ysSBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^Int
i) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> Int -> Ordering
cmp' (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
        c :: Int
c = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
c1 Int
c2

--------------------------------------------------------------------------------

{- Show and Read instances. -}

instance (Unboxed e, Show e) => Show (SBytes# e) where showsPrec :: Int -> SBytes# e -> ShowS
showsPrec Int
p = Int -> [e] -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
p ([e] -> ShowS) -> (SBytes# e -> [e]) -> SBytes# e -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> [e]
forall l e. Linear l e => l -> [e]
listL

instance (Unboxed e, Read e) => Read (SBytes# e) where readPrec :: ReadPrec (SBytes# e)
readPrec = [e] -> SBytes# e
forall l e. Linear l e => [e] -> l
fromList ([e] -> SBytes# e) -> ReadPrec [e] -> ReadPrec (SBytes# e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadPrec [e]
forall a. Read a => ReadPrec a
readPrec

--------------------------------------------------------------------------------

{- Overloaded Lists and Strings support. -}

instance IsString (SBytes# Char) where fromString :: String -> SBytes# Char
fromString = String -> SBytes# Char
forall l e. Linear l e => [e] -> l
fromList

instance (Unboxed e) => E.IsList (SBytes# e)
  where
    type Item (SBytes# e) = e
    
    fromListN :: Int -> [Item (SBytes# e)] -> SBytes# e
fromListN = Int -> [Item (SBytes# e)] -> SBytes# e
forall l e. Linear l e => Int -> [e] -> l
fromListN
    fromList :: [Item (SBytes# e)] -> SBytes# e
fromList  = [Item (SBytes# e)] -> SBytes# e
forall l e. Linear l e => [e] -> l
fromList
    toList :: SBytes# e -> [Item (SBytes# e)]
toList    = SBytes# e -> [Item (SBytes# e)]
forall l e. Linear l e => l -> [e]
listL

--------------------------------------------------------------------------------

{- Semigroup, Monoid, Nullable, Default and Estimate instances. -}

instance Nullable (SBytes# e)
  where
    lzero :: SBytes# e
lzero = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ (STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done (STBytes# s e -> ST s (SBytes# e))
-> ST s (STBytes# s e) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (ST s (STBytes# s e) -> ST s (SBytes# e))
-> ST s (STBytes# s e) -> ST s (SBytes# e)
forall a b. (a -> b) -> a -> b
$ STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall s a. STRep s a -> ST s a
ST (STRep s (STBytes# s e) -> ST s (STBytes# s e))
-> STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
0# State# s
s1# of
      (# State# s
s2#, MutableByteArray# s
marr# #) -> (# State# s
s2#, Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
0 Int
0 MutableByteArray# s
marr# #)
    
    isNull :: SBytes# e -> Bool
isNull SBytes# e
es = case SBytes# e
es of {(SBytes# Int
0 Int
_ ByteArray#
_) -> Bool
True; SBytes# e
_ -> Bool
False}

instance (Unboxed e) => Semigroup (SBytes# e) where <> :: SBytes# e -> SBytes# e -> SBytes# e
(<>) = SBytes# e -> SBytes# e -> SBytes# e
forall l e. Linear l e => l -> l -> l
(++)
instance (Unboxed e) => Monoid    (SBytes# e) where mempty :: SBytes# e
mempty = SBytes# e
forall e. Nullable e => e
Z

instance Default (SBytes# e)
  where
    def :: SBytes# e
def = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ (STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done (STBytes# s e -> ST s (SBytes# e))
-> ST s (STBytes# s e) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (ST s (STBytes# s e) -> ST s (SBytes# e))
-> ST s (STBytes# s e) -> ST s (SBytes# e)
forall a b. (a -> b) -> a -> b
$ STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall s a. STRep s a -> ST s a
ST (STRep s (STBytes# s e) -> ST s (STBytes# s e))
-> STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
0# State# s
s1# of
      (# State# s
s2#, MutableByteArray# s
marr# #) -> (# State# s
s2#, Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
0 Int
0 MutableByteArray# s
marr# #)

instance Estimate (SBytes# e)
  where
    <==> :: Compare (SBytes# e)
(<==>) = Compare Int -> (SBytes# e -> Int) -> Compare (SBytes# e)
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Compare Int
forall o. Ord o => Compare o
(<=>) SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<=. :: SBytes# e -> SBytes# e -> Bool
(.<=.) = (Int -> Int -> Bool)
-> (SBytes# e -> Int) -> SBytes# e -> SBytes# e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<=)  SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>=. :: SBytes# e -> SBytes# e -> Bool
(.>=.) = (Int -> Int -> Bool)
-> (SBytes# e -> Int) -> SBytes# e -> SBytes# e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>=)  SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>. :: SBytes# e -> SBytes# e -> Bool
(.>.)  = (Int -> Int -> Bool)
-> (SBytes# e -> Int) -> SBytes# e -> SBytes# e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>)   SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<. :: SBytes# e -> SBytes# e -> Bool
(.<.)  = (Int -> Int -> Bool)
-> (SBytes# e -> Int) -> SBytes# e -> SBytes# e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<)   SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    
    <.=> :: SBytes# e -> Int -> Ordering
(<.=>) = Compare Int
forall o. Ord o => Compare o
(<=>) Compare Int -> (SBytes# e -> Int) -> SBytes# e -> Int -> Ordering
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>= :: SBytes# e -> Int -> Bool
(.>=)  = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>=)  (Int -> Int -> Bool)
-> (SBytes# e -> Int) -> SBytes# e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<= :: SBytes# e -> Int -> Bool
(.<=)  = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<=)  (Int -> Int -> Bool)
-> (SBytes# e -> Int) -> SBytes# e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .> :: SBytes# e -> Int -> Bool
(.>)   = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>)   (Int -> Int -> Bool)
-> (SBytes# e -> Int) -> SBytes# e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .< :: SBytes# e -> Int -> Bool
(.<)   = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<)   (Int -> Int -> Bool)
-> (SBytes# e -> Int) -> SBytes# e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf

--------------------------------------------------------------------------------

{- Linear, Split and Bordered instances. -}

instance (Unboxed e) => Linear (SBytes# e) e
  where
    toHead :: e -> SBytes# e -> SBytes# e
toHead e
e (SBytes# (I# Int#
c#) (I# Int#
o#) ByteArray#
arr#) = let n# :: Int#
n# = Int#
c# Int# -> Int# -> Int#
+# Int#
1# in (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ STRep s (SBytes# e) -> ST s (SBytes# e)
forall s a. STRep s a -> ST s a
ST (STRep s (SBytes# e) -> ST s (SBytes# e))
-> STRep s (SBytes# e) -> ST s (SBytes# e)
forall a b. (a -> b) -> a -> b
$
      \ State# s
s1# -> case e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e s.
Unboxed e =>
e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
newUnboxed' e
e Int#
n# State# s
s1# of
        (# State# s
s2#, MutableByteArray# s
marr# #) -> case e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e s.
Unboxed e =>
e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
copyUnboxed# e
e ByteArray#
arr# Int#
o# MutableByteArray# s
marr# Int#
1# Int#
c# State# s
s2# of
          State# s
s3# -> case MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# s
marr# State# s
s3# of
            (# State# s
s4#, ByteArray#
res# #) -> (# State# s
s4#, Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int# -> Int
I# Int#
n#) Int
0 ByteArray#
res# #)
    
    toLast :: SBytes# e -> e -> SBytes# e
toLast (SBytes# (I# Int#
c#) (I# Int#
o#) ByteArray#
arr#) e
e = let n# :: Int#
n# = Int#
c# Int# -> Int# -> Int#
+# Int#
1# in (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ STRep s (SBytes# e) -> ST s (SBytes# e)
forall s a. STRep s a -> ST s a
ST (STRep s (SBytes# e) -> ST s (SBytes# e))
-> STRep s (SBytes# e) -> ST s (SBytes# e)
forall a b. (a -> b) -> a -> b
$
      \ State# s
s1# -> case e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e s.
Unboxed e =>
e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
newUnboxed' e
e Int#
n# State# s
s1# of
        (# State# s
s2#, MutableByteArray# s
marr# #) -> case e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e s.
Unboxed e =>
e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
copyUnboxed# e
e ByteArray#
arr# Int#
o# MutableByteArray# s
marr# Int#
0# Int#
c# State# s
s2# of
          State# s
s3# -> case MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# s
marr# State# s
s3# of
            (# State# s
s4#, ByteArray#
res# #) -> (# State# s
s4#, Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int# -> Int
I# Int#
n#) Int
0 ByteArray#
res# #)
    
    head :: SBytes# e -> e
head SBytes# e
es = SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
0
    last :: SBytes# e -> e
last es :: SBytes# e
es@(SBytes# Int
c Int
_ ByteArray#
_) = SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    
    tail :: SBytes# e -> SBytes# e
tail (SBytes# Int
c Int
o ByteArray#
arr#) = Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) ByteArray#
arr#
    init :: SBytes# e -> SBytes# e
init (SBytes# Int
c Int
o ByteArray#
arr#) = Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int
o ByteArray#
arr#
    
    fromList :: [e] -> SBytes# e
fromList = [e] -> SBytes# e
forall l e (f :: * -> *). (Linear l e, Foldable f) => f e -> l
fromFoldable
    
    fromListN :: Int -> [e] -> SBytes# e
fromListN  Int
n [e]
es = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ Int -> [e] -> ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => Int -> [e] -> m l
newLinearN  Int
n [e]
es ST s (STBytes# s e)
-> (STBytes# s e -> ST s (SBytes# e)) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done
    fromFoldable :: f e -> SBytes# e
fromFoldable f e
es = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ f e -> ST s (STBytes# s e)
forall (m :: * -> *) l e (f :: * -> *).
(LinearM m l e, Foldable f) =>
f e -> m l
fromFoldableM f e
es ST s (STBytes# s e)
-> (STBytes# s e -> ST s (SBytes# e)) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done
    
    single :: e -> SBytes# e
single e
e = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ Int -> e -> ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => Int -> e -> m l
filled Int
1 e
e ST s (STBytes# s e)
-> (STBytes# s e -> ST s (SBytes# e)) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done
    
    xs :: SBytes# e
xs@(SBytes# (I# Int#
n1#) (I# Int#
o1#) ByteArray#
arr1#) ++ :: SBytes# e -> SBytes# e -> SBytes# e
++ SBytes# (I# Int#
n2#) (I# Int#
o2#) ByteArray#
arr2# =
      let n# :: Int#
n# = Int#
n1# Int# -> Int# -> Int#
+# Int#
n2# in (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ STRep s (SBytes# e) -> ST s (SBytes# e)
forall s a. STRep s a -> ST s a
ST (STRep s (SBytes# e) -> ST s (SBytes# e))
-> STRep s (SBytes# e) -> ST s (SBytes# e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case SBytes# e
-> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
pnewUnboxed SBytes# e
xs Int#
n# State# s
s1# of
        (# State# s
s2#, MutableByteArray# s
marr# #) -> case SBytes# e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
pcopyUnboxed SBytes# e
xs ByteArray#
arr1# Int#
o1# MutableByteArray# s
marr# Int#
0# Int#
n1# State# s
s2# of
          State# s
s3# -> case SBytes# e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
pcopyUnboxed SBytes# e
xs ByteArray#
arr2# Int#
o2# MutableByteArray# s
marr# Int#
n1# Int#
n2# State# s
s3# of
            State# s
s4# -> case MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# s
marr# State# s
s4# of
              (# State# s
s5#, ByteArray#
arr# #) -> (# State# s
s5#, Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int# -> Int
I# Int#
n#) Int
0 ByteArray#
arr# #)
    
    force :: SBytes# e -> SBytes# e
force es :: SBytes# e
es@(SBytes# n :: Int
n@(I# Int#
n#) (I# Int#
o#) ByteArray#
bytes#) =
      Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# Int
n Int
0 (SBytes# e -> ByteArray# -> Int# -> Int# -> ByteArray#
forall e (proxy :: * -> *).
Unboxed e =>
proxy e -> ByteArray# -> Int# -> Int# -> ByteArray#
cloneUnboxed1# SBytes# e
es ByteArray#
bytes# Int#
n# Int#
o#)
    
    {-# INLINE (!^) #-}
    !^ :: SBytes# e -> Int -> e
(!^) (SBytes# Int
_ (I# Int#
o#) ByteArray#
arr#) = \ (I# Int#
i#) -> ByteArray#
arr# ByteArray# -> Int# -> e
forall e. Unboxed e => ByteArray# -> Int# -> e
!# (Int#
i# Int# -> Int# -> Int#
+# Int#
o#)
    
    write :: SBytes# e -> Int -> e -> SBytes# e
write SBytes# e
es Int
n e
e = Bool -> Bool
not (SBytes# e -> Int -> Bool
forall b i. Bordered b i => b -> i -> Bool
indexIn SBytes# e
es Int
n) Bool -> SBytes# e -> SBytes# e -> SBytes# e
forall a. Bool -> a -> a -> a
? SBytes# e
es (SBytes# e -> SBytes# e) -> SBytes# e -> SBytes# e
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ do
      STBytes# s e
es' <- SBytes# e -> ST s (STBytes# s e)
forall (m :: * -> *) v v'. Thaw m v v' => v -> m v'
thaw SBytes# e
es
      STBytes# s e -> Int -> e -> ST s ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM STBytes# s e
es' Int
n e
e
      STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done STBytes# s e
es'
    
    replicate :: Int -> e -> SBytes# e
replicate Int
n e
e = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ Int -> e -> ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => Int -> e -> m l
filled Int
n e
e ST s (STBytes# s e)
-> (STBytes# s e -> ST s (SBytes# e)) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done
    
    listL :: SBytes# e -> [e]
listL = (e -> [e] -> [e]) -> [e] -> SBytes# e -> [e]
forall l e b. Linear l e => (e -> b -> b) -> b -> l -> b
o_foldr (:) []
    listR :: SBytes# e -> [e]
listR = (e -> [e] -> [e]) -> [e] -> e -> [e]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (:) ([e] -> e -> [e]) -> [e] -> SBytes# e -> [e]
forall l e b. Linear l e => (b -> e -> b) -> b -> l -> b
`o_foldl` []
    
    concat :: f (SBytes# e) -> SBytes# e
concat f (SBytes# e)
ess = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ do
      let n :: Int
n = (SBytes# e -> Int -> Int) -> Int -> f (SBytes# e) -> Int
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr' (Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) (Int -> Int -> Int)
-> (SBytes# e -> Int) -> SBytes# e -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf) Int
0 f (SBytes# e)
ess
      marr :: STBytes# s e
marr@(STBytes# Int
_ Int
_ MutableByteArray# s
marr#) <- Int -> ST s (STBytes# s e)
forall e s. Unboxed e => Int -> ST s (STBytes# s e)
alloc Int
n
      
      let
        write# :: SBytes# e -> Int -> ST s Int
write# (SBytes# c :: Int
c@(I# Int#
c#) (I# Int#
o#) ByteArray#
arr#) i :: Int
i@(I# Int#
i#) = STRep s Int -> ST s Int
forall s a. STRep s a -> ST s a
ST (STRep s Int -> ST s Int) -> STRep s Int -> ST s Int
forall a b. (a -> b) -> a -> b
$
          \ State# s
s2# -> case f (SBytes# e)
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e (m :: * -> *) (proxy :: * -> *) s.
Unboxed e =>
m (proxy e)
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
pcopyUnboxed1 f (SBytes# e)
ess ByteArray#
arr# Int#
o# MutableByteArray# s
marr# Int#
i# Int#
c# State# s
s2# of
            State# s
s3# -> (# State# s
s3#, Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
c #)
      
      (ST s Int -> SBytes# e -> ST s Int)
-> ST s Int -> f (SBytes# e) -> ST s Int
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl ((SBytes# e -> ST s Int -> ST s Int)
-> ST s Int -> SBytes# e -> ST s Int
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((SBytes# e -> ST s Int -> ST s Int)
 -> ST s Int -> SBytes# e -> ST s Int)
-> (SBytes# e -> ST s Int -> ST s Int)
-> ST s Int
-> SBytes# e
-> ST s Int
forall a b. (a -> b) -> a -> b
$ (Int -> ST s Int) -> ST s Int -> ST s Int
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
(=<<) ((Int -> ST s Int) -> ST s Int -> ST s Int)
-> (SBytes# e -> Int -> ST s Int)
-> SBytes# e
-> ST s Int
-> ST s Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> Int -> ST s Int
write#) (Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0) f (SBytes# e)
ess ST s Int -> ST s (SBytes# e) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done STBytes# s e
marr
    
    reverse :: SBytes# e -> SBytes# e
reverse SBytes# e
es = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ SBytes# e -> ST s (STBytes# s e)
forall (m :: * -> *) v i e v' j.
(IndexedM m v i e, Indexed v' j e) =>
v' -> m v
fromIndexed' SBytes# e
es ST s (STBytes# s e)
-> (STBytes# s e -> ST s (STBytes# s e)) -> ST s (STBytes# s e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => l -> m l
reversed ST s (STBytes# s e)
-> (STBytes# s e -> ST s (SBytes# e)) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done
    
    select :: (e -> Maybe a) -> SBytes# e -> [a]
select  e -> Maybe a
f = (e -> [a] -> [a]) -> [a] -> SBytes# e -> [a]
forall l e b. Linear l e => (e -> b -> b) -> b -> l -> b
o_foldr (\ e
o [a]
es -> case e -> Maybe a
f e
o of {Just a
e -> a
e a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
es; Maybe a
_ -> [a]
es}) []
    extract :: (e -> Maybe a) -> SBytes# e -> ([a], SBytes# e)
extract e -> Maybe a
f =
      let g :: e -> ([a], [e]) -> ([a], [e])
g e
o = ([e] -> [e]) -> ([a], [e]) -> ([a], [e])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (e
o e -> [e] -> [e]
forall a. a -> [a] -> [a]
:) (([a], [e]) -> ([a], [e]))
-> (a -> ([a], [e]) -> ([a], [e]))
-> Maybe a
-> ([a], [e])
-> ([a], [e])
forall b a. b -> (a -> b) -> Maybe a -> b
`maybe` (([a] -> [a]) -> ([a], [e]) -> ([a], [e])
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (([a] -> [a]) -> ([a], [e]) -> ([a], [e]))
-> (a -> [a] -> [a]) -> a -> ([a], [e]) -> ([a], [e])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:)) (Maybe a -> ([a], [e]) -> ([a], [e]))
-> Maybe a -> ([a], [e]) -> ([a], [e])
forall a b. (a -> b) -> a -> b
$ e -> Maybe a
f e
o
      in  ([e] -> SBytes# e) -> ([a], [e]) -> ([a], SBytes# e)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second [e] -> SBytes# e
forall l e. Linear l e => [e] -> l
fromList (([a], [e]) -> ([a], SBytes# e))
-> (SBytes# e -> ([a], [e])) -> SBytes# e -> ([a], SBytes# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> ([a], [e]) -> ([a], [e]))
-> ([a], [e]) -> SBytes# e -> ([a], [e])
forall l e b. Linear l e => (e -> b -> b) -> b -> l -> b
o_foldr e -> ([a], [e]) -> ([a], [e])
g ([], [])
    
    selects :: f (e -> Maybe a) -> SBytes# e -> ([[a]], SBytes# e)
selects f (e -> Maybe a)
fs = ([e] -> SBytes# e) -> ([[a]], [e]) -> ([[a]], SBytes# e)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second [e] -> SBytes# e
forall l e. Linear l e => [e] -> l
fromList (([[a]], [e]) -> ([[a]], SBytes# e))
-> (SBytes# e -> ([[a]], [e])) -> SBytes# e -> ([[a]], SBytes# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (e -> Maybe a) -> [e] -> ([[a]], [e])
forall l e (f :: * -> *) a.
(Linear l e, Foldable f) =>
f (e -> Maybe a) -> l -> ([[a]], l)
selects f (e -> Maybe a)
fs ([e] -> ([[a]], [e]))
-> (SBytes# e -> [e]) -> SBytes# e -> ([[a]], [e])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> [e]
forall l e. Linear l e => l -> [e]
listL
    
    ofoldr :: (Int -> e -> b -> b) -> b -> SBytes# e -> b
ofoldr Int -> e -> b -> b
f b
base = \ arr :: SBytes# e
arr@(SBytes# Int
c Int
_ ByteArray#
_) ->
      let go :: Int -> b
go Int
i = Int
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> b -> b -> b
forall a. Bool -> a -> a -> a
? b
base (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ Int -> e -> b -> b
f Int
i (SBytes# e
arr SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i) (Int -> b
go (Int -> b) -> Int -> b
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      in  Int -> b
go Int
0
    
    ofoldl :: (Int -> b -> e -> b) -> b -> SBytes# e -> b
ofoldl Int -> b -> e -> b
f b
base = \ arr :: SBytes# e
arr@(SBytes# Int
c Int
_ ByteArray#
_) ->
      let go :: Int -> b
go Int
i = -Int
1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> b -> b -> b
forall a. Bool -> a -> a -> a
? b
base (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ Int -> b -> e -> b
f Int
i (Int -> b
go (Int -> b) -> Int -> b
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (SBytes# e
arr SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i)
      in  Int -> b
go (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    
    o_foldr :: (e -> b -> b) -> b -> SBytes# e -> b
o_foldr e -> b -> b
f b
base = \ arr :: SBytes# e
arr@(SBytes# Int
c Int
_ ByteArray#
_) ->
      let go :: Int -> b
go Int
i = Int
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> b -> b -> b
forall a. Bool -> a -> a -> a
? b
base (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ e -> b -> b
f (SBytes# e
arr SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i) (Int -> b
go (Int -> b) -> Int -> b
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      in  Int -> b
go Int
0
    
    o_foldl :: (b -> e -> b) -> b -> SBytes# e -> b
o_foldl b -> e -> b
f b
base = \ arr :: SBytes# e
arr@(SBytes# Int
c Int
_ ByteArray#
_) ->
      let go :: Int -> b
go Int
i = -Int
1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> b -> b -> b
forall a. Bool -> a -> a -> a
? b
base (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ b -> e -> b
f (Int -> b
go (Int -> b) -> Int -> b
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (SBytes# e
arr SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i)
      in  Int -> b
go (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)

instance (Unboxed e) => Split (SBytes# e) e
  where
    -- | O(1) 'take', O(1) memory.
    take :: Int -> SBytes# e -> SBytes# e
take Int
n es :: SBytes# e
es@(SBytes# Int
c Int
o ByteArray#
arr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = SBytes# e
forall e. Nullable e => e
Z
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = SBytes# e
es
      | Bool
True = Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# Int
n Int
o ByteArray#
arr#
    
    -- | O(1) 'drop', O(1) memory.
    drop :: Int -> SBytes# e -> SBytes# e
drop Int
n es :: SBytes# e
es@(SBytes# Int
c Int
o ByteArray#
arr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = SBytes# e
es
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = SBytes# e
forall e. Nullable e => e
Z
      |  Bool
True  = Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n) ByteArray#
arr#
    
    -- | O(1) 'split', O(1) memory.
    split :: Int -> SBytes# e -> (SBytes# e, SBytes# e)
split Int
n es :: SBytes# e
es@(SBytes# Int
c Int
o ByteArray#
arr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (SBytes# e
forall e. Nullable e => e
Z, SBytes# e
es)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = (SBytes# e
es, SBytes# e
forall e. Nullable e => e
Z)
      |  Bool
True  = (Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# Int
n Int
o ByteArray#
arr#, Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n) ByteArray#
arr#)
    
    -- | O(1) 'keep', O(1) memory.
    keep :: Int -> SBytes# e -> SBytes# e
keep Int
n es :: SBytes# e
es@(SBytes# Int
c Int
o ByteArray#
arr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = SBytes# e
forall e. Nullable e => e
Z
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = SBytes# e
es
      |  Bool
True  = Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# Int
n (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) ByteArray#
arr#
    
    -- | O(1) 'sans', O(1) memory.
    sans :: Int -> SBytes# e -> SBytes# e
sans Int
n es :: SBytes# e
es@(SBytes# Int
c Int
o ByteArray#
arr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = SBytes# e
es
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = SBytes# e
forall e. Nullable e => e
Z
      |  Bool
True  = Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) Int
o ByteArray#
arr#
    
    -- | O(1) 'divide', O(1) memory.
    divide :: Int -> SBytes# e -> (SBytes# e, SBytes# e)
divide Int
n es :: SBytes# e
es@(SBytes# Int
c Int
o ByteArray#
arr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (SBytes# e
forall e. Nullable e => e
Z, SBytes# e
es)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = (SBytes# e
es, SBytes# e
forall e. Nullable e => e
Z)
      |  Bool
True  = (Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# Int
n (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) ByteArray#
arr#, Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) Int
o ByteArray#
arr#)
    
    splitsBy :: (e -> Bool) -> SBytes# e -> [SBytes# e]
splitsBy e -> Bool
f SBytes# e
es = (e -> Bool) -> SBytes# e -> SBytes# e
forall s e. Split s e => (e -> Bool) -> s -> s
dropWhile e -> Bool
f (SBytes# e -> SBytes# e) -> [SBytes# e] -> [SBytes# e]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> e -> Bool
f (e -> Bool) -> SBytes# e -> [Int]
forall map key e. Map map key e => (e -> Bool) -> map -> [key]
*$ SBytes# e
es [Int] -> SBytes# e -> [SBytes# e]
forall s e (f :: * -> *).
(Split s e, Foldable f) =>
f Int -> s -> [s]
`parts` SBytes# e
es
    
    combo :: Equal e -> SBytes# e -> Int
combo Equal e
_                  SBytes# e
Z = Int
0
    combo Equal e
f es :: SBytes# e
es@(SBytes# Int
n Int
_ ByteArray#
_) =
      let go :: e -> Int -> Int
go e
e Int
i = let e' :: e
e' = SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i in Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n Bool -> Bool -> Bool
|| Bool -> Bool
not (Equal e
f e
e e
e') Bool -> Int -> Int -> Int
forall a. Bool -> a -> a -> a
? Int
i (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ e -> Int -> Int
go e
e' (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      in  e -> Int -> Int
go (SBytes# e -> e
forall l e. Linear l e => l -> e
head SBytes# e
es) Int
1
    
    justifyL :: Int -> e -> SBytes# e -> SBytes# e
justifyL n :: Int
n@(I# Int#
n#) e
e es :: SBytes# e
es@(SBytes# c :: Int
c@(I# Int#
c#) (I# Int#
o#) ByteArray#
src#) = case Int
c Compare Int
forall o. Ord o => Compare o
<=> Int
n of
      Ordering
EQ -> SBytes# e
es
      Ordering
GT -> Int -> SBytes# e -> SBytes# e
forall s e. Split s e => Int -> s -> s
take Int
n SBytes# e
es
      Ordering
LT -> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ STRep s (SBytes# e) -> ST s (SBytes# e)
forall s a. STRep s a -> ST s a
ST (STRep s (SBytes# e) -> ST s (SBytes# e))
-> STRep s (SBytes# e) -> ST s (SBytes# e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e s.
Unboxed e =>
e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
newUnboxed' e
e Int#
n# State# s
s1# of
        (# State# s
s2#, MutableByteArray# s
mbytes# #) -> case e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e s.
Unboxed e =>
e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
copyUnboxed# e
e ByteArray#
src# Int#
o# MutableByteArray# s
mbytes# Int#
0# Int#
c# State# s
s2# of
          State# s
s3# -> case MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# s
mbytes# State# s
s3# of
            (# State# s
s4#, ByteArray#
bytes# #) -> (# State# s
s4#, Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# Int
n Int
0 ByteArray#
bytes# #)
    
    justifyR :: Int -> e -> SBytes# e -> SBytes# e
justifyR n :: Int
n@(I# Int#
n#) e
e es :: SBytes# e
es@(SBytes# c :: Int
c@(I# Int#
c#) (I# Int#
o#) ByteArray#
src#) = case Int
c Compare Int
forall o. Ord o => Compare o
<=> Int
n of
      Ordering
EQ -> SBytes# e
es
      Ordering
GT -> Int -> SBytes# e -> SBytes# e
forall s e. Split s e => Int -> s -> s
take Int
n SBytes# e
es
      Ordering
LT -> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ STRep s (SBytes# e) -> ST s (SBytes# e)
forall s a. STRep s a -> ST s a
ST (STRep s (SBytes# e) -> ST s (SBytes# e))
-> STRep s (SBytes# e) -> ST s (SBytes# e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e s.
Unboxed e =>
e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
newUnboxed' e
e Int#
n# State# s
s1# of
        (# State# s
s2#, MutableByteArray# s
mbytes# #) -> case e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e s.
Unboxed e =>
e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
copyUnboxed# e
e ByteArray#
src# Int#
o# MutableByteArray# s
mbytes# (Int#
n# Int# -> Int# -> Int#
-# Int#
c#) Int#
c# State# s
s2# of
          State# s
s3# -> case MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# s
mbytes# State# s
s3# of
            (# State# s
s4#, ByteArray#
bytes# #) -> (# State# s
s4#, Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# Int
n Int
0 ByteArray#
bytes# #)
    
    each :: Int -> SBytes# e -> SBytes# e
each Int
n es :: SBytes# e
es@(SBytes# Int
c Int
_ ByteArray#
_) =
      let go :: Int -> [e]
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
c Bool -> [e] -> [e] -> [e]
forall a. Bool -> a -> a -> a
? SBytes# e
esSBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^Int
i e -> [e] -> [e]
forall a. a -> [a] -> [a]
: Int -> [e]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n) ([e] -> [e]) -> [e] -> [e]
forall a b. (a -> b) -> a -> b
$ []
      in  case Int
n Compare Int
forall o. Ord o => Compare o
<=> Int
1 of {Ordering
LT -> SBytes# e
forall e. Nullable e => e
Z; Ordering
EQ -> SBytes# e
es; Ordering
GT -> [e] -> SBytes# e
forall l e. Linear l e => [e] -> l
fromList ([e] -> SBytes# e) -> [e] -> SBytes# e
forall a b. (a -> b) -> a -> b
$ Int -> [e]
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)}
    
    isPrefixOf :: SBytes# e -> SBytes# e -> Bool
isPrefixOf xs :: SBytes# e
xs@(SBytes# Int
c1 Int
_ ByteArray#
_) ys :: SBytes# e
ys@(SBytes# Int
c2 Int
_ ByteArray#
_) =
      let eq :: Int -> Bool
eq Int
i = Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
c1 Bool -> Bool -> Bool
|| (SBytes# e
xs SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i) Equal e
forall a. Eq a => a -> a -> Bool
== (SBytes# e
ys SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i) Bool -> Bool -> Bool
&& Int -> Bool
eq (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      in  Int
c1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
c2 Bool -> Bool -> Bool
&& Int -> Bool
eq Int
0
    
    isSuffixOf :: SBytes# e -> SBytes# e -> Bool
isSuffixOf xs :: SBytes# e
xs@(SBytes# Int
c1 Int
_ ByteArray#
_) ys :: SBytes# e
ys@(SBytes# Int
c2 Int
_ ByteArray#
_) =
      let eq :: Int -> Int -> Bool
eq Int
i Int
j = Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
c1 Bool -> Bool -> Bool
|| (SBytes# e
xs SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i) Equal e
forall a. Eq a => a -> a -> Bool
== (SBytes# e
ys SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
j) Bool -> Bool -> Bool
&& Int -> Int -> Bool
eq (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      in  Int
c1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
c2 Bool -> Bool -> Bool
&& Int -> Int -> Bool
eq Int
0 (Int
c2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
c1)
    
    selectWhile :: (e -> Maybe a) -> SBytes# e -> [a]
selectWhile e -> Maybe a
f es :: SBytes# e
es@(SBytes# Int
c Int
_ ByteArray#
_) =
      let go :: Int -> [a]
go Int
i = Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
c Bool -> [a] -> [a] -> [a]
forall a. Bool -> a -> a -> a
? [] ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ [a] -> (a -> [a]) -> Maybe a -> [a]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Int -> [a]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) (Maybe a -> [a]) -> Maybe a -> [a]
forall a b. (a -> b) -> a -> b
$ e -> Maybe a
f (SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i)
      in  Int -> [a]
go Int
0
    
    selectEnd :: (e -> Maybe a) -> SBytes# e -> [a]
selectEnd e -> Maybe a
g xs :: SBytes# e
xs@(SBytes# Int
c Int
_ ByteArray#
_) =
      let go :: Int -> SBytes# e -> [a]
go Int
i SBytes# e
es = Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> [a] -> [a] -> [a]
forall a. Bool -> a -> a -> a
? [] ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ [a] -> (a -> [a]) -> Maybe a -> [a]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Int -> SBytes# e -> [a]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) SBytes# e
es) (Maybe a -> [a]) -> Maybe a -> [a]
forall a b. (a -> b) -> a -> b
$ e -> Maybe a
g (SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i)
      in  [a] -> [a]
forall l e. Linear l e => l -> l
reverse ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ Int -> SBytes# e -> [a]
go (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) SBytes# e
xs

instance Bordered (SBytes# e) Int
  where
    lower :: SBytes# e -> Int
lower = Int -> SBytes# e -> Int
forall a b. a -> b -> a
const Int
0
    
    sizeOf :: SBytes# e -> Int
sizeOf   (SBytes# Int
c Int
_ ByteArray#
_) = Int
c
    upper :: SBytes# e -> Int
upper    (SBytes# Int
c Int
_ ByteArray#
_) = Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
    bounds :: SBytes# e -> (Int, Int)
bounds   (SBytes# Int
c Int
_ ByteArray#
_) = (Int
0, Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    indices :: SBytes# e -> [Int]
indices  (SBytes# Int
c Int
_ ByteArray#
_) = [Int
0 .. Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
    indexOf :: SBytes# e -> Int -> Int
indexOf  (SBytes# Int
c Int
_ ByteArray#
_) = (Int, Int) -> Int -> Int
forall i. Index i => (i, i) -> Int -> i
index (Int
0, Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    offsetOf :: SBytes# e -> Int -> Int
offsetOf (SBytes# Int
c Int
_ ByteArray#
_) = (Int, Int) -> Int -> Int
forall i. Index i => (i, i) -> i -> Int
offset (Int
0, Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    indexIn :: SBytes# e -> Int -> Bool
indexIn  (SBytes# Int
c Int
_ ByteArray#
_) = \ Int
i -> Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
c

--------------------------------------------------------------------------------

{- Set, SetWith and Sort instances. -}

instance (Unboxed e, Ord e) => Set (SBytes# e) e

instance (Unboxed e) => SetWith (SBytes# e) e
  where
    setWith :: Compare e -> SBytes# e -> SBytes# e
setWith Compare e
f = Compare e -> SBytes# e -> SBytes# e
forall e. Unboxed e => Compare e -> SBytes# e -> SBytes# e
nubSorted Compare e
f (SBytes# e -> SBytes# e)
-> (SBytes# e -> SBytes# e) -> SBytes# e -> SBytes# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compare e -> SBytes# e -> SBytes# e
forall s e. Sort s e => Compare e -> s -> s
sortBy Compare e
f
    
    insertWith :: Compare e -> e -> SBytes# e -> SBytes# e
insertWith Compare e
f e
e SBytes# e
es = case (\ e
x -> e
x Compare e
`f` e
e Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
/= Ordering
LT) (e -> Bool) -> SBytes# e -> Maybe Int
forall map key e. Map map key e => (e -> Bool) -> map -> Maybe key
.$ SBytes# e
es of
      Just Int
i -> e
e Compare e
`f` (SBytes# e
esSBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^Int
i) Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ Bool -> SBytes# e -> SBytes# e -> SBytes# e
forall a. Bool -> a -> a -> a
? SBytes# e
es (SBytes# e -> SBytes# e) -> SBytes# e -> SBytes# e
forall a b. (a -> b) -> a -> b
$ Int -> e -> SBytes# e -> SBytes# e
forall e. Unboxed e => Int -> e -> SBytes# e -> SBytes# e
before Int
i e
e SBytes# e
es
      Maybe Int
_      -> SBytes# e
es SBytes# e -> e -> SBytes# e
forall l e. Linear l e => l -> e -> l
:< e
e
    
    deleteWith :: Compare e -> e -> SBytes# e -> SBytes# e
deleteWith Compare e
f e
e SBytes# e
es = Compare e -> e -> SBytes# e -> Bool
forall s o. SetWith s o => Compare o -> o -> s -> Bool
memberWith Compare e
f e
e SBytes# e
es Bool -> SBytes# e -> SBytes# e -> SBytes# e
forall a. Bool -> a -> a -> a
? (e -> Bool) -> SBytes# e -> SBytes# e
forall l e. Linear l e => (e -> Bool) -> l -> l
except (\ e
x -> Compare e
f e
e e
x Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ) SBytes# e
es (SBytes# e -> SBytes# e) -> SBytes# e -> SBytes# e
forall a b. (a -> b) -> a -> b
$ SBytes# e
es
    
    {-# INLINE intersectionWith #-}
    intersectionWith :: Compare e -> SBytes# e -> SBytes# e -> SBytes# e
intersectionWith Compare e
f xs :: SBytes# e
xs@(SBytes# Int
n1 Int
_ ByteArray#
_) ys :: SBytes# e
ys@(SBytes# Int
n2 Int
_ ByteArray#
_) = [e] -> SBytes# e
forall l e. Linear l e => [e] -> l
fromList ([e] -> SBytes# e) -> [e] -> SBytes# e
forall a b. (a -> b) -> a -> b
$ Int -> Int -> [e]
go Int
0 Int
0
      where
        go :: Int -> Int -> [e]
go Int
i Int
j = Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n1 Bool -> Bool -> Bool
|| Int
j Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n2 Bool -> [e] -> [e] -> [e]
forall a. Bool -> a -> a -> a
? [] ([e] -> [e]) -> [e] -> [e]
forall a b. (a -> b) -> a -> b
$ case e
x Compare e
`f` e
y of
            Ordering
EQ -> e
x e -> [e] -> [e]
forall a. a -> [a] -> [a]
: Int -> Int -> [e]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
            Ordering
LT -> Int -> Int -> [e]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
j
            Ordering
GT -> Int -> Int -> [e]
go Int
i (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
          where
            x :: e
x = SBytes# e
xs SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i
            y :: e
y = SBytes# e
ys SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
j
    
    {-# INLINE unionWith #-}
    unionWith :: Compare e -> SBytes# e -> SBytes# e -> SBytes# e
unionWith Compare e
f xs :: SBytes# e
xs@(SBytes# Int
n1 Int
_ ByteArray#
_) ys :: SBytes# e
ys@(SBytes# Int
n2 Int
_ ByteArray#
_) = [e] -> SBytes# e
forall l e. Linear l e => [e] -> l
fromList ([e] -> SBytes# e) -> [e] -> SBytes# e
forall a b. (a -> b) -> a -> b
$ Int -> Int -> [e]
go Int
0 Int
0
      where
        go :: Int -> Int -> [e]
go Int
i Int
j
            | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n1 = (SBytes# e
ys SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^) (Int -> e) -> [Int] -> [e]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
j .. Int
n2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
            | Int
j Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n2 = (SBytes# e
xs SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^) (Int -> e) -> [Int] -> [e]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
i .. Int
n1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
            |  Bool
True   = case e
x Compare e
`f` e
y of
              Ordering
EQ -> e
x e -> [e] -> [e]
forall a. a -> [a] -> [a]
: Int -> Int -> [e]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
              Ordering
LT -> e
x e -> [e] -> [e]
forall a. a -> [a] -> [a]
: Int -> Int -> [e]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
j
              Ordering
GT -> e
y e -> [e] -> [e]
forall a. a -> [a] -> [a]
: Int -> Int -> [e]
go Int
i (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
          where
            x :: e
x = SBytes# e
xs SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i
            y :: e
y = SBytes# e
ys SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
j
    
    {-# INLINE differenceWith #-}
    differenceWith :: Compare e -> SBytes# e -> SBytes# e -> SBytes# e
differenceWith Compare e
f xs :: SBytes# e
xs@(SBytes# Int
n1 Int
_ ByteArray#
_) ys :: SBytes# e
ys@(SBytes# Int
n2 Int
_ ByteArray#
_) = [e] -> SBytes# e
forall l e. Linear l e => [e] -> l
fromList ([e] -> SBytes# e) -> [e] -> SBytes# e
forall a b. (a -> b) -> a -> b
$ Int -> Int -> [e]
go Int
0 Int
0
      where
        go :: Int -> Int -> [e]
go Int
i Int
j
            | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n1 = []
            | Int
j Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n2 = (SBytes# e
xs SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^) (Int -> e) -> [Int] -> [e]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
i .. Int
n1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
            |  Bool
True   = case e
x Compare e
`f` e
y of
              Ordering
EQ -> Int -> Int -> [e]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
              Ordering
LT -> e
x e -> [e] -> [e]
forall a. a -> [a] -> [a]
: Int -> Int -> [e]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
j
              Ordering
GT -> Int -> Int -> [e]
go Int
i (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
          where
            x :: e
x = SBytes# e
xs SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i
            y :: e
y = SBytes# e
ys SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
j
    
    {-# INLINE symdiffWith #-}
    symdiffWith :: Compare e -> SBytes# e -> SBytes# e -> SBytes# e
symdiffWith Compare e
f xs :: SBytes# e
xs@(SBytes# Int
n1 Int
_ ByteArray#
_) ys :: SBytes# e
ys@(SBytes# Int
n2 Int
_ ByteArray#
_) = [e] -> SBytes# e
forall l e. Linear l e => [e] -> l
fromList ([e] -> SBytes# e) -> [e] -> SBytes# e
forall a b. (a -> b) -> a -> b
$ Int -> Int -> [e]
symdiff' Int
0 Int
0
      where
        symdiff' :: Int -> Int -> [e]
symdiff' Int
i Int
j
            | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n1 = (SBytes# e
ys SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^) (Int -> e) -> [Int] -> [e]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
j .. Int
n2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
            | Int
j Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n2 = (SBytes# e
xs SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^) (Int -> e) -> [Int] -> [e]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
i .. Int
n1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
            |  Bool
True   = case e
x Compare e
`f` e
y of
              Ordering
EQ -> Int -> Int -> [e]
symdiff' (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
              Ordering
LT -> e
x e -> [e] -> [e]
forall a. a -> [a] -> [a]
: Int -> Int -> [e]
symdiff' (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
j
              Ordering
GT -> e
y e -> [e] -> [e]
forall a. a -> [a] -> [a]
: Int -> Int -> [e]
symdiff' Int
i (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
          where
            x :: e
x = SBytes# e
xs SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i
            y :: e
y = SBytes# e
ys SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
j
    
    memberWith :: Compare e -> e -> SBytes# e -> Bool
memberWith = Compare e -> e -> SBytes# e -> Bool
forall v e i.
(Linear v e, Bordered v i) =>
Compare e -> e -> v -> Bool
binaryContain
    
    lookupLTWith :: Compare e -> e -> SBytes# e -> Maybe e
lookupLTWith Compare e
_ e
_ SBytes# e
Z  = Maybe e
forall a. Maybe a
Nothing
    lookupLTWith Compare e
f e
o SBytes# e
es
        | Ordering
GT <- e
o Compare e
`f` e
last' = e -> Maybe e
forall a. a -> Maybe a
Just e
last'
        | Ordering
GT <- e
o Compare e
`f` e
head' = e -> Int -> Int -> Maybe e
look' e
head' Int
0 (SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf SBytes# e
es Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
        |       Bool
True        = Maybe e
forall a. Maybe a
Nothing
      where
        head' :: e
head' = SBytes# e
es SBytes# e -> Int -> e
forall map key e. Map map key e => map -> key -> e
.! SBytes# e -> Int
forall b i. Bordered b i => b -> i
lower SBytes# e
es
        last' :: e
last' = SBytes# e
es SBytes# e -> Int -> e
forall map key e. Map map key e => map -> key -> e
.! SBytes# e -> Int
forall b i. Bordered b i => b -> i
upper SBytes# e
es
        
        look' :: e -> Int -> Int -> Maybe e
look' e
r Int
l Int
u = Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
u Bool -> Maybe e -> Maybe e -> Maybe e
forall a. Bool -> a -> a -> a
? e -> Maybe e
forall a. a -> Maybe a
Just e
r (Maybe e -> Maybe e) -> Maybe e -> Maybe e
forall a b. (a -> b) -> a -> b
$ case e
o Compare e
`f` e
e of
            Ordering
LT -> e -> Int -> Int -> Maybe e
look' e
r Int
l (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
            Ordering
EQ -> e -> Maybe e
forall a. a -> Maybe a
Just (e -> Maybe e) -> e -> Maybe e
forall a b. (a -> b) -> a -> b
$ Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1 Bool -> e -> e -> e
forall a. Bool -> a -> a -> a
? e
r (e -> e) -> e -> e
forall a b. (a -> b) -> a -> b
$ SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
            Ordering
GT -> e -> Int -> Int -> Maybe e
look' e
e (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
u
          where
            j :: Int
j = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
u Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
            e :: e
e = SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
j
    
    lookupLEWith :: Compare e -> e -> SBytes# e -> Maybe e
lookupLEWith Compare e
_ e
_ SBytes# e
Z  = Maybe e
forall a. Maybe a
Nothing
    lookupLEWith Compare e
f e
o SBytes# e
es
        | Ordering
GT <- e
o Compare e
`f` e
last' = e -> Maybe e
forall a. a -> Maybe a
Just e
last'
        | Ordering
LT <- e
o Compare e
`f` e
head' = Maybe e
forall a. Maybe a
Nothing
        |       Bool
True        = e -> Int -> Int -> Maybe e
look' e
head' Int
0 (SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf SBytes# e
es Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
      where
        head' :: e
head' = SBytes# e
es SBytes# e -> Int -> e
forall map key e. Map map key e => map -> key -> e
.! SBytes# e -> Int
forall b i. Bordered b i => b -> i
lower SBytes# e
es
        last' :: e
last' = SBytes# e
es SBytes# e -> Int -> e
forall map key e. Map map key e => map -> key -> e
.! SBytes# e -> Int
forall b i. Bordered b i => b -> i
upper SBytes# e
es
        
        look' :: e -> Int -> Int -> Maybe e
look' e
r Int
l Int
u = Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
u Bool -> Maybe e -> Maybe e -> Maybe e
forall a. Bool -> a -> a -> a
? e -> Maybe e
forall a. a -> Maybe a
Just e
r (Maybe e -> Maybe e) -> Maybe e -> Maybe e
forall a b. (a -> b) -> a -> b
$ case e
o Compare e
`f` e
e of
            Ordering
LT -> e -> Int -> Int -> Maybe e
look' e
r Int
l (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
            Ordering
_  -> e -> Int -> Int -> Maybe e
look' e
e (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
u
          where
            j :: Int
j = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
u Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
            e :: e
e = SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
j
    
    lookupGTWith :: Compare e -> e -> SBytes# e -> Maybe e
lookupGTWith Compare e
_ e
_ SBytes# e
Z  = Maybe e
forall a. Maybe a
Nothing
    lookupGTWith Compare e
f e
o SBytes# e
es
        | Ordering
LT <- e
o Compare e
`f` e
head' = e -> Maybe e
forall a. a -> Maybe a
Just e
head'
        | Ordering
LT <- e
o Compare e
`f` e
last' = e -> Int -> Int -> Maybe e
look' e
last' Int
0 (SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf SBytes# e
es Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
        |       Bool
True        = Maybe e
forall a. Maybe a
Nothing
      where
        head' :: e
head' = SBytes# e
es SBytes# e -> Int -> e
forall map key e. Map map key e => map -> key -> e
.! SBytes# e -> Int
forall b i. Bordered b i => b -> i
lower SBytes# e
es
        last' :: e
last' = SBytes# e
es SBytes# e -> Int -> e
forall map key e. Map map key e => map -> key -> e
.! SBytes# e -> Int
forall b i. Bordered b i => b -> i
upper SBytes# e
es
        
        look' :: e -> Int -> Int -> Maybe e
look' e
r Int
l Int
u = Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
u Bool -> Maybe e -> Maybe e -> Maybe e
forall a. Bool -> a -> a -> a
? e -> Maybe e
forall a. a -> Maybe a
Just e
r (Maybe e -> Maybe e) -> Maybe e -> Maybe e
forall a b. (a -> b) -> a -> b
$ case e
o Compare e
`f` e
e of
            Ordering
LT -> e -> Int -> Int -> Maybe e
look' e
e Int
l (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
            Ordering
EQ -> Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= (SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf SBytes# e
es Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Bool -> Maybe e -> Maybe e -> Maybe e
forall a. Bool -> a -> a -> a
? Maybe e
forall a. Maybe a
Nothing (Maybe e -> Maybe e) -> Maybe e -> Maybe e
forall a b. (a -> b) -> a -> b
$ e -> Maybe e
forall a. a -> Maybe a
Just (SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
            Ordering
GT -> e -> Int -> Int -> Maybe e
look' e
r (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
u
          where
            j :: Int
j = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
u Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
            e :: e
e = SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
j
    
    lookupGEWith :: Compare e -> e -> SBytes# e -> Maybe e
lookupGEWith Compare e
_ e
_ SBytes# e
Z  = Maybe e
forall a. Maybe a
Nothing
    lookupGEWith Compare e
f e
o SBytes# e
es
        | Ordering
GT <- e
o Compare e
`f` e
last' = Maybe e
forall a. Maybe a
Nothing
        | Ordering
GT <- e
o Compare e
`f` e
head' = e -> Int -> Int -> Maybe e
look' e
last' Int
0 (SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf SBytes# e
es Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
        |       Bool
True        = e -> Maybe e
forall a. a -> Maybe a
Just e
head'
      where
        head' :: e
head' = SBytes# e
es SBytes# e -> Int -> e
forall map key e. Map map key e => map -> key -> e
.! SBytes# e -> Int
forall b i. Bordered b i => b -> i
lower SBytes# e
es
        last' :: e
last' = SBytes# e
es SBytes# e -> Int -> e
forall map key e. Map map key e => map -> key -> e
.! SBytes# e -> Int
forall b i. Bordered b i => b -> i
upper SBytes# e
es
        
        look' :: e -> Int -> Int -> Maybe e
look' e
r Int
l Int
u = Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
u Bool -> Maybe e -> Maybe e -> Maybe e
forall a. Bool -> a -> a -> a
? e -> Maybe e
forall a. a -> Maybe a
Just e
r (Maybe e -> Maybe e) -> Maybe e -> Maybe e
forall a b. (a -> b) -> a -> b
$ case e
o Compare e
`f` e
e of
            Ordering
LT -> e -> Int -> Int -> Maybe e
look' e
e Int
l (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
            Ordering
EQ -> e -> Maybe e
forall a. a -> Maybe a
Just e
e
            Ordering
GT -> e -> Int -> Int -> Maybe e
look' e
r (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
u
          where
            j :: Int
j = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
u Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
            e :: e
e = SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
j
    
    isSubsetWith :: Compare e -> SBytes# e -> SBytes# e -> Bool
isSubsetWith Compare e
f SBytes# e
xs SBytes# e
ys = (e -> Bool -> Bool) -> Bool -> SBytes# e -> Bool
forall l e b. Linear l e => (e -> b -> b) -> b -> l -> b
o_foldr (\ e
x Bool
b -> Bool
b Bool -> Bool -> Bool
&& Compare e -> e -> SBytes# e -> Bool
forall s o. SetWith s o => Compare o -> o -> s -> Bool
memberWith Compare e
f e
x SBytes# e
ys) Bool
True SBytes# e
xs

instance (Unboxed e) => Scan (SBytes# e) e

instance (Unboxed e) => Sort (SBytes# e) e
  where
    sortBy :: Compare e -> SBytes# e -> SBytes# e
sortBy Compare e
cmp SBytes# e
es = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ do STBytes# s e
es' <- SBytes# e -> ST s (STBytes# s e)
forall (m :: * -> *) v v'. Thaw m v v' => v -> m v'
thaw SBytes# e
es; Compare e -> STBytes# s e -> ST s ()
forall (m :: * -> *) v e i.
(LinearM m v e, BorderedM m v i) =>
Compare e -> v -> m ()
timSortBy Compare e
cmp STBytes# s e
es'; STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done STBytes# s e
es'
    
    sortedBy :: (e -> e -> Bool) -> SBytes# e -> Bool
sortedBy e -> e -> Bool
f es :: SBytes# e
es@(SBytes# Int
n Int
_ ByteArray#
_) =
      let go :: Int -> Bool
go Int
i = let i1 :: Int
i1 = Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 in Int
i1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n Bool -> Bool -> Bool
|| (e -> e -> Bool
f (SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i) (SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i1) Bool -> Bool -> Bool
&& Int -> Bool
go Int
i1)
      in  Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
2 Bool -> Bool -> Bool
|| Int -> Bool
go Int
0

--------------------------------------------------------------------------------

{- Map and Indexed instances. -}

instance (Unboxed e) => Map (SBytes# e) Int e
  where
    toMap :: [(Int, e)] -> SBytes# e
toMap [(Int, e)]
ascs = [(Int, e)] -> Bool
forall e. Nullable e => e -> Bool
isNull [(Int, e)]
ascs Bool -> SBytes# e -> SBytes# e -> SBytes# e
forall a. Bool -> a -> a -> a
? SBytes# e
forall e. Nullable e => e
Z (SBytes# e -> SBytes# e) -> SBytes# e -> SBytes# e
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> [(Int, e)] -> SBytes# e
forall v i e. Indexed v i e => (i, i) -> [(i, e)] -> v
assoc ([(Int, e)] -> (Int, Int)
forall a b. Ord a => [(a, b)] -> (a, a)
ascsBounds [(Int, e)]
ascs) [(Int, e)]
ascs
    
    toMap' :: e -> [(Int, e)] -> SBytes# e
toMap' e
defvalue [(Int, e)]
ascs = [(Int, e)] -> Bool
forall e. Nullable e => e -> Bool
isNull [(Int, e)]
ascs Bool -> SBytes# e -> SBytes# e -> SBytes# e
forall a. Bool -> a -> a -> a
? SBytes# e
forall e. Nullable e => e
Z (SBytes# e -> SBytes# e) -> SBytes# e -> SBytes# e
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> e -> [(Int, e)] -> SBytes# e
forall v i e. Indexed v i e => (i, i) -> e -> [(i, e)] -> v
assoc' ([(Int, e)] -> (Int, Int)
forall a b. Ord a => [(a, b)] -> (a, a)
ascsBounds [(Int, e)]
ascs) e
defvalue [(Int, e)]
ascs
    
    SBytes# e
Z  // :: SBytes# e -> [(Int, e)] -> SBytes# e
// [(Int, e)]
ascs = [(Int, e)] -> SBytes# e
forall map key e. Map map key e => [(key, e)] -> map
toMap [(Int, e)]
ascs
    SBytes# e
es // [(Int, e)]
ascs = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ SBytes# e -> ST s (STBytes# s e)
forall (m :: * -> *) v v'. Thaw m v v' => v -> m v'
thaw SBytes# e
es ST s (STBytes# s e)
-> (STBytes# s e -> ST s (STBytes# s e)) -> ST s (STBytes# s e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (STBytes# s e -> [(Int, e)] -> ST s (STBytes# s e))
-> [(Int, e)] -> STBytes# s e -> ST s (STBytes# s e)
forall a b c. (a -> b -> c) -> b -> a -> c
flip STBytes# s e -> [(Int, e)] -> ST s (STBytes# s e)
forall (m :: * -> *) map key e.
MapM m map key e =>
map -> [(key, e)] -> m map
overwrite [(Int, e)]
ascs ST s (STBytes# s e)
-> (STBytes# s e -> ST s (SBytes# e)) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done
    
    *$ :: (e -> Bool) -> SBytes# e -> [Int]
(*$) e -> Bool
p = (Int -> e -> [Int] -> [Int]) -> [Int] -> SBytes# e -> [Int]
forall l e b. Linear l e => (Int -> e -> b -> b) -> b -> l -> b
ofoldr (\ Int
i e
e [Int]
is -> e -> Bool
p e
e Bool -> [Int] -> [Int] -> [Int]
forall a. Bool -> a -> a -> a
? (Int
i Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
is) ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ [Int]
is) []
    .! :: SBytes# e -> Int -> e
(.!)   = SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
(!^)
    
    kfoldr :: (Int -> e -> b -> b) -> b -> SBytes# e -> b
kfoldr = (Int -> e -> b -> b) -> b -> SBytes# e -> b
forall l e b. Linear l e => (Int -> e -> b -> b) -> b -> l -> b
ofoldr
    kfoldl :: (Int -> b -> e -> b) -> b -> SBytes# e -> b
kfoldl = (Int -> b -> e -> b) -> b -> SBytes# e -> b
forall l e b. Linear l e => (Int -> b -> e -> b) -> b -> l -> b
ofoldl

instance (Unboxed e) => Indexed (SBytes# e) Int e
  where
    assoc :: (Int, Int) -> [(Int, e)] -> SBytes# e
assoc  (Int, Int)
bnds [(Int, e)]
ascs = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> [(Int, e)] -> ST s (STBytes# s e)
forall (m :: * -> *) v i e.
IndexedM m v i e =>
(i, i) -> [(i, e)] -> m v
fromAssocs (Int, Int)
bnds [(Int, e)]
ascs ST s (STBytes# s e)
-> (STBytes# s e -> ST s (SBytes# e)) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done
    assoc' :: (Int, Int) -> e -> [(Int, e)] -> SBytes# e
assoc' (Int, Int)
bnds e
defvalue [(Int, e)]
ascs = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> e -> [(Int, e)] -> ST s (STBytes# s e)
forall (m :: * -> *) v i e.
IndexedM m v i e =>
(i, i) -> e -> [(i, e)] -> m v
fromAssocs' (Int, Int)
bnds e
defvalue [(Int, e)]
ascs ST s (STBytes# s e)
-> (STBytes# s e -> ST s (SBytes# e)) -> ST s (SBytes# e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done
    
    fromIndexed :: m -> SBytes# e
fromIndexed m
es = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ do
      let n :: Int
n = m -> Int
forall b i. Bordered b i => b -> Int
sizeOf m
es
      STBytes# s e
copy <- Int -> ST s (STBytes# s e)
forall e s. Unboxed e => Int -> ST s (STBytes# s e)
alloc Int
n
      [Int] -> (Int -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> ST s ()) -> ST s ()) -> (Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \ Int
i -> STBytes# s e -> Int -> e -> ST s ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM STBytes# s e
copy Int
i (m
es m -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i)
      STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done STBytes# s e
copy

--------------------------------------------------------------------------------

instance (Unboxed e) => Thaw (ST s) (SBytes# e) (STBytes# s e)
  where
    thaw :: SBytes# e -> ST s (STBytes# s e)
thaw es :: SBytes# e
es@(SBytes# c :: Int
c@(I# Int#
c#) (I# Int#
o#) ByteArray#
arr#) = do
      marr :: STBytes# s e
marr@(STBytes# Int
_ Int
_ MutableByteArray# s
marr#) <- Int -> ST s (STBytes# s e)
forall e s. Unboxed e => Int -> ST s (STBytes# s e)
alloc Int
c
      STRep s () -> ST s ()
forall s a. STRep s a -> ST s a
ST (STRep s () -> ST s ()) -> STRep s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case SBytes# e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
pcopyUnboxed SBytes# e
es ByteArray#
arr# Int#
o# MutableByteArray# s
marr# Int#
0# Int#
c# State# s
s1# of
        State# s
s2# -> (# State# s
s2#, () #)
      STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
marr

instance (Unboxed e) => Freeze (ST s) (STBytes# s e) (SBytes# e)
  where
    freeze :: STBytes# s e -> ST s (SBytes# e)
freeze       = STBytes# s e -> ST s (STBytes# s e)
forall e s. Unboxed e => STBytes# s e -> ST s (STBytes# s e)
cloneSTBytes# (STBytes# s e -> ST s (STBytes# s e))
-> (STBytes# s e -> ST s (SBytes# e))
-> STBytes# s e
-> ST s (SBytes# e)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done
    unsafeFreeze :: STBytes# s e -> ST s (SBytes# e)
unsafeFreeze = STBytes# s e -> ST s (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done

--------------------------------------------------------------------------------

-- | 'STBytes#' is mutable pseudo-primitive 'Int'-indexed strict unboxed array type.
data STBytes# s e = STBytes#
              {-# UNPACK #-} !Int    -- ^ Element count (not a real size)
              {-# UNPACK #-} !Int    -- ^ Offset (in elements)
              !(MutableByteArray# s) -- ^ Real primitive byte array
  deriving ( Typeable )

type role STBytes# nominal representational

--------------------------------------------------------------------------------

instance Eq (STBytes# s e)
  where
    (STBytes# Int
c1 Int
o1 MutableByteArray# s
marr1#) == :: STBytes# s e -> STBytes# s e -> Bool
== (STBytes# Int
c2 Int
o2 MutableByteArray# s
marr2#) =
      let same :: Bool
same = Int# -> Bool
isTrue# (MutableByteArray# s -> MutableByteArray# s -> Int#
forall d. MutableByteArray# d -> MutableByteArray# d -> Int#
sameMutableByteArray# MutableByteArray# s
marr1# MutableByteArray# s
marr2#)
      in  Int
c1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
c2 Bool -> Bool -> Bool
&& (Int
c1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
|| Int
o1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
o2 Bool -> Bool -> Bool
&& Bool
same)

--------------------------------------------------------------------------------

{- Estimate, Bordered, BorderedM, LinearM and SplitM instances. -}

instance Estimate (STBytes# s e)
  where
    <==> :: Compare (STBytes# s e)
(<==>) = Compare Int -> (STBytes# s e -> Int) -> Compare (STBytes# s e)
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Compare Int
forall o. Ord o => Compare o
(<=>) STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<=. :: STBytes# s e -> STBytes# s e -> Bool
(.<=.) = (Int -> Int -> Bool)
-> (STBytes# s e -> Int) -> STBytes# s e -> STBytes# s e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<=)  STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>=. :: STBytes# s e -> STBytes# s e -> Bool
(.>=.) = (Int -> Int -> Bool)
-> (STBytes# s e -> Int) -> STBytes# s e -> STBytes# s e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>=)  STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>. :: STBytes# s e -> STBytes# s e -> Bool
(.>.)  = (Int -> Int -> Bool)
-> (STBytes# s e -> Int) -> STBytes# s e -> STBytes# s e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>)   STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<. :: STBytes# s e -> STBytes# s e -> Bool
(.<.)  = (Int -> Int -> Bool)
-> (STBytes# s e -> Int) -> STBytes# s e -> STBytes# s e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<)   STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    
    <.=> :: STBytes# s e -> Int -> Ordering
(<.=>) = Compare Int
forall o. Ord o => Compare o
(<=>) Compare Int
-> (STBytes# s e -> Int) -> STBytes# s e -> Int -> Ordering
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>= :: STBytes# s e -> Int -> Bool
(.>=)  = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>=)  (Int -> Int -> Bool)
-> (STBytes# s e -> Int) -> STBytes# s e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<= :: STBytes# s e -> Int -> Bool
(.<=)  = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<=)  (Int -> Int -> Bool)
-> (STBytes# s e -> Int) -> STBytes# s e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .> :: STBytes# s e -> Int -> Bool
(.>)   = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>)   (Int -> Int -> Bool)
-> (STBytes# s e -> Int) -> STBytes# s e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .< :: STBytes# s e -> Int -> Bool
(.<)   = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<)   (Int -> Int -> Bool)
-> (STBytes# s e -> Int) -> STBytes# s e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf

instance Bordered (STBytes# s e) Int
  where
    bounds :: STBytes# s e -> (Int, Int)
bounds (STBytes# Int
c Int
_ MutableByteArray# s
_) = (Int
0, Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    sizeOf :: STBytes# s e -> Int
sizeOf (STBytes# Int
c Int
_ MutableByteArray# s
_) = Int
c

instance BorderedM (ST s) (STBytes# s e) Int
  where
    getLower :: STBytes# s e -> ST s Int
getLower STBytes# s e
_ = Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0
    
    nowIndexIn :: STBytes# s e -> Int -> ST s Bool
nowIndexIn (STBytes# Int
c Int
_ MutableByteArray# s
_) = Bool -> ST s Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> ST s Bool) -> (Int -> Bool) -> Int -> ST s Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, Int) -> Int -> Bool
forall i. Index i => (i, i) -> i -> Bool
inRange (Int
0, Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    getIndices :: STBytes# s e -> ST s [Int]
getIndices (STBytes# Int
c Int
_ MutableByteArray# s
_) = [Int] -> ST s [Int]
forall (m :: * -> *) a. Monad m => a -> m a
return [Int
0 .. Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
    getBounds :: STBytes# s e -> ST s (Int, Int)
getBounds  (STBytes# Int
c Int
_ MutableByteArray# s
_) = (Int, Int) -> ST s (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
0, Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    getUpper :: STBytes# s e -> ST s Int
getUpper   (STBytes# Int
c Int
_ MutableByteArray# s
_) = Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    getSizeOf :: STBytes# s e -> ST s Int
getSizeOf  (STBytes# Int
c Int
_ MutableByteArray# s
_) = Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c

instance (Unboxed e) => LinearM (ST s) (STBytes# s e) e
  where
    newNull :: ST s (STBytes# s e)
newNull = STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall s a. STRep s a -> ST s a
ST (STRep s (STBytes# s e) -> ST s (STBytes# s e))
-> STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
0# State# s
s1# of
      (# State# s
s2#, MutableByteArray# s
marr# #) -> (# State# s
s2#, Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
0 Int
0 MutableByteArray# s
marr# #)
    
    getHead :: STBytes# s e -> ST s e
getHead STBytes# s e
es = STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) map key e.
MapM m map key e =>
map -> key -> m e
>! Int
0
    getLast :: STBytes# s e -> ST s e
getLast STBytes# s e
es = STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) map key e.
MapM m map key e =>
map -> key -> m e
>! STBytes# s e -> Int
forall b i. Bordered b i => b -> i
upper STBytes# s e
es
    nowNull :: STBytes# s e -> ST s Bool
nowNull STBytes# s e
es = Bool -> ST s Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf STBytes# s e
es Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0)
    newLinear :: [e] -> ST s (STBytes# s e)
newLinear  = [e] -> ST s (STBytes# s e)
forall (m :: * -> *) l e (f :: * -> *).
(LinearM m l e, Foldable f) =>
f e -> m l
fromFoldableM
    
    newLinearN :: Int -> [e] -> ST s (STBytes# s e)
newLinearN Int
c [e]
es = STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall s a. STRep s a -> ST s a
ST (STRep s (STBytes# s e) -> ST s (STBytes# s e))
-> STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case [e] -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
pnewUnboxed [e]
es Int#
n# State# s
s1# of
        (# State# s
s2#, MutableByteArray# s
marr# #) ->
          let
            go :: e -> (Int# -> State# s -> State# s) -> Int# -> State# s -> State# s
go e
y Int# -> State# s -> State# s
r = \ Int#
i# State# s
s3# -> case MutableByteArray# s -> Int# -> e -> State# s -> State# s
forall e s.
Unboxed e =>
MutableByteArray# s -> Int# -> e -> State# s -> State# s
writeByteArray# MutableByteArray# s
marr# Int#
i# e
y State# s
s3# of
              State# s
s4# -> if Int# -> Bool
isTrue# (Int#
i# Int# -> Int# -> Int#
==# Int#
n# Int# -> Int# -> Int#
-# Int#
1#) then State# s
s4# else Int# -> State# s -> State# s
r (Int#
i# Int# -> Int# -> Int#
+# Int#
1#) State# s
s4#
          in Int -> MutableByteArray# s -> STRep s (STBytes# s e)
forall s e. Int -> MutableByteArray# s -> STRep s (STBytes# s e)
done' Int
n MutableByteArray# s
marr# (if Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then State# s
s2# else (e
 -> (Int# -> State# s -> State# s) -> Int# -> State# s -> State# s)
-> (Int# -> State# s -> State# s)
-> [e]
-> Int#
-> State# s
-> State# s
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr e -> (Int# -> State# s -> State# s) -> Int# -> State# s -> State# s
go (\ Int#
_ State# s
s# -> State# s
s#) [e]
es Int#
0# State# s
s2#)
      where
        !n :: Int
n@(I# Int#
n#) = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
c
    
    fromFoldableM :: f e -> ST s (STBytes# s e)
fromFoldableM f e
es = STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall s a. STRep s a -> ST s a
ST (STRep s (STBytes# s e) -> ST s (STBytes# s e))
-> STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case f e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
pnewUnboxed f e
es Int#
n# State# s
s1# of
        (# State# s
s2#, MutableByteArray# s
marr# #) ->
          let
            go :: e -> (Int# -> State# s -> State# s) -> Int# -> State# s -> State# s
go e
y Int# -> State# s -> State# s
r = \ Int#
i# State# s
s3# -> case MutableByteArray# s -> Int# -> e -> State# s -> State# s
forall e s.
Unboxed e =>
MutableByteArray# s -> Int# -> e -> State# s -> State# s
writeByteArray# MutableByteArray# s
marr# Int#
i# e
y State# s
s3# of
              State# s
s4# -> if Int# -> Bool
isTrue# (Int#
i# Int# -> Int# -> Int#
==# Int#
n# Int# -> Int# -> Int#
-# Int#
1#) then State# s
s4# else Int# -> State# s -> State# s
r (Int#
i# Int# -> Int# -> Int#
+# Int#
1#) State# s
s4#
          in Int -> MutableByteArray# s -> STRep s (STBytes# s e)
forall s e. Int -> MutableByteArray# s -> STRep s (STBytes# s e)
done' Int
n MutableByteArray# s
marr# (if Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then State# s
s2# else (e
 -> (Int# -> State# s -> State# s) -> Int# -> State# s -> State# s)
-> (Int# -> State# s -> State# s)
-> f e
-> Int#
-> State# s
-> State# s
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr e -> (Int# -> State# s -> State# s) -> Int# -> State# s -> State# s
go (\ Int#
_ State# s
s# -> State# s
s#) f e
es Int#
0# State# s
s2#)
      where
        !n :: Int
n@(I# Int#
n#) = f e -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length f e
es
    
    getLeft :: STBytes# s e -> ST s [e]
getLeft  es :: STBytes# s e
es@(STBytes# Int
n Int
_ MutableByteArray# s
_) = (STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#>) (Int -> ST s e) -> [Int] -> ST s [e]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
`mapM` [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
    getRight :: STBytes# s e -> ST s [e]
getRight es :: STBytes# s e
es@(STBytes# Int
n Int
_ MutableByteArray# s
_) = (STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#>) (Int -> ST s e) -> [Int] -> ST s [e]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
`mapM` [Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2 .. Int
0]
    
    {-# INLINE (!#>) #-}
    !#> :: STBytes# s e -> Int -> ST s e
(!#>) (STBytes# Int
_ (I# Int#
o#) MutableByteArray# s
marr#) = \ (I# Int#
i#) -> STRep s e -> ST s e
forall s a. STRep s a -> ST s a
ST (STRep s e -> ST s e) -> STRep s e -> ST s e
forall a b. (a -> b) -> a -> b
$ MutableByteArray# s
marr# MutableByteArray# s -> Int# -> STRep s e
forall e s.
Unboxed e =>
MutableByteArray# s -> Int# -> State# s -> (# State# s, e #)
!># (Int#
o# Int# -> Int# -> Int#
+# Int#
i#)
    
    writeM :: STBytes# s e -> Int -> e -> ST s ()
writeM = STBytes# s e -> Int -> e -> ST s ()
forall (m :: * -> *) v i e. IndexedM m v i e => v -> i -> e -> m ()
writeM'
    
    copied :: STBytes# s e -> ST s (STBytes# s e)
copied es :: STBytes# s e
es@(STBytes# Int
n Int
_ MutableByteArray# s
_) = do
      STBytes# s e
copy <- Int -> ST s (STBytes# s e)
forall e s. Unboxed e => Int -> ST s (STBytes# s e)
alloc Int
n
      [Int] -> (Int -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> ST s ()) -> ST s ()) -> (Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \ Int
i -> STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i ST s e -> (e -> ST s ()) -> ST s ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> Int -> e -> ST s ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM STBytes# s e
copy Int
i
      STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
copy
    
    copied' :: STBytes# s e -> Int -> Int -> ST s (STBytes# s e)
copied' STBytes# s e
es Int
l Int
n = do
      STBytes# s e
copy <- Int -> ST s (STBytes# s e)
forall e s. Unboxed e => Int -> ST s (STBytes# s e)
alloc Int
n
      [Int] -> (Int -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> ST s ()) -> ST s ()) -> (Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \ Int
i -> STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> (Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
i) ST s e -> (e -> ST s ()) -> ST s ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> Int -> e -> ST s ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM STBytes# s e
copy Int
i
      STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
copy
    
    reversed :: STBytes# s e -> ST s (STBytes# s e)
reversed es :: STBytes# s e
es@(STBytes# Int
n Int
_ MutableByteArray# s
_) =
      let go :: Int -> Int -> ST s ()
go Int
i Int
j = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
j) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int -> Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ST s () -> ST s () -> ST s ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> STBytes# s e -> Int -> Int -> ST s ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> Int -> m ()
swapM STBytes# s e
es Int
i Int
j
      in  Int -> Int -> ST s ()
go Int
0 (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ST s () -> ST s (STBytes# s e) -> ST s (STBytes# s e)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
es
    
    filled :: Int -> e -> ST s (STBytes# s e)
filled Int
n e
e = STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall s a. STRep s a -> ST s a
ST (STRep s (STBytes# s e) -> ST s (STBytes# s e))
-> STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e s.
Unboxed e =>
e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
newUnboxed' e
e Int#
n# State# s
s1# of
        (# State# s
s2#, MutableByteArray# s
marr# #) -> (# State# s
s2#, Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
n' Int
0 MutableByteArray# s
marr# #)
      where
        !n' :: Int
n'@(I# Int#
n#) = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
n
    
    copyTo :: STBytes# s e -> Int -> STBytes# s e -> Int -> Int -> ST s ()
copyTo STBytes# s e
src Int
sc STBytes# s e
trg Int
tc n :: Int
n@(I# Int#
n#) = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
        Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when      (Int
sc Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
tc Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0)      (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ String -> ST s ()
forall a. String -> a
underEx String
"copyTo"
        Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
sc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n1 Bool -> Bool -> Bool
|| Int
tc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n2) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ String -> ST s ()
forall a. String -> a
overEx  String
"copyTo"
        
        STRep s () -> ST s ()
forall s a. STRep s a -> ST s a
ST (STRep s () -> ST s ()) -> STRep s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case STBytes# s e
-> MutableByteArray# s
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e
-> MutableByteArray# s
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
pcopyUnboxedM STBytes# s e
src MutableByteArray# s
src# Int#
so# MutableByteArray# s
trg# Int#
to# Int#
n# State# s
s1# of
          State# s
s2# -> (# State# s
s2#, () #)
      where
        !(STBytes# Int
n1 Int
o1 MutableByteArray# s
src#) = STBytes# s e
src; !(I# Int#
so#) = Int
o1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sc
        !(STBytes# Int
n2 Int
o2 MutableByteArray# s
trg#) = STBytes# s e
trg; !(I# Int#
to#) = Int
o2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
tc
    
    merged :: f (STBytes# s e) -> ST s (STBytes# s e)
merged f (STBytes# s e)
ess = do
        STBytes# s e
marr <- Int -> ST s (STBytes# s e)
forall e s. Unboxed e => Int -> ST s (STBytes# s e)
alloc Int
n
        let writer :: STBytes# s e -> Int -> ST s Int
writer arr :: STBytes# s e
arr@(STBytes# Int
c Int
_ MutableByteArray# s
_) Int
i = (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
c) Int -> ST s () -> ST s Int
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ STBytes# s e -> Int -> STBytes# s e -> Int -> Int -> ST s ()
forall (m :: * -> *) l e.
LinearM m l e =>
l -> Int -> l -> Int -> Int -> m ()
copyTo STBytes# s e
arr Int
0 STBytes# s e
marr Int
i Int
c
        STBytes# s e
marr STBytes# s e -> ST s Int -> ST s (STBytes# s e)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (STBytes# s e -> ST s Int -> ST s Int)
-> ST s Int -> f (STBytes# s e) -> ST s Int
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((Int -> ST s Int) -> ST s Int -> ST s Int
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
(=<<) ((Int -> ST s Int) -> ST s Int -> ST s Int)
-> (STBytes# s e -> Int -> ST s Int)
-> STBytes# s e
-> ST s Int
-> ST s Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# s e -> Int -> ST s Int
writer) (Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0) f (STBytes# s e)
ess
      where
        n :: Int
n = (STBytes# s e -> Int -> Int) -> Int -> f (STBytes# s e) -> Int
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr' (Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) (Int -> Int -> Int)
-> (STBytes# s e -> Int) -> STBytes# s e -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# s e -> Int
forall b i. Bordered b i => b -> Int
sizeOf) Int
0 f (STBytes# s e)
ess
    
    ofoldrM :: (Int -> e -> r -> ST s r) -> r -> STBytes# s e -> ST s r
ofoldrM Int -> e -> r -> ST s r
f r
base = \ arr :: STBytes# s e
arr@(STBytes# Int
n Int
_ MutableByteArray# s
_) ->
      let go :: Int -> ST s r
go Int
i =  Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> ST s r -> ST s r -> ST s r
forall a. Bool -> a -> a -> a
? r -> ST s r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base (ST s r -> ST s r) -> ST s r -> ST s r
forall a b. (a -> b) -> a -> b
$ (STBytes# s e
arr STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i) ST s e -> ST s r -> (e -> r -> ST s r) -> ST s r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
>>=<< Int -> ST s r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) ((e -> r -> ST s r) -> ST s r) -> (e -> r -> ST s r) -> ST s r
forall a b. (a -> b) -> a -> b
$ Int -> e -> r -> ST s r
f Int
i
      in  Int -> ST s r
go Int
0
    
    ofoldlM :: (Int -> r -> e -> ST s r) -> r -> STBytes# s e -> ST s r
ofoldlM Int -> r -> e -> ST s r
f r
base = \ arr :: STBytes# s e
arr@(STBytes# Int
n Int
_ MutableByteArray# s
_) ->
      let go :: Int -> ST s r
go Int
i = -Int
1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> ST s r -> ST s r -> ST s r
forall a. Bool -> a -> a -> a
? r -> ST s r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base (ST s r -> ST s r) -> ST s r -> ST s r
forall a b. (a -> b) -> a -> b
$ Int -> ST s r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ST s r -> ST s e -> (r -> e -> ST s r) -> ST s r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
>>=<< (STBytes# s e
arr STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i) ((r -> e -> ST s r) -> ST s r) -> (r -> e -> ST s r) -> ST s r
forall a b. (a -> b) -> a -> b
$ (Int -> r -> e -> ST s r
f Int
i)
      in  Int -> ST s r
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    
    foldrM :: (e -> r -> ST s r) -> r -> STBytes# s e -> ST s r
foldrM e -> r -> ST s r
f r
base = \ arr :: STBytes# s e
arr@(STBytes# Int
n Int
_ MutableByteArray# s
_) ->
      let go :: Int -> ST s r
go Int
i = Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> ST s r -> ST s r -> ST s r
forall a. Bool -> a -> a -> a
? r -> ST s r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base (ST s r -> ST s r) -> ST s r -> ST s r
forall a b. (a -> b) -> a -> b
$ (STBytes# s e
arr STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i) ST s e -> ST s r -> (e -> r -> ST s r) -> ST s r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
>>=<< Int -> ST s r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) ((e -> r -> ST s r) -> ST s r) -> (e -> r -> ST s r) -> ST s r
forall a b. (a -> b) -> a -> b
$ e -> r -> ST s r
f
      in  Int -> ST s r
go Int
0
    
    foldlM :: (r -> e -> ST s r) -> r -> STBytes# s e -> ST s r
foldlM r -> e -> ST s r
f r
base = \ arr :: STBytes# s e
arr@(STBytes# Int
n Int
_ MutableByteArray# s
_) ->
      let go :: Int -> ST s r
go Int
i = -Int
1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> ST s r -> ST s r -> ST s r
forall a. Bool -> a -> a -> a
? r -> ST s r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base (ST s r -> ST s r) -> ST s r -> ST s r
forall a b. (a -> b) -> a -> b
$ Int -> ST s r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ST s r -> ST s e -> (r -> e -> ST s r) -> ST s r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
>>=<< (STBytes# s e
arr STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i) ((r -> e -> ST s r) -> ST s r) -> (r -> e -> ST s r) -> ST s r
forall a b. (a -> b) -> a -> b
$ r -> e -> ST s r
f
      in  Int -> ST s r
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)

instance (Unboxed e) => SplitM (ST s) (STBytes# s e) e
  where
    takeM :: Int -> STBytes# s e -> ST s (STBytes# s e)
takeM Int
n es :: STBytes# s e
es@(STBytes# Int
c Int
o MutableByteArray# s
marr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => m l
newNull
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
es
      |  Bool
True  = STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
n Int
o MutableByteArray# s
marr#)
    
    dropM :: Int -> STBytes# s e -> ST s (STBytes# s e)
dropM Int
n es :: STBytes# s e
es@(STBytes# Int
c Int
o MutableByteArray# s
marr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => m l
newNull
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
es
      |  Bool
True  = STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n) MutableByteArray# s
marr#)
    
    keepM :: Int -> STBytes# s e -> ST s (STBytes# s e)
keepM Int
n es :: STBytes# s e
es@(STBytes# Int
c Int
o MutableByteArray# s
marr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => m l
newNull
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
es
      |  Bool
True  = STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
n (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
o) MutableByteArray# s
marr#)
    
    sansM :: Int -> STBytes# s e -> ST s (STBytes# s e)
sansM Int
n es :: STBytes# s e
es@(STBytes# Int
c Int
o MutableByteArray# s
marr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => m l
newNull
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
es
      |  Bool
True  = STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) Int
o MutableByteArray# s
marr#)
    
    splitM :: Int -> STBytes# s e -> ST s (STBytes# s e, STBytes# s e)
splitM Int
n es :: STBytes# s e
es@(STBytes# Int
c Int
o MutableByteArray# s
marr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = do STBytes# s e
e' <- ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => m l
newNull; (STBytes# s e, STBytes# s e) -> ST s (STBytes# s e, STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return (STBytes# s e
e', STBytes# s e
es)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = do STBytes# s e
e' <- ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => m l
newNull; (STBytes# s e, STBytes# s e) -> ST s (STBytes# s e, STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return (STBytes# s e
es, STBytes# s e
e')
      |  Bool
True  = (STBytes# s e, STBytes# s e) -> ST s (STBytes# s e, STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
n Int
o MutableByteArray# s
marr#, Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n) MutableByteArray# s
marr#)
    
    divideM :: Int -> STBytes# s e -> ST s (STBytes# s e, STBytes# s e)
divideM Int
n es :: STBytes# s e
es@(STBytes# Int
c Int
o MutableByteArray# s
marr#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = do STBytes# s e
e' <- ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => m l
newNull; (STBytes# s e, STBytes# s e) -> ST s (STBytes# s e, STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return (STBytes# s e
es, STBytes# s e
e')
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = do STBytes# s e
e' <- ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => m l
newNull; (STBytes# s e, STBytes# s e) -> ST s (STBytes# s e, STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return (STBytes# s e
e', STBytes# s e
es)
      |  Bool
True  = (STBytes# s e, STBytes# s e) -> ST s (STBytes# s e, STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
n (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
o) MutableByteArray# s
marr#, Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) Int
o MutableByteArray# s
marr#)
    
    prefixM :: (e -> Bool) -> STBytes# s e -> ST s Int
prefixM e -> Bool
p es :: STBytes# s e
es@(STBytes# Int
c Int
_ MutableByteArray# s
_) =
      let go :: Int -> ST s Int
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c Bool -> ST s Int -> ST s Int -> ST s Int
forall a. Bool -> a -> a -> a
? Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c (ST s Int -> ST s Int) -> ST s Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ do e
e <- STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e -> Bool
p e
e Bool -> ST s Int -> ST s Int -> ST s Int
forall a. Bool -> a -> a -> a
? Int -> ST s Int
go (Int -> Int
forall a. Enum a => a -> a
succ Int
i) (ST s Int -> ST s Int) -> ST s Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
      in  Int -> ST s Int
go Int
0
    
    suffixM :: (e -> Bool) -> STBytes# s e -> ST s Int
suffixM e -> Bool
p es :: STBytes# s e
es@(STBytes# Int
c Int
_ MutableByteArray# s
_) =
      let go :: Int -> ST s Int
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> ST s Int -> ST s Int -> ST s Int
forall a. Bool -> a -> a -> a
? Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c (ST s Int -> ST s Int) -> ST s Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ do e
e <- STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e -> Bool
p e
e Bool -> ST s Int -> ST s Int -> ST s Int
forall a. Bool -> a -> a -> a
? Int -> ST s Int
go (Int -> Int
forall a. Enum a => a -> a
pred Int
i) (ST s Int -> ST s Int) -> ST s Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
      in  Int -> ST s Int
go (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
    
    mprefix :: (e -> ST s Bool) -> STBytes# s e -> ST s Int
mprefix e -> ST s Bool
p es :: STBytes# s e
es@(STBytes# Int
c Int
_ MutableByteArray# s
_) =
      let go :: Int -> ST s Int
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c Bool -> ST s Int -> ST s Int -> ST s Int
forall a. Bool -> a -> a -> a
? Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c (ST s Int -> ST s Int) -> ST s Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ do e
e <- STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e -> ST s Bool
p e
e ST s Bool -> ST s Int -> ST s Int -> ST s Int
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
?^ Int -> ST s Int
go (Int -> Int
forall a. Enum a => a -> a
succ Int
1) (ST s Int -> ST s Int) -> ST s Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
      in  Int -> ST s Int
go Int
0
    
    msuffix :: (e -> ST s Bool) -> STBytes# s e -> ST s Int
msuffix e -> ST s Bool
p es :: STBytes# s e
es@(STBytes# Int
c Int
_ MutableByteArray# s
_) =
      let go :: Int -> ST s Int
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> ST s Int -> ST s Int -> ST s Int
forall a. Bool -> a -> a -> a
? Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c (ST s Int -> ST s Int) -> ST s Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ do e
e <- STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e -> ST s Bool
p e
e ST s Bool -> ST s Int -> ST s Int -> ST s Int
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
?^ Int -> ST s Int
go (Int -> Int
forall a. Enum a => a -> a
pred Int
i) (ST s Int -> ST s Int) -> ST s Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
      in  Int -> ST s Int
go (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))

--------------------------------------------------------------------------------

{- MapM, IndexedM and SortM instances. -}

instance (Unboxed e) => MapM (ST s) (STBytes# s e) Int e
  where
    newMap :: [(Int, e)] -> ST s (STBytes# s e)
newMap [(Int, e)]
ascs = (Int, Int) -> [(Int, e)] -> ST s (STBytes# s e)
forall (m :: * -> *) v i e.
IndexedM m v i e =>
(i, i) -> [(i, e)] -> m v
fromAssocs ([(Int, e)] -> (Int, Int)
forall a b. Ord a => [(a, b)] -> (a, a)
ascsBounds [(Int, e)]
ascs) [(Int, e)]
ascs
    
    newMap' :: e -> [(Int, e)] -> ST s (STBytes# s e)
newMap' e
defvalue [(Int, e)]
ascs = (Int, Int) -> e -> [(Int, e)] -> ST s (STBytes# s e)
forall (m :: * -> *) v i e.
IndexedM m v i e =>
(i, i) -> e -> [(i, e)] -> m v
fromAssocs' ([(Int, e)] -> (Int, Int)
forall a b. Ord a => [(a, b)] -> (a, a)
ascsBounds [(Int, e)]
ascs) e
defvalue [(Int, e)]
ascs
    
    >! :: STBytes# s e -> Int -> ST s e
(>!) = STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
(!#>)
    
    overwrite :: STBytes# s e -> [(Int, e)] -> ST s (STBytes# s e)
overwrite es :: STBytes# s e
es@(STBytes# Int
c Int
_ MutableByteArray# s
_) [(Int, e)]
ascs =
      let ies :: [(Int, e)]
ies = ((Int, e) -> Bool) -> [(Int, e)] -> [(Int, e)]
forall l e. Linear l e => (e -> Bool) -> l -> l
filter ((Int, Int) -> Int -> Bool
forall i. Index i => (i, i) -> i -> Bool
inRange (Int
0, Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Int -> Bool) -> ((Int, e) -> Int) -> (Int, e) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, e) -> Int
forall a b. (a, b) -> a
fst) [(Int, e)]
ascs
      in  ((Int, e) -> ST s ()) -> [(Int, e)] -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((Int -> e -> ST s ()) -> (Int, e) -> ST s ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((Int -> e -> ST s ()) -> (Int, e) -> ST s ())
-> (Int -> e -> ST s ()) -> (Int, e) -> ST s ()
forall a b. (a -> b) -> a -> b
$ STBytes# s e -> Int -> e -> ST s ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM STBytes# s e
es) [(Int, e)]
ies ST s () -> ST s (STBytes# s e) -> ST s (STBytes# s e)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
es
    
    kfoldrM :: (Int -> e -> acc -> ST s acc) -> acc -> STBytes# s e -> ST s acc
kfoldrM = (Int -> e -> acc -> ST s acc) -> acc -> STBytes# s e -> ST s acc
forall (m :: * -> *) l e r.
LinearM m l e =>
(Int -> e -> r -> m r) -> r -> l -> m r
ofoldrM
    kfoldlM :: (Int -> acc -> e -> ST s acc) -> acc -> STBytes# s e -> ST s acc
kfoldlM = (Int -> acc -> e -> ST s acc) -> acc -> STBytes# s e -> ST s acc
forall (m :: * -> *) l e r.
LinearM m l e =>
(Int -> r -> e -> m r) -> r -> l -> m r
ofoldlM

instance (Unboxed e) => IndexedM (ST s) (STBytes# s e) Int e
  where
    fromAssocs :: (Int, Int) -> [(Int, e)] -> ST s (STBytes# s e)
fromAssocs  (Int, Int)
bnds [(Int, e)]
ascs = Int -> ST s (STBytes# s e)
forall e s. Unboxed e => Int -> ST s (STBytes# s e)
alloc ((Int, Int) -> Int
forall i. Index i => (i, i) -> Int
size (Int, Int)
bnds) ST s (STBytes# s e)
-> (STBytes# s e -> ST s (STBytes# s e)) -> ST s (STBytes# s e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (STBytes# s e -> [(Int, e)] -> ST s (STBytes# s e))
-> [(Int, e)] -> STBytes# s e -> ST s (STBytes# s e)
forall a b c. (a -> b -> c) -> b -> a -> c
flip STBytes# s e -> [(Int, e)] -> ST s (STBytes# s e)
forall (m :: * -> *) map key e.
MapM m map key e =>
map -> [(key, e)] -> m map
overwrite [(Int, e)]
ascs
    fromAssocs' :: (Int, Int) -> e -> [(Int, e)] -> ST s (STBytes# s e)
fromAssocs' (Int, Int)
bnds e
defvalue [(Int, e)]
ascs = (Int, Int) -> Int
forall i. Index i => (i, i) -> Int
size (Int, Int)
bnds Int -> e -> ST s (STBytes# s e)
forall (m :: * -> *) l e. LinearM m l e => Int -> e -> m l
`filled` e
defvalue ST s (STBytes# s e)
-> (STBytes# s e -> ST s (STBytes# s e)) -> ST s (STBytes# s e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (STBytes# s e -> [(Int, e)] -> ST s (STBytes# s e)
forall (m :: * -> *) map key e.
MapM m map key e =>
map -> [(key, e)] -> m map
`overwrite` [(Int, e)]
ascs)
    
    {-# INLINE writeM' #-}
    writeM' :: STBytes# s e -> Int -> e -> ST s ()
writeM' (STBytes# Int
_ (I# Int#
o#) MutableByteArray# s
marr#) = \ (I# Int#
i#) e
e -> STRep s () -> ST s ()
forall s a. STRep s a -> ST s a
ST (STRep s () -> ST s ()) -> STRep s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
      \ State# s
s1# -> case MutableByteArray# s -> Int# -> e -> State# s -> State# s
forall e s.
Unboxed e =>
MutableByteArray# s -> Int# -> e -> State# s -> State# s
writeByteArray# MutableByteArray# s
marr# (Int#
o# Int# -> Int# -> Int#
+# Int#
i#) e
e State# s
s1# of
        State# s
s2# -> (# State# s
s2#, () #)
    
    fromIndexed' :: v' -> ST s (STBytes# s e)
fromIndexed' v'
es = do
      let n :: Int
n = v' -> Int
forall b i. Bordered b i => b -> Int
sizeOf v'
es
      STBytes# s e
copy <- Int -> ST s (STBytes# s e)
forall e s. Unboxed e => Int -> ST s (STBytes# s e)
alloc Int
n
      [Int] -> (Int -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> ST s ()) -> ST s ()) -> (Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \ Int
i -> STBytes# s e -> Int -> e -> ST s ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM STBytes# s e
copy Int
i (v'
es v' -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i)
      STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
copy
    
    fromIndexedM :: v' -> ST s (STBytes# s e)
fromIndexedM v'
es = do
      Int
n    <- v' -> ST s Int
forall (m :: * -> *) b i. BorderedM m b i => b -> m Int
getSizeOf v'
es
      STBytes# s e
copy <- Int -> ST s (STBytes# s e)
forall e s. Unboxed e => Int -> ST s (STBytes# s e)
alloc Int
n
      [Int] -> (Int -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> ST s ()) -> ST s ()) -> (Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \ Int
i -> v'
es v' -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i ST s e -> (e -> ST s ()) -> ST s ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= STBytes# s e -> Int -> e -> ST s ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM STBytes# s e
copy Int
i
      STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
copy

instance (Unboxed e) => SortM (ST s) (STBytes# s e) e
  where
    sortedMBy :: (e -> e -> Bool) -> STBytes# s e -> ST s Bool
sortedMBy e -> e -> Bool
f es :: STBytes# s e
es@(STBytes# Int
n Int
_ MutableByteArray# s
_) =
      let go :: Int -> e -> ST s Bool
go Int
i e
e1 = Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n Bool -> ST s Bool -> ST s Bool -> ST s Bool
forall a. Bool -> a -> a -> a
? Bool -> ST s Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True (ST s Bool -> ST s Bool) -> ST s Bool -> ST s Bool
forall a b. (a -> b) -> a -> b
$ do e
e2 <- STBytes# s e
es STBytes# s e -> Int -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e
e1 e -> e -> Bool
`f` e
e2 Bool -> ST s Bool -> ST s Bool -> ST s Bool
forall a. Bool -> a -> a -> a
? Int -> e -> ST s Bool
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) e
e2 (ST s Bool -> ST s Bool) -> ST s Bool -> ST s Bool
forall a b. (a -> b) -> a -> b
$ Bool -> ST s Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
      in  Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
2 Bool -> ST s Bool -> ST s Bool -> ST s Bool
forall a. Bool -> a -> a -> a
? Bool -> ST s Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True (ST s Bool -> ST s Bool) -> ST s Bool -> ST s Bool
forall a b. (a -> b) -> a -> b
$ Int -> e -> ST s Bool
go Int
1 (e -> ST s Bool) -> ST s e -> ST s Bool
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< STBytes# s e -> ST s e
forall (m :: * -> *) l e. LinearM m l e => l -> m e
getHead STBytes# s e
es
    
    sortMBy :: Compare e -> STBytes# s e -> ST s ()
sortMBy = Compare e -> STBytes# s e -> ST s ()
forall (m :: * -> *) v e i.
(LinearM m v e, BorderedM m v i) =>
Compare e -> v -> m ()
timSortBy

--------------------------------------------------------------------------------

-- | 'MIOBytes#' is mutable pseudo-primitive 'Int'-indexed strict unboxed array.
newtype MIOBytes# (io :: Type -> Type) e = MIOBytes# (STBytes# RealWorld e)
  deriving ( MIOBytes# io e -> MIOBytes# io e -> Bool
(MIOBytes# io e -> MIOBytes# io e -> Bool)
-> (MIOBytes# io e -> MIOBytes# io e -> Bool)
-> Eq (MIOBytes# io e)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (io :: * -> *) e. MIOBytes# io e -> MIOBytes# io e -> Bool
/= :: MIOBytes# io e -> MIOBytes# io e -> Bool
$c/= :: forall (io :: * -> *) e. MIOBytes# io e -> MIOBytes# io e -> Bool
== :: MIOBytes# io e -> MIOBytes# io e -> Bool
$c== :: forall (io :: * -> *) e. MIOBytes# io e -> MIOBytes# io e -> Bool
Eq )

-- | 'IOBytes#' is mutable pseudo-primitive 'Int'-indexed strict unboxed array.
type IOBytes# = MIOBytes# IO

{-# INLINE unpack #-}
unpack :: MIOBytes# io e -> STBytes# RealWorld e
unpack :: MIOBytes# io e -> STBytes# RealWorld e
unpack =  MIOBytes# io e -> STBytes# RealWorld e
coerce

{-# INLINE pack #-}
pack :: (MonadIO io) => ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack :: ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack =  ST RealWorld (MIOBytes# io e) -> io (MIOBytes# io e)
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld (MIOBytes# io e) -> io (MIOBytes# io e))
-> (ST RealWorld (STBytes# RealWorld e)
    -> ST RealWorld (MIOBytes# io e))
-> ST RealWorld (STBytes# RealWorld e)
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ST RealWorld (STBytes# RealWorld e)
-> ST RealWorld (MIOBytes# io e)
coerce

--------------------------------------------------------------------------------

{- Estimate, Bordered and BorderedM instances. -}

instance Estimate (MIOBytes# io e)
  where
    <==> :: Compare (MIOBytes# io e)
(<==>) = Compare Int -> (MIOBytes# io e -> Int) -> Compare (MIOBytes# io e)
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Compare Int
forall o. Ord o => Compare o
(<=>) MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<=. :: MIOBytes# io e -> MIOBytes# io e -> Bool
(.<=.) = (Int -> Int -> Bool)
-> (MIOBytes# io e -> Int)
-> MIOBytes# io e
-> MIOBytes# io e
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<=)  MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>=. :: MIOBytes# io e -> MIOBytes# io e -> Bool
(.>=.) = (Int -> Int -> Bool)
-> (MIOBytes# io e -> Int)
-> MIOBytes# io e
-> MIOBytes# io e
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>=)  MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>. :: MIOBytes# io e -> MIOBytes# io e -> Bool
(.>.)  = (Int -> Int -> Bool)
-> (MIOBytes# io e -> Int)
-> MIOBytes# io e
-> MIOBytes# io e
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>)   MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<. :: MIOBytes# io e -> MIOBytes# io e -> Bool
(.<.)  = (Int -> Int -> Bool)
-> (MIOBytes# io e -> Int)
-> MIOBytes# io e
-> MIOBytes# io e
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<)   MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    
    <.=> :: MIOBytes# io e -> Int -> Ordering
(<.=>) = Compare Int
forall o. Ord o => Compare o
(<=>) Compare Int
-> (MIOBytes# io e -> Int) -> MIOBytes# io e -> Int -> Ordering
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>= :: MIOBytes# io e -> Int -> Bool
(.>=)  = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>=)  (Int -> Int -> Bool)
-> (MIOBytes# io e -> Int) -> MIOBytes# io e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<= :: MIOBytes# io e -> Int -> Bool
(.<=)  = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<=)  (Int -> Int -> Bool)
-> (MIOBytes# io e -> Int) -> MIOBytes# io e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .> :: MIOBytes# io e -> Int -> Bool
(.>)   = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>)   (Int -> Int -> Bool)
-> (MIOBytes# io e -> Int) -> MIOBytes# io e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .< :: MIOBytes# io e -> Int -> Bool
(.<)   = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<)   (Int -> Int -> Bool)
-> (MIOBytes# io e -> Int) -> MIOBytes# io e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf

instance Bordered (MIOBytes# io e) Int
  where
    lower :: MIOBytes# io e -> Int
lower = Int -> MIOBytes# io e -> Int
forall a b. a -> b -> a
const Int
0
    
    sizeOf :: MIOBytes# io e -> Int
sizeOf   (MIOBytes# (STBytes# Int
c Int
_ MutableByteArray# RealWorld
_)) = Int
c
    upper :: MIOBytes# io e -> Int
upper    (MIOBytes# (STBytes# Int
c Int
_ MutableByteArray# RealWorld
_)) = Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
    bounds :: MIOBytes# io e -> (Int, Int)
bounds   (MIOBytes# (STBytes# Int
c Int
_ MutableByteArray# RealWorld
_)) = (Int
0, Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    indices :: MIOBytes# io e -> [Int]
indices  (MIOBytes# (STBytes# Int
c Int
_ MutableByteArray# RealWorld
_)) = [Int
0 .. Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
    indexOf :: MIOBytes# io e -> Int -> Int
indexOf  (MIOBytes# (STBytes# Int
c Int
_ MutableByteArray# RealWorld
_)) = (Int, Int) -> Int -> Int
forall i. Index i => (i, i) -> Int -> i
index (Int
0, Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    offsetOf :: MIOBytes# io e -> Int -> Int
offsetOf (MIOBytes# (STBytes# Int
c Int
_ MutableByteArray# RealWorld
_)) = (Int, Int) -> Int -> Int
forall i. Index i => (i, i) -> i -> Int
offset (Int
0, Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    indexIn :: MIOBytes# io e -> Int -> Bool
indexIn  (MIOBytes# (STBytes# Int
c Int
_ MutableByteArray# RealWorld
_)) = \ Int
i -> Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
c

instance (MonadIO io) => BorderedM io (MIOBytes# io e) Int
  where
    getIndexOf :: MIOBytes# io e -> Int -> io Int
getIndexOf = Int -> io Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> io Int)
-> (MIOBytes# io e -> Int -> Int)
-> MIOBytes# io e
-> Int
-> io Int
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... STBytes# RealWorld e -> Int -> Int
forall b i. Bordered b i => b -> Int -> i
indexOf (STBytes# RealWorld e -> Int -> Int)
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> Int
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    getIndices :: MIOBytes# io e -> io [Int]
getIndices = [Int] -> io [Int]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Int] -> io [Int])
-> (MIOBytes# io e -> [Int]) -> MIOBytes# io e -> io [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> [Int]
forall b i. Bordered b i => b -> [i]
indices (STBytes# RealWorld e -> [Int])
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    getSizeOf :: MIOBytes# io e -> io Int
getSizeOf  = Int -> io Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> io Int)
-> (MIOBytes# io e -> Int) -> MIOBytes# io e -> io Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> Int
forall b i. Bordered b i => b -> Int
sizeOf (STBytes# RealWorld e -> Int)
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    getBounds :: MIOBytes# io e -> io (Int, Int)
getBounds  = (Int, Int) -> io (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Int, Int) -> io (Int, Int))
-> (MIOBytes# io e -> (Int, Int))
-> MIOBytes# io e
-> io (Int, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> (Int, Int)
forall b i. Bordered b i => b -> (i, i)
bounds (STBytes# RealWorld e -> (Int, Int))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> (Int, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    getUpper :: MIOBytes# io e -> io Int
getUpper   = Int -> io Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> io Int)
-> (MIOBytes# io e -> Int) -> MIOBytes# io e -> io Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> Int
forall b i. Bordered b i => b -> i
upper (STBytes# RealWorld e -> Int)
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    getLower :: MIOBytes# io e -> io Int
getLower MIOBytes# io e
_ = Int -> io Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0

--------------------------------------------------------------------------------

{- LinearM and SplitM instances. -}

instance (MonadIO io, Unboxed e) => LinearM io (MIOBytes# io e) e
  where
    newNull :: io (MIOBytes# io e)
newNull = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e. LinearM m l e => m l
newNull
    singleM :: e -> io (MIOBytes# io e)
singleM = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (e -> ST RealWorld (STBytes# RealWorld e))
-> e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e. LinearM m l e => e -> m l
singleM
    nowNull :: MIOBytes# io e -> io Bool
nowNull = ST RealWorld Bool -> io Bool
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld Bool -> io Bool)
-> (MIOBytes# io e -> ST RealWorld Bool)
-> MIOBytes# io e
-> io Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> ST RealWorld Bool
forall (m :: * -> *) l e. LinearM m l e => l -> m Bool
nowNull (STBytes# RealWorld e -> ST RealWorld Bool)
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    getHead :: MIOBytes# io e -> io e
getHead = ST RealWorld e -> io e
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld e -> io e)
-> (MIOBytes# io e -> ST RealWorld e) -> MIOBytes# io e -> io e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> ST RealWorld e
forall (m :: * -> *) l e. LinearM m l e => l -> m e
getHead (STBytes# RealWorld e -> ST RealWorld e)
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    getLast :: MIOBytes# io e -> io e
getLast = ST RealWorld e -> io e
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld e -> io e)
-> (MIOBytes# io e -> ST RealWorld e) -> MIOBytes# io e -> io e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> ST RealWorld e
forall (m :: * -> *) l e. LinearM m l e => l -> m e
getLast (STBytes# RealWorld e -> ST RealWorld e)
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    
    prepend :: e -> MIOBytes# io e -> io (MIOBytes# io e)
prepend e
e = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (MIOBytes# io e -> ST RealWorld (STBytes# RealWorld e))
-> MIOBytes# io e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e. LinearM m l e => e -> l -> m l
prepend e
e (STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld (STBytes# RealWorld e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    append :: MIOBytes# io e -> e -> io (MIOBytes# io e)
append MIOBytes# io e
es = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (e -> ST RealWorld (STBytes# RealWorld e))
-> e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e. LinearM m l e => l -> e -> m l
append (MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack MIOBytes# io e
es)
    
    newLinear :: [e] -> io (MIOBytes# io e)
newLinear     = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> ([e] -> ST RealWorld (STBytes# RealWorld e))
-> [e]
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [e] -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e. LinearM m l e => [e] -> m l
newLinear
    newLinearN :: Int -> [e] -> io (MIOBytes# io e)
newLinearN    = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (Int -> [e] -> ST RealWorld (STBytes# RealWorld e))
-> Int
-> [e]
-> io (MIOBytes# io e)
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... Int -> [e] -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e. LinearM m l e => Int -> [e] -> m l
newLinearN
    fromFoldableM :: f e -> io (MIOBytes# io e)
fromFoldableM = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (f e -> ST RealWorld (STBytes# RealWorld e))
-> f e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e (f :: * -> *).
(LinearM m l e, Foldable f) =>
f e -> m l
fromFoldableM
    
    !#> :: MIOBytes# io e -> Int -> io e
(!#>) = ST RealWorld e -> io e
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld e -> io e)
-> (MIOBytes# io e -> Int -> ST RealWorld e)
-> MIOBytes# io e
-> Int
-> io e
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... STBytes# RealWorld e -> Int -> ST RealWorld e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
(!#>) (STBytes# RealWorld e -> Int -> ST RealWorld e)
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> Int
-> ST RealWorld e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    
    writeM :: MIOBytes# io e -> Int -> e -> io ()
writeM MIOBytes# io e
es = ST RealWorld () -> io ()
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld () -> io ())
-> (Int -> e -> ST RealWorld ()) -> Int -> e -> io ()
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... STBytes# RealWorld e -> Int -> e -> ST RealWorld ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM (MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack MIOBytes# io e
es)
    
    copied :: MIOBytes# io e -> io (MIOBytes# io e)
copied   = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack  (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (MIOBytes# io e -> ST RealWorld (STBytes# RealWorld e))
-> MIOBytes# io e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e. LinearM m l e => l -> m l
copied   (STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld (STBytes# RealWorld e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    getLeft :: MIOBytes# io e -> io [e]
getLeft  = ST RealWorld [e] -> io [e]
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld [e] -> io [e])
-> (MIOBytes# io e -> ST RealWorld [e]) -> MIOBytes# io e -> io [e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> ST RealWorld [e]
forall (m :: * -> *) l e. LinearM m l e => l -> m [e]
getLeft  (STBytes# RealWorld e -> ST RealWorld [e])
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld [e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    reversed :: MIOBytes# io e -> io (MIOBytes# io e)
reversed = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack  (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (MIOBytes# io e -> ST RealWorld (STBytes# RealWorld e))
-> MIOBytes# io e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e. LinearM m l e => l -> m l
reversed (STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld (STBytes# RealWorld e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    getRight :: MIOBytes# io e -> io [e]
getRight = ST RealWorld [e] -> io [e]
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld [e] -> io [e])
-> (MIOBytes# io e -> ST RealWorld [e]) -> MIOBytes# io e -> io [e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> ST RealWorld [e]
forall (m :: * -> *) l e. LinearM m l e => l -> m [e]
getRight (STBytes# RealWorld e -> ST RealWorld [e])
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld [e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    
    copied' :: MIOBytes# io e -> Int -> Int -> io (MIOBytes# io e)
copied' MIOBytes# io e
es = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (Int -> Int -> ST RealWorld (STBytes# RealWorld e))
-> Int
-> Int
-> io (MIOBytes# io e)
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... STBytes# RealWorld e
-> Int -> Int -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> Int -> m l
copied' (MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack MIOBytes# io e
es)
    
    merged :: f (MIOBytes# io e) -> io (MIOBytes# io e)
merged = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack  (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (f (MIOBytes# io e) -> ST RealWorld (STBytes# RealWorld e))
-> f (MIOBytes# io e)
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  [STBytes# RealWorld e] -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e (f :: * -> *).
(LinearM m l e, Foldable f) =>
f l -> m l
merged ([STBytes# RealWorld e] -> ST RealWorld (STBytes# RealWorld e))
-> (f (MIOBytes# io e) -> [STBytes# RealWorld e])
-> f (MIOBytes# io e)
-> ST RealWorld (STBytes# RealWorld e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MIOBytes# io e
 -> [STBytes# RealWorld e] -> [STBytes# RealWorld e])
-> [STBytes# RealWorld e]
-> f (MIOBytes# io e)
-> [STBytes# RealWorld e]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((:) (STBytes# RealWorld e
 -> [STBytes# RealWorld e] -> [STBytes# RealWorld e])
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> [STBytes# RealWorld e]
-> [STBytes# RealWorld e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack) []
    filled :: Int -> e -> io (MIOBytes# io e)
filled = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (Int -> e -> ST RealWorld (STBytes# RealWorld e))
-> Int
-> e
-> io (MIOBytes# io e)
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... Int -> e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) l e. LinearM m l e => Int -> e -> m l
filled
    
    copyTo :: MIOBytes# io e -> Int -> MIOBytes# io e -> Int -> Int -> io ()
copyTo MIOBytes# io e
src Int
so MIOBytes# io e
trg Int
to = ST RealWorld () -> io ()
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld () -> io ())
-> (Int -> ST RealWorld ()) -> Int -> io ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e
-> Int -> STBytes# RealWorld e -> Int -> Int -> ST RealWorld ()
forall (m :: * -> *) l e.
LinearM m l e =>
l -> Int -> l -> Int -> Int -> m ()
copyTo (MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack MIOBytes# io e
src) Int
so (MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack MIOBytes# io e
trg) Int
to
    
    ofoldrM :: (Int -> e -> r -> io r) -> r -> MIOBytes# io e -> io r
ofoldrM Int -> e -> r -> io r
f r
base = \ arr :: MIOBytes# io e
arr@(MIOBytes# (STBytes# Int
n Int
_ MutableByteArray# RealWorld
_)) ->
      let go :: Int -> io r
go Int
i =  Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> io r -> io r -> io r
forall a. Bool -> a -> a -> a
? r -> io r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base (io r -> io r) -> io r -> io r
forall a b. (a -> b) -> a -> b
$ (MIOBytes# io e
arr MIOBytes# io e -> Int -> io e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i) io e -> io r -> (e -> r -> io r) -> io r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
>>=<< Int -> io r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) ((e -> r -> io r) -> io r) -> (e -> r -> io r) -> io r
forall a b. (a -> b) -> a -> b
$ Int -> e -> r -> io r
f Int
i
      in  Int -> io r
go Int
0
    
    ofoldlM :: (Int -> r -> e -> io r) -> r -> MIOBytes# io e -> io r
ofoldlM Int -> r -> e -> io r
f r
base = \ arr :: MIOBytes# io e
arr@(MIOBytes# (STBytes# Int
n Int
_ MutableByteArray# RealWorld
_)) ->
      let go :: Int -> io r
go Int
i = -Int
1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> io r -> io r -> io r
forall a. Bool -> a -> a -> a
? r -> io r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base (io r -> io r) -> io r -> io r
forall a b. (a -> b) -> a -> b
$ Int -> io r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) io r -> io e -> (r -> e -> io r) -> io r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
>>=<< (MIOBytes# io e
arr MIOBytes# io e -> Int -> io e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i) ((r -> e -> io r) -> io r) -> (r -> e -> io r) -> io r
forall a b. (a -> b) -> a -> b
$ (Int -> r -> e -> io r
f Int
i)
      in  Int -> io r
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    
    foldrM :: (e -> r -> io r) -> r -> MIOBytes# io e -> io r
foldrM e -> r -> io r
f r
base MIOBytes# io e
arr =
      let go :: Int -> io r
go Int
i = MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf MIOBytes# io e
arr Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> io r -> io r -> io r
forall a. Bool -> a -> a -> a
? r -> io r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base (io r -> io r) -> io r -> io r
forall a b. (a -> b) -> a -> b
$ (MIOBytes# io e
arr MIOBytes# io e -> Int -> io e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i) io e -> io r -> (e -> r -> io r) -> io r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
>>=<< Int -> io r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) ((e -> r -> io r) -> io r) -> (e -> r -> io r) -> io r
forall a b. (a -> b) -> a -> b
$ e -> r -> io r
f
      in  Int -> io r
go Int
0
    
    foldlM :: (r -> e -> io r) -> r -> MIOBytes# io e -> io r
foldlM r -> e -> io r
f r
base MIOBytes# io e
arr =
      let go :: Int -> io r
go Int
i = -Int
1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Bool -> io r -> io r -> io r
forall a. Bool -> a -> a -> a
? r -> io r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base (io r -> io r) -> io r -> io r
forall a b. (a -> b) -> a -> b
$ Int -> io r
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) io r -> io e -> (r -> e -> io r) -> io r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
>>=<< (MIOBytes# io e
arr MIOBytes# io e -> Int -> io e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i) ((r -> e -> io r) -> io r) -> (r -> e -> io r) -> io r
forall a b. (a -> b) -> a -> b
$ r -> e -> io r
f
      in  Int -> io r
go (MIOBytes# io e -> Int
forall b i. Bordered b i => b -> Int
sizeOf MIOBytes# io e
arr Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)

instance (MonadIO io, Unboxed e) => SplitM io (MIOBytes# io e) e
  where
    takeM :: Int -> MIOBytes# io e -> io (MIOBytes# io e)
takeM Int
n = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (MIOBytes# io e -> ST RealWorld (STBytes# RealWorld e))
-> MIOBytes# io e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) s e. SplitM m s e => Int -> s -> m s
takeM Int
n (STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld (STBytes# RealWorld e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    dropM :: Int -> MIOBytes# io e -> io (MIOBytes# io e)
dropM Int
n = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (MIOBytes# io e -> ST RealWorld (STBytes# RealWorld e))
-> MIOBytes# io e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) s e. SplitM m s e => Int -> s -> m s
dropM Int
n (STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld (STBytes# RealWorld e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    keepM :: Int -> MIOBytes# io e -> io (MIOBytes# io e)
keepM Int
n = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (MIOBytes# io e -> ST RealWorld (STBytes# RealWorld e))
-> MIOBytes# io e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) s e. SplitM m s e => Int -> s -> m s
keepM Int
n (STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld (STBytes# RealWorld e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    sansM :: Int -> MIOBytes# io e -> io (MIOBytes# io e)
sansM Int
n = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (MIOBytes# io e -> ST RealWorld (STBytes# RealWorld e))
-> MIOBytes# io e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) s e. SplitM m s e => Int -> s -> m s
sansM Int
n (STBytes# RealWorld e -> ST RealWorld (STBytes# RealWorld e))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld (STBytes# RealWorld e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    
    prefixM :: (e -> Bool) -> MIOBytes# io e -> io Int
prefixM e -> Bool
f = ST RealWorld Int -> io Int
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld Int -> io Int)
-> (MIOBytes# io e -> ST RealWorld Int) -> MIOBytes# io e -> io Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> Bool) -> STBytes# RealWorld e -> ST RealWorld Int
forall (m :: * -> *) s e. SplitM m s e => (e -> Bool) -> s -> m Int
prefixM e -> Bool
f (STBytes# RealWorld e -> ST RealWorld Int)
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    suffixM :: (e -> Bool) -> MIOBytes# io e -> io Int
suffixM e -> Bool
f = ST RealWorld Int -> io Int
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld Int -> io Int)
-> (MIOBytes# io e -> ST RealWorld Int) -> MIOBytes# io e -> io Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> Bool) -> STBytes# RealWorld e -> ST RealWorld Int
forall (m :: * -> *) s e. SplitM m s e => (e -> Bool) -> s -> m Int
suffixM e -> Bool
f (STBytes# RealWorld e -> ST RealWorld Int)
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    
    mprefix :: (e -> io Bool) -> MIOBytes# io e -> io Int
mprefix e -> io Bool
p es :: MIOBytes# io e
es@(MIOBytes# (STBytes# Int
c Int
_ MutableByteArray# RealWorld
_)) =
      let go :: Int -> io Int
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c Bool -> io Int -> io Int -> io Int
forall a. Bool -> a -> a -> a
? Int -> io Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c (io Int -> io Int) -> io Int -> io Int
forall a b. (a -> b) -> a -> b
$ do e
e <- MIOBytes# io e
es MIOBytes# io e -> Int -> io e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e -> io Bool
p e
e io Bool -> io Int -> io Int -> io Int
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
?^ Int -> io Int
go (Int -> Int
forall a. Enum a => a -> a
succ Int
1) (io Int -> io Int) -> io Int -> io Int
forall a b. (a -> b) -> a -> b
$ Int -> io Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
      in  Int -> io Int
go Int
0
    
    msuffix :: (e -> io Bool) -> MIOBytes# io e -> io Int
msuffix e -> io Bool
p es :: MIOBytes# io e
es@(MIOBytes# (STBytes# Int
c Int
_ MutableByteArray# RealWorld
_)) =
      let go :: Int -> io Int
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> io Int -> io Int -> io Int
forall a. Bool -> a -> a -> a
? Int -> io Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c (io Int -> io Int) -> io Int -> io Int
forall a b. (a -> b) -> a -> b
$ do e
e <- MIOBytes# io e
es MIOBytes# io e -> Int -> io e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e -> io Bool
p e
e io Bool -> io Int -> io Int -> io Int
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
?^ Int -> io Int
go (Int -> Int
forall a. Enum a => a -> a
pred Int
i) (io Int -> io Int) -> io Int -> io Int
forall a b. (a -> b) -> a -> b
$ Int -> io Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
      in  Int -> io Int
go (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))

--------------------------------------------------------------------------------

{- MapM, IndexedM and SortM instances. -}

instance (MonadIO io, Unboxed e) => MapM io (MIOBytes# io e) Int e
  where
    newMap' :: e -> [(Int, e)] -> io (MIOBytes# io e)
newMap' = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (e -> [(Int, e)] -> ST RealWorld (STBytes# RealWorld e))
-> e
-> [(Int, e)]
-> io (MIOBytes# io e)
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... e -> [(Int, e)] -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) map key e.
MapM m map key e =>
e -> [(key, e)] -> m map
newMap'
    newMap :: [(Int, e)] -> io (MIOBytes# io e)
newMap  = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack  (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> ([(Int, e)] -> ST RealWorld (STBytes# RealWorld e))
-> [(Int, e)]
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  [(Int, e)] -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) map key e.
MapM m map key e =>
[(key, e)] -> m map
newMap
    
    >! :: MIOBytes# io e -> Int -> io e
(>!) = MIOBytes# io e -> Int -> io e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
(!#>)
    
    overwrite :: MIOBytes# io e -> [(Int, e)] -> io (MIOBytes# io e)
overwrite = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (MIOBytes# io e
    -> [(Int, e)] -> ST RealWorld (STBytes# RealWorld e))
-> MIOBytes# io e
-> [(Int, e)]
-> io (MIOBytes# io e)
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... STBytes# RealWorld e
-> [(Int, e)] -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) map key e.
MapM m map key e =>
map -> [(key, e)] -> m map
overwrite (STBytes# RealWorld e
 -> [(Int, e)] -> ST RealWorld (STBytes# RealWorld e))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> [(Int, e)]
-> ST RealWorld (STBytes# RealWorld e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    
    kfoldrM :: (Int -> e -> acc -> io acc) -> acc -> MIOBytes# io e -> io acc
kfoldrM = (Int -> e -> acc -> io acc) -> acc -> MIOBytes# io e -> io acc
forall (m :: * -> *) l e r.
LinearM m l e =>
(Int -> e -> r -> m r) -> r -> l -> m r
ofoldrM
    kfoldlM :: (Int -> acc -> e -> io acc) -> acc -> MIOBytes# io e -> io acc
kfoldlM = (Int -> acc -> e -> io acc) -> acc -> MIOBytes# io e -> io acc
forall (m :: * -> *) l e r.
LinearM m l e =>
(Int -> r -> e -> m r) -> r -> l -> m r
ofoldlM

instance (MonadIO io, Unboxed e) => IndexedM io (MIOBytes# io e) Int e
  where
    fromAssocs :: (Int, Int) -> [(Int, e)] -> io (MIOBytes# io e)
fromAssocs  (Int, Int)
bnds = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack  (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> ([(Int, e)] -> ST RealWorld (STBytes# RealWorld e))
-> [(Int, e)]
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  (Int, Int) -> [(Int, e)] -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) v i e.
IndexedM m v i e =>
(i, i) -> [(i, e)] -> m v
fromAssocs  (Int, Int)
bnds
    fromAssocs' :: (Int, Int) -> e -> [(Int, e)] -> io (MIOBytes# io e)
fromAssocs' (Int, Int)
bnds = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (e -> [(Int, e)] -> ST RealWorld (STBytes# RealWorld e))
-> e
-> [(Int, e)]
-> io (MIOBytes# io e)
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... (Int, Int)
-> e -> [(Int, e)] -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) v i e.
IndexedM m v i e =>
(i, i) -> e -> [(i, e)] -> m v
fromAssocs' (Int, Int)
bnds
    
    writeM' :: MIOBytes# io e -> Int -> e -> io ()
writeM' MIOBytes# io e
es = ST RealWorld () -> io ()
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld () -> io ())
-> (Int -> e -> ST RealWorld ()) -> Int -> e -> io ()
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... STBytes# RealWorld e -> Int -> e -> ST RealWorld ()
forall (m :: * -> *) v i e. IndexedM m v i e => v -> i -> e -> m ()
writeM' (MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack MIOBytes# io e
es)
    
    fromIndexed' :: v' -> io (MIOBytes# io e)
fromIndexed' = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (v' -> ST RealWorld (STBytes# RealWorld e))
-> v'
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v' -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) v i e v' j.
(IndexedM m v i e, Indexed v' j e) =>
v' -> m v
fromIndexed'
    
    fromIndexedM :: v' -> io (MIOBytes# io e)
fromIndexedM v'
es = do
      Int
n    <- v' -> io Int
forall (m :: * -> *) b i. BorderedM m b i => b -> m Int
getSizeOf v'
es
      MIOBytes# io e
copy <- Int -> e -> io (MIOBytes# io e)
forall (m :: * -> *) l e. LinearM m l e => Int -> e -> m l
filled Int
n (String -> e
forall a. String -> a
unreachEx String
"fromIndexedM")
      [Int] -> (Int -> io ()) -> io ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> io ()) -> io ()) -> (Int -> io ()) -> io ()
forall a b. (a -> b) -> a -> b
$ \ Int
i -> v'
es v' -> Int -> io e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i io e -> (e -> io ()) -> io ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MIOBytes# io e -> Int -> e -> io ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM MIOBytes# io e
copy Int
i
      MIOBytes# io e -> io (MIOBytes# io e)
forall (m :: * -> *) a. Monad m => a -> m a
return MIOBytes# io e
copy

instance (MonadIO io, Unboxed e) => SortM io (MIOBytes# io e) e
  where
    sortedMBy :: (e -> e -> Bool) -> MIOBytes# io e -> io Bool
sortedMBy e -> e -> Bool
f = ST RealWorld Bool -> io Bool
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld Bool -> io Bool)
-> (MIOBytes# io e -> ST RealWorld Bool)
-> MIOBytes# io e
-> io Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> e -> Bool) -> STBytes# RealWorld e -> ST RealWorld Bool
forall (m :: * -> *) s e.
SortM m s e =>
(e -> e -> Bool) -> s -> m Bool
sortedMBy e -> e -> Bool
f (STBytes# RealWorld e -> ST RealWorld Bool)
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    sortMBy :: Compare e -> MIOBytes# io e -> io ()
sortMBy     = Compare e -> MIOBytes# io e -> io ()
forall (m :: * -> *) v e i.
(LinearM m v e, BorderedM m v i) =>
Compare e -> v -> m ()
timSortBy

--------------------------------------------------------------------------------

instance (MonadIO io, Unboxed e) => Thaw io (SBytes# e) (MIOBytes# io e)
  where
    unsafeThaw :: SBytes# e -> io (MIOBytes# io e)
unsafeThaw = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (SBytes# e -> ST RealWorld (STBytes# RealWorld e))
-> SBytes# e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) v v'. Thaw m v v' => v -> m v'
unsafeThaw
    thaw :: SBytes# e -> io (MIOBytes# io e)
thaw       = ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
forall (io :: * -> *) e.
MonadIO io =>
ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e)
pack (ST RealWorld (STBytes# RealWorld e) -> io (MIOBytes# io e))
-> (SBytes# e -> ST RealWorld (STBytes# RealWorld e))
-> SBytes# e
-> io (MIOBytes# io e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SBytes# e -> ST RealWorld (STBytes# RealWorld e)
forall (m :: * -> *) v v'. Thaw m v v' => v -> m v'
thaw

instance (MonadIO io, Unboxed e) => Freeze io (MIOBytes# io e) (SBytes# e)
  where
    unsafeFreeze :: MIOBytes# io e -> io (SBytes# e)
unsafeFreeze = ST RealWorld (SBytes# e) -> io (SBytes# e)
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld (SBytes# e) -> io (SBytes# e))
-> (MIOBytes# io e -> ST RealWorld (SBytes# e))
-> MIOBytes# io e
-> io (SBytes# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> ST RealWorld (SBytes# e)
forall (m :: * -> *) v' v. Freeze m v' v => v' -> m v
unsafeFreeze (STBytes# RealWorld e -> ST RealWorld (SBytes# e))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld (SBytes# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack
    freeze :: MIOBytes# io e -> io (SBytes# e)
freeze       = ST RealWorld (SBytes# e) -> io (SBytes# e)
forall (io :: * -> *) e. MonadIO io => ST RealWorld e -> io e
stToMIO (ST RealWorld (SBytes# e) -> io (SBytes# e))
-> (MIOBytes# io e -> ST RealWorld (SBytes# e))
-> MIOBytes# io e
-> io (SBytes# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STBytes# RealWorld e -> ST RealWorld (SBytes# e)
forall (m :: * -> *) v' v. Freeze m v' v => v' -> m v
freeze (STBytes# RealWorld e -> ST RealWorld (SBytes# e))
-> (MIOBytes# io e -> STBytes# RealWorld e)
-> MIOBytes# io e
-> ST RealWorld (SBytes# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MIOBytes# io e -> STBytes# RealWorld e
forall (io :: * -> *) e. MIOBytes# io e -> STBytes# RealWorld e
unpack

--------------------------------------------------------------------------------

instance (Storable e, Unboxed e) => Freeze IO (Int, Ptr e) (SBytes# e)
  where
    freeze :: (Int, Ptr e) -> IO (SBytes# e)
freeze (Int
n, Ptr e
ptr) = do
      let !n' :: Int
n'@(I# Int#
n#) = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
n
      MIOBytes# IO e
es' <- ST RealWorld (MIOBytes# IO e) -> IO (MIOBytes# IO e)
forall a. ST RealWorld a -> IO a
stToIO (ST RealWorld (MIOBytes# IO e) -> IO (MIOBytes# IO e))
-> (STRep RealWorld (MIOBytes# IO e)
    -> ST RealWorld (MIOBytes# IO e))
-> STRep RealWorld (MIOBytes# IO e)
-> IO (MIOBytes# IO e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STRep RealWorld (MIOBytes# IO e) -> ST RealWorld (MIOBytes# IO e)
forall s a. STRep s a -> ST s a
ST (STRep RealWorld (MIOBytes# IO e) -> IO (MIOBytes# IO e))
-> STRep RealWorld (MIOBytes# IO e) -> IO (MIOBytes# IO e)
forall a b. (a -> b) -> a -> b
$ \ State# RealWorld
s1# -> case Ptr e
-> Int#
-> State# RealWorld
-> (# State# RealWorld, MutableByteArray# RealWorld #)
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
pnewUnboxed Ptr e
ptr Int#
n# State# RealWorld
s1# of
        (# State# RealWorld
s2#, MutableByteArray# RealWorld
marr# #) -> (# State# RealWorld
s2#, STBytes# RealWorld e -> MIOBytes# IO e
forall (io :: * -> *) e. STBytes# RealWorld e -> MIOBytes# io e
MIOBytes# (Int -> Int -> MutableByteArray# RealWorld -> STBytes# RealWorld e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
n' Int
0 MutableByteArray# RealWorld
marr#) #)
      [Int] -> (Int -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> IO ()) -> IO ()) -> (Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Int
i -> Ptr e -> Int -> IO e
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr e
ptr Int
i IO e -> (e -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MIOBytes# IO e -> Int -> e -> IO ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM MIOBytes# IO e
es' Int
i
      MIOBytes# IO e -> IO (SBytes# e)
forall (m :: * -> *) v' v. Freeze m v' v => v' -> m v
freeze MIOBytes# IO e
es'

instance (Storable e, Unboxed e) => Thaw IO (SBytes# e) (Int, Ptr e)
  where
    thaw :: SBytes# e -> IO (Int, Ptr e)
thaw (SBytes# Int
n Int
o ByteArray#
arr#) = do
      Ptr e
ptr <- Int -> IO (Ptr e)
forall a. Storable a => Int -> IO (Ptr a)
callocArray Int
n
      [Int] -> (Int -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
o .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> IO ()) -> IO ()) -> (Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ i :: Int
i@(I# Int#
i#) -> Ptr e -> Int -> e -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr e
ptr Int
i (ByteArray#
arr# ByteArray# -> Int# -> e
forall e. Unboxed e => ByteArray# -> Int# -> e
!# Int#
i#)
      (Int, Ptr e) -> IO (Int, Ptr e)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n, Ptr e
ptr)

--------------------------------------------------------------------------------

-- | 'unpackSBytes#' returns 'ByteArray#' field of 'SBytes#'.
unpackSBytes# :: (Unboxed e) => SBytes# e -> ByteArray#
unpackSBytes# :: SBytes# e -> ByteArray#
unpackSBytes# = \ (SBytes# Int
_ Int
_ ByteArray#
marr#) -> ByteArray#
marr#

-- | 'offsetSBytes#' returns 'SBytes#' offset in elements.
offsetSBytes# :: (Unboxed e) => SBytes# e -> Int
offsetSBytes# :: SBytes# e -> Int
offsetSBytes# =  \ (SBytes# Int
_ Int
o ByteArray#
_) -> Int
o

-- | 'packSBytes#' creates new 'SBytes#' from sized 'ByteArray#'.
packSBytes# :: (Unboxed e) => Int -> ByteArray# -> SBytes# e
packSBytes# :: Int -> ByteArray# -> SBytes# e
packSBytes# Int
n ByteArray#
marr# = Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
n) Int
0 ByteArray#
marr#

-- | 'fromSBytes#' returns new 'ByteArray#'.
fromSBytes# :: (Unboxed e) => SBytes# e -> ByteArray#
fromSBytes# :: SBytes# e -> ByteArray#
fromSBytes# es :: SBytes# e
es@(SBytes# c :: Int
c@(I# Int#
c#) o :: Int
o@(I# Int#
o#) ByteArray#
src#) =
  let
    !(SBytes# Int
_ Int
_ ByteArray#
res#) = (forall s. ST s (SBytes# Any)) -> SBytes# Any
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# Any)) -> SBytes# Any)
-> (forall s. ST s (SBytes# Any)) -> SBytes# Any
forall a b. (a -> b) -> a -> b
$ STRep s (SBytes# Any) -> ST s (SBytes# Any)
forall s a. STRep s a -> ST s a
ST (STRep s (SBytes# Any) -> ST s (SBytes# Any))
-> STRep s (SBytes# Any) -> ST s (SBytes# Any)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case SBytes# e
-> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
pnewUnboxed SBytes# e
es Int#
c# State# s
s1# of
      (# State# s
s2#, MutableByteArray# s
mcopy# #) -> case SBytes# e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
pcopyUnboxed SBytes# e
es ByteArray#
src# Int#
o# MutableByteArray# s
mcopy# Int#
0# Int#
c# State# s
s2# of
        State# s
s3# -> case MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# s
mcopy# State# s
s3# of
          (# State# s
s4#, ByteArray#
copy# #) -> (# State# s
s4#, Int -> Int -> ByteArray# -> SBytes# Any
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# Int
c Int
o ByteArray#
copy# #)
  in  ByteArray#
res#

{- |
  'unsafeCoerceSBytes#' is unsafe low-lowel coerce of an array with recounting
  the number of elements and offset (with possible rounding).
-}
unsafeCoerceSBytes# :: (Unboxed a, Unboxed b) => SBytes# a -> SBytes# b
unsafeCoerceSBytes# :: SBytes# a -> SBytes# b
unsafeCoerceSBytes# pa :: SBytes# a
pa@(SBytes# Int
n Int
o ByteArray#
arr#) = SBytes# b
pb
  where
    n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
s1 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
s2; s1 :: Int
s1 = SBytes# a -> Int -> Int
forall e (proxy :: * -> *). Unboxed e => proxy e -> Int -> Int
psizeof SBytes# a
pa Int
8
    o' :: Int
o' = Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
s1 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
s2; s2 :: Int
s2 = SBytes# b -> Int -> Int
forall e (proxy :: * -> *). Unboxed e => proxy e -> Int -> Int
psizeof SBytes# b
pb Int
8
    pb :: SBytes# b
pb = Int -> Int -> ByteArray# -> SBytes# b
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# Int
n' Int
o' ByteArray#
arr#

-- | 'unpackSTBytes#' returns 'MutableByteArray#' field of 'STBytes#'.
unpackSTBytes# :: (Unboxed e) => STBytes# s e -> MutableByteArray# s
unpackSTBytes# :: STBytes# s e -> MutableByteArray# s
unpackSTBytes# =  \ (STBytes# Int
_ Int
_ MutableByteArray# s
marr#) -> MutableByteArray# s
marr#

-- | 'offsetSTBytes#' returns 'STBytes#' offset in bytes.
offsetSTBytes# :: (Unboxed e) => STBytes# s e -> Int#
offsetSTBytes# :: STBytes# s e -> Int#
offsetSTBytes# =  \ (STBytes# Int
_ (I# Int#
o#) MutableByteArray# s
_) -> Int#
o#

-- | 'packSTBytes#' creates new 'STBytes#' from sized 'MutableByteArray#'.
packSTBytes# :: (Unboxed e) => Int -> MutableByteArray# s -> STBytes# s e
packSTBytes# :: Int -> MutableByteArray# s -> STBytes# s e
packSTBytes# Int
n MutableByteArray# s
marr# = Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
n) Int
0 MutableByteArray# s
marr#

-- | 'fromSTBytes#' returns new 'MutableByteArray#'.
fromSTBytes# :: (Unboxed e) => STBytes# s e -> State# s -> (# State# s, MutableByteArray# s #)
fromSTBytes# :: STBytes# s e -> State# s -> (# State# s, MutableByteArray# s #)
fromSTBytes# STBytes# s e
es = \ State# s
s1# -> case STBytes# s e -> ST s (STBytes# s e)
forall e s. Unboxed e => STBytes# s e -> ST s (STBytes# s e)
cloneSTBytes# STBytes# s e
es of
  ST STRep s (STBytes# s e)
rep -> case STRep s (STBytes# s e)
rep State# s
s1# of (# State# s
s2#, (STBytes# Int
_ Int
_ MutableByteArray# s
marr#) #) -> (# State# s
s2#, MutableByteArray# s
marr# #)

{- |
  'unsafeCoerceSTBytes#' is unsafe low-lowel coerce of an mutable array with
  recounting the number of elements and offset (with possible rounding).
-}
unsafeCoerceSTBytes# :: (Unboxed a, Unboxed b) => STBytes# s a -> STBytes# s b
unsafeCoerceSTBytes# :: STBytes# s a -> STBytes# s b
unsafeCoerceSTBytes# pa :: STBytes# s a
pa@(STBytes# Int
n Int
o MutableByteArray# s
arr#) = STBytes# s b
pb
  where
    n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
s1 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
s2; s1 :: Int
s1 = STBytes# s a -> Int -> Int
forall e (proxy :: * -> *). Unboxed e => proxy e -> Int -> Int
psizeof STBytes# s a
pa Int
8
    o' :: Int
o' = Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
s1 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
s2; s2 :: Int
s2 = STBytes# s b -> Int -> Int
forall e (proxy :: * -> *). Unboxed e => proxy e -> Int -> Int
psizeof STBytes# s b
pb Int
8
    pb :: STBytes# s b
pb = Int -> Int -> MutableByteArray# s -> STBytes# s b
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
n' Int
o' MutableByteArray# s
arr#

{- |
  @'unsafeSBytesToPtr#' es@ byte-wise stores 'SBytes#' content to 'Ptr'. Returns
  the number of overwritten elements and a pointer to @psizeof es (sizeOf es)@
  bytes of allocated memory.
-}
unsafeSBytesToPtr# :: (Unboxed e) => SBytes# e -> IO (Int, Ptr e)
unsafeSBytesToPtr# :: SBytes# e -> IO (Int, Ptr e)
unsafeSBytesToPtr# es :: SBytes# e
es@(SBytes# Int
c (I# Int#
o#) ByteArray#
marr#) = do
  let
    pokeByte :: Ptr a -> Int -> Word8 -> IO ()
    pokeByte :: Ptr a -> Int -> Word8 -> IO ()
pokeByte =  Ptr a -> Int -> Word8 -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff
    
    n :: Int
n = SBytes# e -> Int -> Int
forall e (proxy :: * -> *). Unboxed e => proxy e -> Int -> Int
psizeof SBytes# e
es Int
c
  
  Ptr e
ptr <- Int -> IO (Ptr e)
forall a. Int -> IO (Ptr a)
mallocBytes Int
n
  [Int] -> (Int -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> IO ()) -> IO ()) -> (Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ i :: Int
i@(I# Int#
i#) -> Ptr e -> Int -> Word8 -> IO ()
forall a. Ptr a -> Int -> Word8 -> IO ()
pokeByte Ptr e
ptr Int
i (ByteArray#
marr# ByteArray# -> Int# -> Word8
forall e. Unboxed e => ByteArray# -> Int# -> e
!# (Int#
o# Int# -> Int# -> Int#
+# Int#
i#))
  (Int, Ptr e) -> IO (Int, Ptr e)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n, Ptr e
ptr)

{- |
  @'unsafePtrToSBytes#' n ptr@ byte-wise stores @n@ elements of 'Ptr' @ptr@ to
  'SBytes#'.
-}
unsafePtrToSBytes# :: (Unboxed e) => (Int, Ptr e) -> IO (SBytes# e)
unsafePtrToSBytes# :: (Int, Ptr e) -> IO (SBytes# e)
unsafePtrToSBytes# (Int
c, Ptr e
ptr) = do
  let
    !n :: Int
n@(I# Int#
n#) = Ptr e -> Int -> Int
forall e (proxy :: * -> *). Unboxed e => proxy e -> Int -> Int
psizeof Ptr e
ptr Int
c'
    c' :: Int
c' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
c
  
  es :: STBytes# RealWorld e
es@(STBytes# Int
_ Int
_ MutableByteArray# RealWorld
arr#) <- ST RealWorld (STBytes# RealWorld e) -> IO (STBytes# RealWorld e)
forall a. ST RealWorld a -> IO a
stToIO (ST RealWorld (STBytes# RealWorld e) -> IO (STBytes# RealWorld e))
-> ST RealWorld (STBytes# RealWorld e) -> IO (STBytes# RealWorld e)
forall a b. (a -> b) -> a -> b
$ STRep RealWorld (STBytes# RealWorld e)
-> ST RealWorld (STBytes# RealWorld e)
forall s a. STRep s a -> ST s a
ST (STRep RealWorld (STBytes# RealWorld e)
 -> ST RealWorld (STBytes# RealWorld e))
-> STRep RealWorld (STBytes# RealWorld e)
-> ST RealWorld (STBytes# RealWorld e)
forall a b. (a -> b) -> a -> b
$ \ State# RealWorld
s1# -> case Ptr e
-> Int#
-> State# RealWorld
-> (# State# RealWorld, MutableByteArray# RealWorld #)
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
pnewUnboxed Ptr e
ptr Int#
n# State# RealWorld
s1# of
    (# State# RealWorld
s2#, MutableByteArray# RealWorld
marr# #) -> (# State# RealWorld
s2#, Int -> Int -> MutableByteArray# RealWorld -> STBytes# RealWorld e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
c' Int
0 MutableByteArray# RealWorld
marr# #)
  
  [Int] -> (Int -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> IO ()) -> IO ()) -> (Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ i :: Int
i@(I# Int#
i#) -> do
    Word8
e <- Ptr e -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr e
ptr Int
i :: IO Word8
    ST RealWorld () -> IO ()
forall a. ST RealWorld a -> IO a
stToIO (ST RealWorld () -> IO ()) -> ST RealWorld () -> IO ()
forall a b. (a -> b) -> a -> b
$ STRep RealWorld () -> ST RealWorld ()
forall s a. STRep s a -> ST s a
ST (STRep RealWorld () -> ST RealWorld ())
-> STRep RealWorld () -> ST RealWorld ()
forall a b. (a -> b) -> a -> b
$ \ State# RealWorld
s1# -> case MutableByteArray# RealWorld
-> Int# -> Word8 -> State# RealWorld -> State# RealWorld
forall e s.
Unboxed e =>
MutableByteArray# s -> Int# -> e -> State# s -> State# s
writeByteArray# MutableByteArray# RealWorld
arr# Int#
i# Word8
e State# RealWorld
s1# of
      State# RealWorld
s2# -> (# State# RealWorld
s2#, () #)
  
  ST RealWorld (SBytes# e) -> IO (SBytes# e)
forall a. ST RealWorld a -> IO a
stToIO (STBytes# RealWorld e -> ST RealWorld (SBytes# e)
forall s e. STBytes# s e -> ST s (SBytes# e)
done STBytes# RealWorld e
es)

-- | Calculate hash 'SBytes#' using 'hashUnboxedWith'.
hashSBytesWith# :: (Unboxed e) => Int -> SBytes# e -> Int
hashSBytesWith# :: Int -> SBytes# e -> Int
hashSBytesWith# (I# Int#
salt#) es :: SBytes# e
es@(SBytes# (I# Int#
c#) (I# Int#
o#) ByteArray#
bytes#) =
  Int# -> Int
I# (e -> Int# -> Int# -> ByteArray# -> Int# -> Int#
forall e.
Unboxed e =>
e -> Int# -> Int# -> ByteArray# -> Int# -> Int#
hashUnboxedWith (SBytes# e -> e
forall (proxy :: * -> *) e. proxy e -> e
fromProxy SBytes# e
es) Int#
c# Int#
o# ByteArray#
bytes# Int#
salt#)

--------------------------------------------------------------------------------

{-# INLINE done #-}
done :: STBytes# s e -> ST s (SBytes# e)
done :: STBytes# s e -> ST s (SBytes# e)
done (STBytes# Int
n Int
o MutableByteArray# s
marr#) = STRep s (SBytes# e) -> ST s (SBytes# e)
forall s a. STRep s a -> ST s a
ST (STRep s (SBytes# e) -> ST s (SBytes# e))
-> STRep s (SBytes# e) -> ST s (SBytes# e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# s
marr# State# s
s1# of
  (# State# s
s2#, ByteArray#
arr# #) -> (# State# s
s2#, Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# Int
n Int
o ByteArray#
arr# #)

{-# INLINE done' #-}
done' :: Int -> MutableByteArray# s -> STRep s (STBytes# s e)
done' :: Int -> MutableByteArray# s -> STRep s (STBytes# s e)
done' Int
n MutableByteArray# s
marr# = \ State# s
s1# -> (# State# s
s1#, Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
n Int
0 MutableByteArray# s
marr# #)

-- | alloc creates filled by default value pseudo-primitive.
alloc :: (Unboxed e) => Int -> ST s (STBytes# s e)
alloc :: Int -> ST s (STBytes# s e)
alloc c :: Int
c@(I# Int#
c#) =
  let res :: ST s (STBytes# s e)
res = STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall s a. STRep s a -> ST s a
ST (STRep s (STBytes# s e) -> ST s (STBytes# s e))
-> STRep s (STBytes# s e) -> ST s (STBytes# s e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case ST s (STBytes# s e)
-> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e (m :: * -> *) (proxy :: * -> *) s.
Unboxed e =>
m (proxy e)
-> Int# -> State# s -> (# State# s, MutableByteArray# s #)
pnewUnboxed1 ST s (STBytes# s e)
res Int#
c# State# s
s1# of
        (# State# s
s2#, MutableByteArray# s
marr# #) -> (# State# s
s2#, Int -> Int -> MutableByteArray# s -> STBytes# s e
forall s e. Int -> Int -> MutableByteArray# s -> STBytes# s e
STBytes# Int
c Int
0 MutableByteArray# s
marr# #)
  in  ST s (STBytes# s e)
res

cloneSTBytes# :: (Unboxed e) => STBytes# s e -> ST s (STBytes# s e)
cloneSTBytes# :: STBytes# s e -> ST s (STBytes# s e)
cloneSTBytes# es :: STBytes# s e
es@(STBytes# c :: Int
c@(I# Int#
c#) (I# Int#
o#) MutableByteArray# s
marr#) = do
  copy :: STBytes# s e
copy@(STBytes# Int
_ Int
_ MutableByteArray# s
copy#) <- Int -> ST s (STBytes# s e)
forall e s. Unboxed e => Int -> ST s (STBytes# s e)
alloc Int
c
  STRep s () -> ST s ()
forall s a. STRep s a -> ST s a
ST (STRep s () -> ST s ()) -> STRep s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case STBytes# s e
-> MutableByteArray# s
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e (proxy :: * -> *) s.
Unboxed e =>
proxy e
-> MutableByteArray# s
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
pcopyUnboxedM STBytes# s e
es MutableByteArray# s
marr# Int#
o# MutableByteArray# s
copy# Int#
0# Int#
c# State# s
s1# of
    State# s
s2# -> (# State# s
s2#, () #)
  STBytes# s e -> ST s (STBytes# s e)
forall (m :: * -> *) a. Monad m => a -> m a
return STBytes# s e
copy

before :: (Unboxed e) => Int -> e -> SBytes# e -> SBytes# e
before :: Int -> e -> SBytes# e -> SBytes# e
before n :: Int
n@(I# Int#
n#) e
e es :: SBytes# e
es@(SBytes# c :: Int
c@(I# Int#
c#) (I# Int#
o#) ByteArray#
arr#)
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c = SBytes# e
es SBytes# e -> e -> SBytes# e
forall l e. Linear l e => l -> e -> l
:< e
e
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = e
e e -> SBytes# e -> SBytes# e
forall l e. Linear l e => e -> l -> l
:> SBytes# e
es
  |  Bool
True  = (forall s. ST s (SBytes# e)) -> SBytes# e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SBytes# e)) -> SBytes# e)
-> (forall s. ST s (SBytes# e)) -> SBytes# e
forall a b. (a -> b) -> a -> b
$ STRep s (SBytes# e) -> ST s (SBytes# e)
forall s a. STRep s a -> ST s a
ST (STRep s (SBytes# e) -> ST s (SBytes# e))
-> STRep s (SBytes# e) -> ST s (SBytes# e)
forall a b. (a -> b) -> a -> b
$ \ State# s
s1# -> case e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
forall e s.
Unboxed e =>
e -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
newUnboxed' e
e (Int#
c# Int# -> Int# -> Int#
+# Int#
1#) State# s
s1# of
    (# State# s
s2#, MutableByteArray# s
marr# #) -> case e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e s.
Unboxed e =>
e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
copyUnboxed# e
e ByteArray#
arr# Int#
o# MutableByteArray# s
marr# Int#
0# Int#
n# State# s
s2# of
      State# s
s3# -> case e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall e s.
Unboxed e =>
e
-> ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
copyUnboxed# e
e ByteArray#
arr# (Int#
o# Int# -> Int# -> Int#
+# Int#
n#) MutableByteArray# s
marr# (Int#
n# Int# -> Int# -> Int#
+# Int#
1#) (Int#
c# Int# -> Int# -> Int#
-# Int#
n#) State# s
s3# of
        State# s
s4# -> case MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# s
marr# State# s
s4# of
          (# State# s
s5#, ByteArray#
res# #) -> (# State# s
s5#, Int -> Int -> ByteArray# -> SBytes# e
forall e. Int -> Int -> ByteArray# -> SBytes# e
SBytes# (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
0 ByteArray#
res# #)

{-# INLINE nubSorted #-}
nubSorted :: (Unboxed e) => Compare e -> SBytes# e -> SBytes# e
nubSorted :: Compare e -> SBytes# e -> SBytes# e
nubSorted Compare e
_ SBytes# e
Z  = SBytes# e
forall e. Nullable e => e
Z
nubSorted Compare e
f SBytes# e
es =
  let fun :: e -> [e] -> [e]
fun = \ e
e [e]
ls -> e
e Compare e
`f` [e] -> e
forall l e. Linear l e => l -> e
head [e]
ls Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ Bool -> [e] -> [e] -> [e]
forall a. Bool -> a -> a -> a
? [e]
ls ([e] -> [e]) -> [e] -> [e]
forall a b. (a -> b) -> a -> b
$ e
e e -> [e] -> [e]
forall a. a -> [a] -> [a]
: [e]
ls
  in  [e] -> SBytes# e
forall l e. Linear l e => [e] -> l
fromList ([e] -> SBytes# e) -> [e] -> SBytes# e
forall a b. (a -> b) -> a -> b
$ (e -> [e] -> [e]) -> [e] -> [e] -> [e]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr e -> [e] -> [e]
fun [SBytes# e -> e
forall l e. Linear l e => l -> e
last SBytes# e
es] ((SBytes# e
es SBytes# e -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^) (Int -> e) -> [Int] -> [e]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
0 .. SBytes# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf SBytes# e
es Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2])

ascsBounds :: (Ord a) => [(a, b)] -> (a, a)
ascsBounds :: [(a, b)] -> (a, a)
ascsBounds =  \ ((a
x, b
_) : [(a, b)]
xs) -> ((a, b) -> (a, a) -> (a, a)) -> (a, a) -> [(a, b)] -> (a, a)
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ (a
e, b
_) (a
mn, a
mx) -> (a -> a -> a
forall a. Ord a => a -> a -> a
min a
mn a
e, a -> a -> a
forall a. Ord a => a -> a -> a
max a
mx a
e)) (a
x, a
x) [(a, b)]
xs

--------------------------------------------------------------------------------

overEx :: String -> a
overEx :: String -> a
overEx =  IndexException -> a
forall a e. Exception e => e -> a
throw (IndexException -> a) -> (String -> IndexException) -> String -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IndexException
IndexOverflow (String -> IndexException) -> ShowS -> String -> IndexException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
"in SDP.Prim.SBytes."

underEx :: String -> a
underEx :: String -> a
underEx =  IndexException -> a
forall a e. Exception e => e -> a
throw (IndexException -> a) -> (String -> IndexException) -> String -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IndexException
IndexUnderflow (String -> IndexException) -> ShowS -> String -> IndexException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
"in SDP.Prim.SBytes."

unreachEx :: String -> a
unreachEx :: String -> a
unreachEx =  UnreachableException -> a
forall a e. Exception e => e -> a
throw (UnreachableException -> a)
-> (String -> UnreachableException) -> String -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> UnreachableException
UnreachableException (String -> UnreachableException)
-> ShowS -> String -> UnreachableException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
"in SDP.Prim.SBytes."