{-# LANGUAGE DeriveFunctor     #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE InstanceSigs      #-}
{-# LANGUAGE MultiWayIf        #-}

module HaskellWorks.Data.Json.Simple.Cursor.SemiIndex
  ( buildSemiIndex
  , SemiIndex(..)
  ) where

import Control.Monad.ST
import Data.Word

import qualified Data.ByteString                                     as BS
import qualified Data.ByteString.Unsafe                              as BSU
import qualified Data.Vector.Storable                                as DVS
import qualified HaskellWorks.Data.Bits.Writer.Storable              as W
import qualified HaskellWorks.Data.Json.Simple.Cursor.Internal.Word8 as W8

{- HLINT ignore "Redundant do"        -}
{- HLINT ignore "Reduce duplication"  -}

data Context = InJson | InString | InEscape deriving (Context -> Context -> Bool
(Context -> Context -> Bool)
-> (Context -> Context -> Bool) -> Eq Context
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Context -> Context -> Bool
$c/= :: Context -> Context -> Bool
== :: Context -> Context -> Bool
$c== :: Context -> Context -> Bool
Eq, Int -> Context -> ShowS
[Context] -> ShowS
Context -> String
(Int -> Context -> ShowS)
-> (Context -> String) -> ([Context] -> ShowS) -> Show Context
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Context] -> ShowS
$cshowList :: [Context] -> ShowS
show :: Context -> String
$cshow :: Context -> String
showsPrec :: Int -> Context -> ShowS
$cshowsPrec :: Int -> Context -> ShowS
Show)

data SemiIndex v = SemiIndex
  { SemiIndex v -> Context
semiIndexContext :: !Context
  , SemiIndex v -> v
semiIndexIb      :: !v
  , SemiIndex v -> v
semiIndexBp      :: !v
  } deriving (a -> SemiIndex b -> SemiIndex a
(a -> b) -> SemiIndex a -> SemiIndex b
(forall a b. (a -> b) -> SemiIndex a -> SemiIndex b)
-> (forall a b. a -> SemiIndex b -> SemiIndex a)
-> Functor SemiIndex
forall a b. a -> SemiIndex b -> SemiIndex a
forall a b. (a -> b) -> SemiIndex a -> SemiIndex b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> SemiIndex b -> SemiIndex a
$c<$ :: forall a b. a -> SemiIndex b -> SemiIndex a
fmap :: (a -> b) -> SemiIndex a -> SemiIndex b
$cfmap :: forall a b. (a -> b) -> SemiIndex a -> SemiIndex b
Functor, Functor SemiIndex
Foldable SemiIndex
Functor SemiIndex
-> Foldable SemiIndex
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> SemiIndex a -> f (SemiIndex b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    SemiIndex (f a) -> f (SemiIndex a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> SemiIndex a -> m (SemiIndex b))
-> (forall (m :: * -> *) a.
    Monad m =>
    SemiIndex (m a) -> m (SemiIndex a))
-> Traversable SemiIndex
(a -> f b) -> SemiIndex a -> f (SemiIndex b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
SemiIndex (m a) -> m (SemiIndex a)
forall (f :: * -> *) a.
Applicative f =>
SemiIndex (f a) -> f (SemiIndex a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SemiIndex a -> m (SemiIndex b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SemiIndex a -> f (SemiIndex b)
sequence :: SemiIndex (m a) -> m (SemiIndex a)
$csequence :: forall (m :: * -> *) a.
Monad m =>
SemiIndex (m a) -> m (SemiIndex a)
mapM :: (a -> m b) -> SemiIndex a -> m (SemiIndex b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SemiIndex a -> m (SemiIndex b)
sequenceA :: SemiIndex (f a) -> f (SemiIndex a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
SemiIndex (f a) -> f (SemiIndex a)
traverse :: (a -> f b) -> SemiIndex a -> f (SemiIndex b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SemiIndex a -> f (SemiIndex b)
$cp2Traversable :: Foldable SemiIndex
$cp1Traversable :: Functor SemiIndex
Traversable, SemiIndex a -> Bool
(a -> m) -> SemiIndex a -> m
(a -> b -> b) -> b -> SemiIndex a -> b
(forall m. Monoid m => SemiIndex m -> m)
-> (forall m a. Monoid m => (a -> m) -> SemiIndex a -> m)
-> (forall m a. Monoid m => (a -> m) -> SemiIndex a -> m)
-> (forall a b. (a -> b -> b) -> b -> SemiIndex a -> b)
-> (forall a b. (a -> b -> b) -> b -> SemiIndex a -> b)
-> (forall b a. (b -> a -> b) -> b -> SemiIndex a -> b)
-> (forall b a. (b -> a -> b) -> b -> SemiIndex a -> b)
-> (forall a. (a -> a -> a) -> SemiIndex a -> a)
-> (forall a. (a -> a -> a) -> SemiIndex a -> a)
-> (forall a. SemiIndex a -> [a])
-> (forall a. SemiIndex a -> Bool)
-> (forall a. SemiIndex a -> Int)
-> (forall a. Eq a => a -> SemiIndex a -> Bool)
-> (forall a. Ord a => SemiIndex a -> a)
-> (forall a. Ord a => SemiIndex a -> a)
-> (forall a. Num a => SemiIndex a -> a)
-> (forall a. Num a => SemiIndex a -> a)
-> Foldable SemiIndex
forall a. Eq a => a -> SemiIndex a -> Bool
forall a. Num a => SemiIndex a -> a
forall a. Ord a => SemiIndex a -> a
forall m. Monoid m => SemiIndex m -> m
forall a. SemiIndex a -> Bool
forall a. SemiIndex a -> Int
forall a. SemiIndex a -> [a]
forall a. (a -> a -> a) -> SemiIndex a -> a
forall m a. Monoid m => (a -> m) -> SemiIndex a -> m
forall b a. (b -> a -> b) -> b -> SemiIndex a -> b
forall a b. (a -> b -> b) -> b -> SemiIndex a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: SemiIndex a -> a
$cproduct :: forall a. Num a => SemiIndex a -> a
sum :: SemiIndex a -> a
$csum :: forall a. Num a => SemiIndex a -> a
minimum :: SemiIndex a -> a
$cminimum :: forall a. Ord a => SemiIndex a -> a
maximum :: SemiIndex a -> a
$cmaximum :: forall a. Ord a => SemiIndex a -> a
elem :: a -> SemiIndex a -> Bool
$celem :: forall a. Eq a => a -> SemiIndex a -> Bool
length :: SemiIndex a -> Int
$clength :: forall a. SemiIndex a -> Int
null :: SemiIndex a -> Bool
$cnull :: forall a. SemiIndex a -> Bool
toList :: SemiIndex a -> [a]
$ctoList :: forall a. SemiIndex a -> [a]
foldl1 :: (a -> a -> a) -> SemiIndex a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SemiIndex a -> a
foldr1 :: (a -> a -> a) -> SemiIndex a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> SemiIndex a -> a
foldl' :: (b -> a -> b) -> b -> SemiIndex a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> SemiIndex a -> b
foldl :: (b -> a -> b) -> b -> SemiIndex a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> SemiIndex a -> b
foldr' :: (a -> b -> b) -> b -> SemiIndex a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> SemiIndex a -> b
foldr :: (a -> b -> b) -> b -> SemiIndex a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> SemiIndex a -> b
foldMap' :: (a -> m) -> SemiIndex a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> SemiIndex a -> m
foldMap :: (a -> m) -> SemiIndex a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> SemiIndex a -> m
fold :: SemiIndex m -> m
$cfold :: forall m. Monoid m => SemiIndex m -> m
Foldable)

buildSemiIndex :: BS.ByteString -> SemiIndex (DVS.Vector Word64)
buildSemiIndex :: ByteString -> SemiIndex (Vector Word64)
buildSemiIndex ByteString
bs = (forall s. ST s (SemiIndex (MVector s Word64)))
-> SemiIndex (Vector Word64)
forall (f :: * -> *) a.
(Traversable f, Storable a) =>
(forall s. ST s (f (MVector s a))) -> f (Vector a)
DVS.createT ((forall s. ST s (SemiIndex (MVector s Word64)))
 -> SemiIndex (Vector Word64))
-> (forall s. ST s (SemiIndex (MVector s Word64)))
-> SemiIndex (Vector Word64)
forall a b. (a -> b) -> a -> b
$ do
  let len :: Int
len = (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8
  Writer s
mib <- Int -> ST s (Writer s)
forall s. Int -> ST s (Writer s)
W.newWriter Int
len
  Writer s
mbp <- Int -> ST s (Writer s)
forall s. Int -> ST s (Writer s)
W.newWriter (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2)
  Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
forall s.
Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
buildFromByteString Writer s
mib Writer s
mbp ByteString
bs Int
0 Context
InJson
{-# INLINE buildSemiIndex #-}

buildFromByteString :: W.Writer s -> W.Writer s -> BS.ByteString -> Int -> Context -> ST s (SemiIndex (DVS.MVector s Word64))
buildFromByteString :: Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
buildFromByteString Writer s
ib Writer s
bp ByteString
bs Int
i Context
context = if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< ByteString -> Int
BS.length ByteString
bs
  then do
    let c :: Word8
c = ByteString -> Int -> Word8
BSU.unsafeIndex ByteString
bs Int
i
    case Context
context of
      Context
InJson -> if
        | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
W8.openBracket Bool -> Bool -> Bool
|| Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
W8.openBrace -> do
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
bp Word64
1
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
bp Word64
1
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
ib Word64
1
          Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
forall s.
Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
buildFromByteString Writer s
ib Writer s
bp ByteString
bs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Context
InJson
        | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
W8.closeBracket Bool -> Bool -> Bool
|| Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
W8.closeBrace -> do
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
bp Word64
0
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
bp Word64
0
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
ib Word64
1
          Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
forall s.
Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
buildFromByteString Writer s
ib Writer s
bp ByteString
bs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Context
InJson
        | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
W8.comma Bool -> Bool -> Bool
|| Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
W8.colon -> do
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
bp Word64
0
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
bp Word64
1
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
ib Word64
1
          Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
forall s.
Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
buildFromByteString Writer s
ib Writer s
bp ByteString
bs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Context
InJson
        | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
W8.doubleQuote -> do
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
ib Word64
0
          Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
forall s.
Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
buildFromByteString Writer s
ib Writer s
bp ByteString
bs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Context
InString
        | Bool
otherwise -> do
          Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
ib Word64
0
          Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
forall s.
Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
buildFromByteString Writer s
ib Writer s
bp ByteString
bs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Context
InJson
      Context
InString -> do
        Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
ib Word64
0
        let newContext :: Context
newContext = if
              | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
W8.doubleQuote  -> Context
InJson
              | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
W8.backSlash    -> Context
InEscape
              | Bool
otherwise           -> Context
InString
        Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
forall s.
Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
buildFromByteString Writer s
ib Writer s
bp ByteString
bs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Context
newContext
      Context
InEscape -> do
        Writer s -> Word64 -> ST s ()
forall s. Writer s -> Word64 -> ST s ()
W.unsafeWriteBit Writer s
ib Word64
0
        Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
forall s.
Writer s
-> Writer s
-> ByteString
-> Int
-> Context
-> ST s (SemiIndex (MVector s Word64))
buildFromByteString Writer s
ib Writer s
bp ByteString
bs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Context
InString
  else do
    MVector s Word64
ibv <- Writer s -> ST s (MVector s Word64)
forall s. Writer s -> ST s (MVector s Word64)
W.written Writer s
ib
    MVector s Word64
bpv <- Writer s -> ST s (MVector s Word64)
forall s. Writer s -> ST s (MVector s Word64)
W.written Writer s
bp
    SemiIndex (MVector s Word64) -> ST s (SemiIndex (MVector s Word64))
forall (m :: * -> *) a. Monad m => a -> m a
return (Context
-> MVector s Word64
-> MVector s Word64
-> SemiIndex (MVector s Word64)
forall v. Context -> v -> v -> SemiIndex v
SemiIndex Context
context MVector s Word64
ibv MVector s Word64
bpv)
{-# INLINE buildFromByteString #-}