{-# LANGUAGE FlexibleInstances #-}

module HaskellWorks.Data.BalancedParens.NewOpenAt
  ( NewOpenAt(..)
  ) where

import Data.Word
import HaskellWorks.Data.Bits.BitLength
import HaskellWorks.Data.Bits.BitShown
import HaskellWorks.Data.Bits.BitWise
import HaskellWorks.Data.Positioning

import qualified Data.Vector.Storable as DVS

class NewOpenAt v where
  newOpenAt :: v -> Count -> Bool

newOpenAt' :: (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt' :: a -> Count -> Bool
newOpenAt' a
v Count
c = (Count
0 Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
<= Count
c Bool -> Bool -> Bool
&& Count
c Count -> Count -> Bool
forall a. Ord a => a -> a -> Bool
< a -> Count
forall v. BitLength v => v -> Count
bitLength a
v) Bool -> Bool -> Bool
&& (a
v a -> Position -> Bool
forall a. TestBit a => a -> Position -> Bool
.?. Count -> Position
forall a. ToPosition a => a -> Position
toPosition Count
c)
{-# INLINE newOpenAt' #-}

instance (BitLength a, TestBit a) => NewOpenAt (BitShown a) where
  newOpenAt :: BitShown a -> Count -> Bool
newOpenAt = a -> Count -> Bool
forall a. (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt' (a -> Count -> Bool)
-> (BitShown a -> a) -> BitShown a -> Count -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BitShown a -> a
forall a. BitShown a -> a
bitShown
  {-# INLINE newOpenAt #-}

instance NewOpenAt [Bool] where
  newOpenAt :: [Bool] -> Count -> Bool
newOpenAt = [Bool] -> Count -> Bool
forall a. (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt'
  {-# INLINE newOpenAt #-}

instance NewOpenAt (DVS.Vector Word8) where
  newOpenAt :: Vector Word8 -> Count -> Bool
newOpenAt = Vector Word8 -> Count -> Bool
forall a. (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt'
  {-# INLINE newOpenAt #-}

instance NewOpenAt (DVS.Vector Word16) where
  newOpenAt :: Vector Word16 -> Count -> Bool
newOpenAt = Vector Word16 -> Count -> Bool
forall a. (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt'
  {-# INLINE newOpenAt #-}

instance NewOpenAt (DVS.Vector Word32) where
  newOpenAt :: Vector Word32 -> Count -> Bool
newOpenAt = Vector Word32 -> Count -> Bool
forall a. (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt'
  {-# INLINE newOpenAt #-}

instance NewOpenAt (DVS.Vector Word64) where
  newOpenAt :: Vector Count -> Count -> Bool
newOpenAt = Vector Count -> Count -> Bool
forall a. (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt'
  {-# INLINE newOpenAt #-}

instance NewOpenAt Word8 where
  newOpenAt :: Word8 -> Count -> Bool
newOpenAt = Word8 -> Count -> Bool
forall a. (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt'
  {-# INLINE newOpenAt #-}

instance NewOpenAt Word16 where
  newOpenAt :: Word16 -> Count -> Bool
newOpenAt = Word16 -> Count -> Bool
forall a. (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt'
  {-# INLINE newOpenAt #-}

instance NewOpenAt Word32 where
  newOpenAt :: Word32 -> Count -> Bool
newOpenAt = Word32 -> Count -> Bool
forall a. (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt'
  {-# INLINE newOpenAt #-}

instance NewOpenAt Word64 where
  newOpenAt :: Count -> Count -> Bool
newOpenAt = Count -> Count -> Bool
forall a. (BitLength a, TestBit a) => a -> Count -> Bool
newOpenAt'
  {-# INLINE newOpenAt #-}