module HaskellWorks.Data.BalancedParens.Internal.Broadword.FindUnmatchedCloseFar.Vector16
( findUnmatchedCloseFar
) where
import Data.Int
import Data.Word
import HaskellWorks.Data.AtIndex
import HaskellWorks.Data.Bits.BitLength
import HaskellWorks.Data.Int.Unsigned
import HaskellWorks.Data.Positioning
import qualified Data.Vector.Storable as DVS
import qualified HaskellWorks.Data.BalancedParens.Internal.Broadword.FindUnmatchedCloseFar.Word16 as BWW16
import qualified HaskellWorks.Data.Drop as HW
import qualified HaskellWorks.Data.Length as HW
findUnmatchedCloseCont :: Int64 -> Count -> DVS.Vector Word16 -> Count
findUnmatchedCloseCont :: Int64 -> Count -> Vector Word16 -> Count
findUnmatchedCloseCont Int64
i Count
c Vector Word16
v = if Int64
i Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< Vector Word16 -> Int64
forall v. Length v => v -> Int64
HW.end Vector Word16
v
then case Count -> Count -> Word16 -> Count
BWW16.findUnmatchedCloseFar Count
c Count
0 Word16
w of
Count
q -> if Count
q Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
>= Word16 -> Count
forall v. BitLength v => v -> Count
bitLength Word16
w
then Int64 -> Count -> Vector Word16 -> Count
findUnmatchedCloseCont (Int64
i Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
1) (Count
q Count -> Count -> Count
forall a. Num a => a -> a -> a
- Word16 -> Count
forall v. BitLength v => v -> Count
bitLength Word16
w) Vector Word16
v
else Count
b Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
q
else Count
b Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
c
where b :: Count
b = Int64 -> UnsignedOf Int64
forall a. Unsigned a => a -> UnsignedOf a
unsigned Int64
i Count -> Count -> Count
forall a. Num a => a -> a -> a
* Word16 -> Count
forall v. BitLength v => v -> Count
bitLength Word16
w
w :: Elem (Vector Word16)
w = Vector Word16
v Vector Word16 -> Int64 -> Elem (Vector Word16)
forall v. AtIndex v => v -> Int64 -> Elem v
!!! Int64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
i
{-# INLINE findUnmatchedCloseCont #-}
findUnmatchedClose' :: Word64 -> Word64 -> DVS.Vector Word16 -> Count
findUnmatchedClose' :: Count -> Count -> Vector Word16 -> Count
findUnmatchedClose' Count
c Count
p Vector Word16
v = if Vector Word16 -> Int
forall a. Storable a => Vector a -> Int
DVS.length Vector Word16
v Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
then case Count -> Count -> Word16 -> Count
BWW16.findUnmatchedCloseFar Count
c Count
p Word16
w of
Count
q -> if Count
q Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
>= Word16 -> Count
forall v. BitLength v => v -> Count
bitLength Word16
w
then Int64 -> Count -> Vector Word16 -> Count
findUnmatchedCloseCont Int64
1 (Count
q Count -> Count -> Count
forall a. Num a => a -> a -> a
- Word16 -> Count
forall v. BitLength v => v -> Count
bitLength Word16
w) Vector Word16
v
else Count
q
else Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
* Count
2 Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
c
where w :: Elem (Vector Word16)
w = Vector Word16
v Vector Word16 -> Int64 -> Elem (Vector Word16)
forall v. AtIndex v => v -> Int64 -> Elem v
!!! Int64
0
{-# INLINE findUnmatchedClose' #-}
findUnmatchedCloseFar :: Word64 -> Word64 -> DVS.Vector Word16 -> Count
findUnmatchedCloseFar :: Count -> Count -> Vector Word16 -> Count
findUnmatchedCloseFar Count
c Count
p Vector Word16
v = Count -> Count -> Vector Word16 -> Count
findUnmatchedClose' Count
c (Count
p Count -> Count -> Count
forall a. Num a => a -> a -> a
- Count
vd) (Count -> Vector Word16 -> Vector Word16
forall v. Drop v => Count -> v -> v
HW.drop Count
vi Vector Word16
v) Count -> Count -> Count
forall a. Num a => a -> a -> a
+ Count
vd
where vi :: Count
vi = Count
p Count -> Count -> Count
forall a. Integral a => a -> a -> a
`div` Vector Word16 -> Count
forall v. (AtIndex v, BitLength (Elem v)) => v -> Count
elemBitLength Vector Word16
v
vd :: Count
vd = Count
vi Count -> Count -> Count
forall a. Num a => a -> a -> a
* Vector Word16 -> Count
forall v. (AtIndex v, BitLength (Elem v)) => v -> Count
elemBitLength Vector Word16
v
{-# INLINE findUnmatchedCloseFar #-}