{-# LANGUAGE CPP #-}

#if __GLASGOW_HASKELL__ >= 902
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
#endif

-- | This is the code for the main engine.  This captures the posix
-- subexpressions.  There is also a non-capturing engine, and a
-- testing engine.
--
-- It is polymorphic over the internal Uncons type class, and
-- specialized to produce the needed variants.

module Text.Regex.TDFA.NewDFA.Engine_FA(execMatch) where

import Data.Array.Base(unsafeRead,unsafeWrite,STUArray(..))

import GHC.Arr(STArray(..))
import GHC.ST(ST(..))
import GHC.Exts(MutableByteArray#,RealWorld,Int#,getSizeofMutableByteArray#,unsafeCoerce#,State#)

import Prelude hiding ((!!))
import Control.Monad(when,unless,forM,forM_,liftM2,foldM)
import Data.Array.MArray(MArray(..))
import Data.Array.Unsafe(unsafeFreeze)
import Data.Array.IArray(Array,bounds,assocs,Ix(range))
import qualified Data.IntMap.CharMap2 as CMap(findWithDefault)
import Data.IntMap(IntMap)
import qualified Data.IntMap as IMap(null,toList,lookup,insert)
import Data.Maybe(catMaybes)
import Data.Monoid as Mon(Monoid(..))
import Data.IntSet(IntSet)
import qualified Data.IntSet as ISet(toAscList,null)
import Data.Array.IArray((!))
import Data.List(sortBy,groupBy)
import Data.STRef(STRef,newSTRef,readSTRef,writeSTRef)
import qualified Control.Monad.ST.Strict as S(ST,runST)
import Data.Sequence(Seq,ViewL(..),viewl)
import qualified Data.Sequence as Seq(null)
import qualified Data.ByteString.Char8 as SBS(ByteString)
import qualified Data.ByteString.Lazy.Char8 as LBS(ByteString)
import Foreign.Ptr(Ptr)

import Text.Regex.Base(MatchArray,MatchOffset,MatchLength)
import Text.Regex.TDFA.Common hiding (indent)
import Text.Regex.TDFA.NewDFA.Uncons(Uncons(uncons))
import Text.Regex.TDFA.NewDFA.MakeTest(test_singleline,test_multiline)

--import Debug.Trace

-- trace :: String -> a -> a
-- trace _ a = a

err :: String -> a
err :: forall a. String -> a
err String
s = String -> String -> a
forall a. String -> String -> a
common_error String
"Text.Regex.TDFA.NewDFA.Engine_FA"  String
s

{-# INLINE (!!) #-}
(!!) :: (MArray a e (S.ST s),Ix i) => a i e -> Int -> S.ST s e
!! :: forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
(!!) = a i e -> Int -> ST s e
forall i. Ix i => a i e -> Int -> ST s e
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Int -> m e
unsafeRead
{-# INLINE set #-}
set :: (MArray a e (S.ST s),Ix i) => a i e -> Int -> e -> S.ST s ()
set :: forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> e -> ST s ()
set = a i e -> Int -> e -> ST s ()
forall i. Ix i => a i e -> Int -> e -> ST s ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Int -> e -> m ()
unsafeWrite

noSource :: ((Index, Instructions),STUArray s Tag Position,OrbitLog)
noSource :: forall s. ((Int, Instructions), STUArray s Int Int, OrbitLog)
noSource = ((-Int
1,String -> Instructions
forall a. String -> a
err String
"noSource"),String -> STUArray s Int Int
forall a. String -> a
err String
"noSource",String -> OrbitLog
forall a. String -> a
err String
"noSource")

{-# SPECIALIZE execMatch :: Regex -> Position -> Char -> ([] Char) -> [MatchArray] #-}
{-# SPECIALIZE execMatch :: Regex -> Position -> Char -> (Seq Char) -> [MatchArray] #-}
{-# SPECIALIZE execMatch :: Regex -> Position -> Char -> SBS.ByteString -> [MatchArray] #-}
{-# SPECIALIZE execMatch :: Regex -> Position -> Char -> LBS.ByteString -> [MatchArray] #-}
execMatch :: forall text. Uncons text => Regex -> Position -> Char -> text -> [MatchArray]
execMatch :: forall text.
Uncons text =>
Regex -> Int -> Char -> text -> [MatchArray]
execMatch (Regex { regex_dfa :: Regex -> DFA
regex_dfa =  DFA {d_id :: DFA -> SetIndex
d_id=SetIndex
didIn,d_dt :: DFA -> DT
d_dt=DT
dtIn}
                 , regex_init :: Regex -> Int
regex_init = Int
startState
                 , regex_b_index :: Regex -> (Int, Int)
regex_b_index = (Int, Int)
b_index
                 , regex_b_tags :: Regex -> (Int, Int)
regex_b_tags = (Int, Int)
b_tags_all
                 , regex_tags :: Regex -> Array Int OP
regex_tags = Array Int OP
aTags
                 , regex_groups :: Regex -> Array Int [GroupInfo]
regex_groups = Array Int [GroupInfo]
aGroups
                 , regex_compOptions :: Regex -> CompOption
regex_compOptions = CompOption { multiline :: CompOption -> Bool
multiline = Bool
newline } } )
          Int
offsetIn Char
prevIn text
inputIn = (forall s. ST s [MatchArray]) -> [MatchArray]
forall a. (forall s. ST s a) -> a
S.runST ST s [MatchArray]
forall s. ST s [MatchArray]
goNext where

  b_tags :: (Tag,Tag)
  !b_tags :: (Int, Int)
b_tags = (Int, Int)
b_tags_all

  orbitTags :: [Tag]
  !orbitTags :: [Int]
orbitTags = ((Int, OP) -> Int) -> [(Int, OP)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int, OP) -> Int
forall a b. (a, b) -> a
fst ([(Int, OP)] -> [Int])
-> (Array Int OP -> [(Int, OP)]) -> Array Int OP -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, OP) -> Bool) -> [(Int, OP)] -> [(Int, OP)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((OP
OrbitOP -> OP -> Bool
forall a. Eq a => a -> a -> Bool
==)(OP -> Bool) -> ((Int, OP) -> OP) -> (Int, OP) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Int, OP) -> OP
forall a b. (a, b) -> b
snd) ([(Int, OP)] -> [(Int, OP)])
-> (Array Int OP -> [(Int, OP)]) -> Array Int OP -> [(Int, OP)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array Int OP -> [(Int, OP)]
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> [(i, e)]
assocs (Array Int OP -> [Int]) -> Array Int OP -> [Int]
forall a b. (a -> b) -> a -> b
$ Array Int OP
aTags

  test :: WhichTest -> Index -> Char -> text -> Bool
  !test :: WhichTest -> Int -> Char -> text -> Bool
test = Bool -> WhichTest -> Int -> Char -> text -> Bool
forall text.
Uncons text =>
Bool -> WhichTest -> Int -> Char -> text -> Bool
mkTest Bool
newline

  comp :: C s
  comp :: forall s. C s
comp = {-# SCC "matchHere.comp" #-} Array Int OP -> C s
forall s. Array Int OP -> C s
ditzyComp'3 Array Int OP
aTags

  goNext :: forall s. ST s [MatchArray]
  goNext :: forall s. ST s [MatchArray]
goNext = {-# SCC "goNext" #-} do
    (SScratch s1In s2In (winQ,blank,which)) <- (Int, Int) -> (Int, Int) -> ST s (SScratch s)
forall s. (Int, Int) -> (Int, Int) -> ST s (SScratch s)
newScratch (Int, Int)
b_index (Int, Int)
b_tags
    spawnAt b_tags blank startState s1In offsetIn
    let next MScratch s
s1 MScratch s
s2 SetIndex
did DT
dt Int
offset Char
prev text
input = {-# SCC "goNext.next" #-}
          case DT
dt of
            Testing' {dt_test :: DT -> WhichTest
dt_test=WhichTest
wt,dt_a :: DT -> DT
dt_a=DT
a,dt_b :: DT -> DT
dt_b=DT
b} ->
              if WhichTest -> Int -> Char -> text -> Bool
test WhichTest
wt Int
offset Char
prev text
input
                then MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Int
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s1 MScratch s
s2 SetIndex
did DT
a Int
offset Char
prev text
input
                else MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Int
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s1 MScratch s
s2 SetIndex
did DT
b Int
offset Char
prev text
input
            Simple' {dt_win :: DT -> IntMap Instructions
dt_win=IntMap Instructions
w,dt_trans :: DT -> CharMap Transition
dt_trans=CharMap Transition
t,dt_other :: DT -> Transition
dt_other=Transition
o} -> do
              Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (IntMap Instructions -> Bool
forall a. IntMap a -> Bool
IMap.null IntMap Instructions
w) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
                MScratch s -> IntMap Instructions -> Int -> ST s ()
processWinner MScratch s
s1 IntMap Instructions
w Int
offset
              case text -> Maybe (Char, text)
forall a. Uncons a => a -> Maybe (Char, a)
uncons text
input of
                Maybe (Char, text)
Nothing -> ST s [MatchArray]
finalizeWinner
                Just (Char
c,text
input') ->
                  case Transition -> Char -> CharMap Transition -> Transition
forall a. a -> Char -> CharMap a -> a
CMap.findWithDefault Transition
o Char
c CharMap Transition
t of
                    Transition {trans_single :: Transition -> DFA
trans_single=DFA {d_id :: DFA -> SetIndex
d_id=SetIndex
did',d_dt :: DFA -> DT
d_dt=DT
dt'},trans_how :: Transition -> DTrans
trans_how=DTrans
dtrans}
                      | SetIndex -> Bool
ISet.null SetIndex
did' -> ST s [MatchArray]
finalizeWinner
                      | Bool
otherwise -> MScratch s
-> MScratch s
-> SetIndex
-> SetIndex
-> DT
-> DTrans
-> Int
-> Char
-> text
-> ST s [MatchArray]
findTrans MScratch s
s1 MScratch s
s2 SetIndex
did SetIndex
did' DT
dt' DTrans
dtrans Int
offset Char
c text
input'

-- compressOrbits gets all the current Tag-0 start information from
-- the NFA states; then it loops through all the Orbit tags with
-- compressOrbit.
--
-- compressOrbit on such a Tag loops through all the NFS states'
-- m_orbit record, discarding ones that are Nothing and discarding
-- ones that are too new to care about (after the cutoff value).
--
-- compressOrbit then groups the Orbits records by the Tag-0 start
-- position and the basePos position.  Entries in different groups
-- will never be comparable in the future so they can be processed
-- separately.  Groups could probably be even more finely
-- distinguished, as a further optimization, but the justification will
-- be tricky.
--
-- Current Tag-0 values are at most offset and all newly spawned
-- groups will have Tag-0 of at least (succ offset) so the current
-- groups are closed to those spawned in the future.  The basePos may
-- be as large as offset and may be overwritten later with values of
-- offset or larger (and this will also involve deleting the Orbits
-- record).  Thus there could be a future collision between a current
-- group with basePos==offset and an updated record that acquires
-- basePos==offset.  By excluding groups with basePos before the
-- current offset the collision between existing and future records
-- is avoided.
--
-- An entry in a group can only collide with that group's
-- descendants. compressOrbit sends each group to the compressGroup
-- command.
--
-- compressGroup on a single record checks whether it's Seq can be
-- cleared and if so it will clear it (and set ordinal to Nothing but
-- this this not particularly important).
--
-- compressGroup on many records sorts and groups the members and zips
-- the groups with their new ordinal value.  The comparison is based
-- on the old ordinal value, then the inOrbit value, and then the (Seq
-- Position) data.
--
-- The old ordinals of the group will all be Nothing or all be Just,
-- but this condition is neither checked nor violations detected.
-- This comparison is justified because once records get different
-- ordinals assigned they will never change places.
--
-- The inOrbit Bool is only different if one of them has set the stop
-- position to at most (succ offset).  They will obly be compared if
-- the other one leaves, an its stop position will be at least offset.
-- The previous sentence is justified by inspection of the "assemble"
-- function in the TDFA module: there is no (PostUpdate
-- LeaveOrbitTask) so the largest possible value for the stop Tag is
-- (pred offset). Thus the record with inOrbit==False would beat (be
-- GT than) the record with inOrbit==True.
--
-- The Seq comparison is safe because the largest existing Position
-- value is (pred offset) and the smallest future Position value is
-- offset.  The previous sentence is justified by inspection of the
-- "assemble" function in the TDFA module: there is no (PostUpdate
-- EnterOrbitTags) so the largest possible value in the Seq is (pred
-- offset).
--
-- The updated Orbits get the new ordinal value and an empty (Seq
-- Position).

        compressOrbits :: MScratch s -> IntSet -> Position -> ST s ()
        compressOrbits MScratch s
s1 SetIndex
did Int
offset = do
          let getStart :: Int -> ST s (Int, Int)
getStart Int
state = do start <- ST s Int
-> (STUArray s Int Int -> ST s Int)
-> Maybe (STUArray s Int Int)
-> ST s Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> ST s Int
forall a. String -> a
err String
"compressOrbit,1") (STUArray s Int Int -> Int -> ST s Int
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
0) (Maybe (STUArray s Int Int) -> ST s Int)
-> ST s (Maybe (STUArray s Int Int)) -> ST s Int
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MScratch s -> STArray s Int (Maybe (STUArray s Int Int))
forall s. MScratch s -> STArray s Int (Maybe (STUArray s Int Int))
m_pos MScratch s
s1 STArray s Int (Maybe (STUArray s Int Int))
-> Int -> ST s (Maybe (STUArray s Int Int))
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
state
                                  return (state,start)
              cutoff :: Int
cutoff = Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
50 -- Require: cutoff <= offset, MAGIC TUNABLE CONSTANT 50
          ss <- (Int -> ST s (Int, Int)) -> [Int] -> ST s [(Int, Int)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Int -> ST s (Int, Int)
forall {s}.
(MArray (STUArray s) Int (ST s),
 MArray (STArray s) (Maybe (STUArray s Int Int)) (ST s)) =>
Int -> ST s (Int, Int)
getStart (SetIndex -> [Int]
ISet.toAscList SetIndex
did)
          let compressOrbit Int
tag = do
                mos <- [(Int, Int)]
-> ((Int, Int) -> ST s (Maybe ((Int, Int), Orbits)))
-> ST s [Maybe ((Int, Int), Orbits)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(Int, Int)]
ss ( \ p :: (Int, Int)
p@(Int
state,Int
_start) -> do
                                  mo <- (OrbitLog -> Maybe Orbits) -> ST s OrbitLog -> ST s (Maybe Orbits)
forall a b. (a -> b) -> ST s a -> ST s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> OrbitLog -> Maybe Orbits
forall a. Int -> IntMap a -> Maybe a
IMap.lookup Int
tag) (MScratch s -> STArray s Int OrbitLog
forall s. MScratch s -> STArray s Int OrbitLog
m_orbit MScratch s
s1 STArray s Int OrbitLog -> Int -> ST s OrbitLog
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
state)
                                  case mo of
                                    Just Orbits
orbits | Orbits -> Int
basePos Orbits
orbits Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
cutoff -> Maybe ((Int, Int), Orbits) -> ST s (Maybe ((Int, Int), Orbits))
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (((Int, Int), Orbits) -> Maybe ((Int, Int), Orbits)
forall a. a -> Maybe a
Just ((Int, Int)
p,Orbits
orbits))
                                                | Bool
otherwise -> Maybe ((Int, Int), Orbits) -> ST s (Maybe ((Int, Int), Orbits))
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ((Int, Int), Orbits)
forall a. Maybe a
Nothing
                                    Maybe Orbits
_ -> Maybe ((Int, Int), Orbits) -> ST s (Maybe ((Int, Int), Orbits))
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ((Int, Int), Orbits)
forall a. Maybe a
Nothing )
                let compressGroup [((Int
state,b
_),Orbits
orbit)] | Seq Int -> Bool
forall a. Seq a -> Bool
Seq.null (Orbits -> Seq Int
getOrbits Orbits
orbit) = () -> ST s ()
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                                                      | Bool
otherwise =
                      STArray s Int OrbitLog -> Int -> OrbitLog -> ST s ()
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> e -> ST s ()
set (MScratch s -> STArray s Int OrbitLog
forall s. MScratch s -> STArray s Int OrbitLog
m_orbit MScratch s
s1) Int
state
                      (OrbitLog -> ST s ())
-> (OrbitLog -> OrbitLog) -> OrbitLog -> ST s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Orbits -> OrbitLog -> OrbitLog
forall a. Int -> a -> IntMap a -> IntMap a
IMap.insert Int
tag (Orbits -> OrbitLog -> OrbitLog) -> Orbits -> OrbitLog -> OrbitLog
forall a b. (a -> b) -> a -> b
$! (Orbits
orbit { ordinal = Nothing, getOrbits = mempty}))
                      (OrbitLog -> ST s ()) -> ST s OrbitLog -> ST s ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MScratch s -> STArray s Int OrbitLog
forall s. MScratch s -> STArray s Int OrbitLog
m_orbit MScratch s
s1 STArray s Int OrbitLog -> Int -> ST s OrbitLog
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
state

                    compressGroup [((Int, b), Orbits)]
gs = do
                      let sortPos :: (a, Orbits) -> (a, Orbits) -> Ordering
sortPos (a
_,Orbits
b1) (a
_,Orbits
b2) = Maybe Int -> Maybe Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Maybe Int
ordinal Orbits
b1) (Orbits -> Maybe Int
ordinal Orbits
b2) Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
`mappend`
                                                  Bool -> Bool -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Bool
inOrbit Orbits
b2) (Orbits -> Bool
inOrbit Orbits
b1) Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
`mappend`
                                                  ViewL Int -> ViewL Int -> Ordering
comparePos (Seq Int -> ViewL Int
forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Int
getOrbits Orbits
b1)) (Seq Int -> ViewL Int
forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Int
getOrbits Orbits
b2))
                          groupPos :: (a, Orbits) -> (a, Orbits) -> Bool
groupPos (a
_,Orbits
b1) (a
_,Orbits
b2) = Orbits -> Maybe Int
ordinal Orbits
b1 Maybe Int -> Maybe Int -> Bool
forall a. Eq a => a -> a -> Bool
== Orbits -> Maybe Int
ordinal Orbits
b2 Bool -> Bool -> Bool
&& Orbits -> Seq Int
getOrbits Orbits
b1 Seq Int -> Seq Int -> Bool
forall a. Eq a => a -> a -> Bool
== Orbits -> Seq Int
getOrbits Orbits
b2
                          gs' :: [(Int, [((Int, b), Orbits)])]
gs' = [Int] -> [[((Int, b), Orbits)]] -> [(Int, [((Int, b), Orbits)])]
forall a b. [a] -> [b] -> [(a, b)]
zip [(Int
1::Int)..] ((((Int, b), Orbits) -> ((Int, b), Orbits) -> Bool)
-> [((Int, b), Orbits)] -> [[((Int, b), Orbits)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy ((Int, b), Orbits) -> ((Int, b), Orbits) -> Bool
forall {a} {a}. (a, Orbits) -> (a, Orbits) -> Bool
groupPos ([((Int, b), Orbits)] -> [[((Int, b), Orbits)]])
-> ([((Int, b), Orbits)] -> [((Int, b), Orbits)])
-> [((Int, b), Orbits)]
-> [[((Int, b), Orbits)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (((Int, b), Orbits) -> ((Int, b), Orbits) -> Ordering)
-> [((Int, b), Orbits)] -> [((Int, b), Orbits)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy ((Int, b), Orbits) -> ((Int, b), Orbits) -> Ordering
forall {a} {a}. (a, Orbits) -> (a, Orbits) -> Ordering
sortPos ([((Int, b), Orbits)] -> [[((Int, b), Orbits)]])
-> [((Int, b), Orbits)] -> [[((Int, b), Orbits)]]
forall a b. (a -> b) -> a -> b
$ [((Int, b), Orbits)]
gs)
                      [(Int, [((Int, b), Orbits)])]
-> ((Int, [((Int, b), Orbits)]) -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Int, [((Int, b), Orbits)])]
gs' (((Int, [((Int, b), Orbits)]) -> ST s ()) -> ST s ())
-> ((Int, [((Int, b), Orbits)]) -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \ (!Int
n,[((Int, b), Orbits)]
eqs) -> do
                        [((Int, b), Orbits)] -> (((Int, b), Orbits) -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [((Int, b), Orbits)]
eqs ((((Int, b), Orbits) -> ST s ()) -> ST s ())
-> (((Int, b), Orbits) -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \ ((Int
state,b
_),Orbits
orbit) ->
                          STArray s Int OrbitLog -> Int -> OrbitLog -> ST s ()
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> e -> ST s ()
set (MScratch s -> STArray s Int OrbitLog
forall s. MScratch s -> STArray s Int OrbitLog
m_orbit MScratch s
s1) Int
state
                           (OrbitLog -> ST s ())
-> (OrbitLog -> OrbitLog) -> OrbitLog -> ST s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Orbits -> OrbitLog -> OrbitLog
forall a. Int -> a -> IntMap a -> IntMap a
IMap.insert Int
tag (Orbits -> OrbitLog -> OrbitLog) -> Orbits -> OrbitLog -> OrbitLog
forall a b. (a -> b) -> a -> b
$! (Orbits
orbit { ordinal = Just n, getOrbits = mempty }))
                            (OrbitLog -> ST s ()) -> ST s OrbitLog -> ST s ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MScratch s -> STArray s Int OrbitLog
forall s. MScratch s -> STArray s Int OrbitLog
m_orbit MScratch s
s1 STArray s Int OrbitLog -> Int -> ST s OrbitLog
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
state
                let sorter ((a
_,a
a1),Orbits
b1) ((a
_,a
a2),Orbits
b2) = a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
a1 a
a2 Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
`mappend` Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Int
basePos Orbits
b1) (Orbits -> Int
basePos Orbits
b2)
                    grouper ((a
_,a
a1),Orbits
b1) ((a
_,a
a2),Orbits
b2) = a
a1a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
a2 Bool -> Bool -> Bool
&& Orbits -> Int
basePos Orbits
b1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Orbits -> Int
basePos Orbits
b2
                    orbitGroups = (((Int, Int), Orbits) -> ((Int, Int), Orbits) -> Bool)
-> [((Int, Int), Orbits)] -> [[((Int, Int), Orbits)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy ((Int, Int), Orbits) -> ((Int, Int), Orbits) -> Bool
forall {a} {a} {a}.
Eq a =>
((a, a), Orbits) -> ((a, a), Orbits) -> Bool
grouper ([((Int, Int), Orbits)] -> [[((Int, Int), Orbits)]])
-> ([Maybe ((Int, Int), Orbits)] -> [((Int, Int), Orbits)])
-> [Maybe ((Int, Int), Orbits)]
-> [[((Int, Int), Orbits)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (((Int, Int), Orbits) -> ((Int, Int), Orbits) -> Ordering)
-> [((Int, Int), Orbits)] -> [((Int, Int), Orbits)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy ((Int, Int), Orbits) -> ((Int, Int), Orbits) -> Ordering
forall {a} {a} {a}.
Ord a =>
((a, a), Orbits) -> ((a, a), Orbits) -> Ordering
sorter ([((Int, Int), Orbits)] -> [((Int, Int), Orbits)])
-> ([Maybe ((Int, Int), Orbits)] -> [((Int, Int), Orbits)])
-> [Maybe ((Int, Int), Orbits)]
-> [((Int, Int), Orbits)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe ((Int, Int), Orbits)] -> [((Int, Int), Orbits)]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe ((Int, Int), Orbits)] -> [[((Int, Int), Orbits)]])
-> [Maybe ((Int, Int), Orbits)] -> [[((Int, Int), Orbits)]]
forall a b. (a -> b) -> a -> b
$ [Maybe ((Int, Int), Orbits)]
mos
                mapM_ compressGroup orbitGroups
          mapM_ compressOrbit orbitTags

-- findTrans has to (part 1) decide, for each destination, "which" of
-- zero or more source NFA states will be the chosen source.  Then it
-- has to (part 2) perform the transition or spawn.  It keeps track of
-- the starting index while doing so, and compares the earliest start
-- with the stored winners.  (part 3) If some winners are ready to be
-- released then the future continuation of the search is placed in
-- "storeNext".  If no winners are ready to be released then the
-- computation continues immediately.

        findTrans
          :: MScratch s
          -> MScratch s
          -> IntSet
          -> SetIndex
          -> DT
          -> DTrans
          -> Index
          -> Char
          -> text
          -> ST s [MatchArray]
        findTrans MScratch s
s1 MScratch s
s2 SetIndex
did SetIndex
did' DT
dt' DTrans
dtrans Int
offset Char
prev' text
input' =  {-# SCC "goNext.findTrans" #-} do
          -- findTrans part 0
          -- MAGIC TUNABLE CONSTANT 100 (and 100-1). TODO: (offset .&. 127 == 127) instead?
          Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not ([Int] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
orbitTags) Bool -> Bool -> Bool
&& (Int
offset Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Int
100 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
99)) (MScratch s -> SetIndex -> Int -> ST s ()
compressOrbits MScratch s
s1 SetIndex
did Int
offset)
          -- findTrans part 1
          let findTransTo :: (Int, IntMap (a, Instructions)) -> ST s ()
findTransTo (Int
destIndex,IntMap (a, Instructions)
sources) | IntMap (a, Instructions) -> Bool
forall a. IntMap a -> Bool
IMap.null IntMap (a, Instructions)
sources =
                STArray s Int ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> Int
-> ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> ST s ()
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> e -> ST s ()
set STArray s Int ((Int, Instructions), STUArray s Int Int, OrbitLog)
which Int
destIndex ((Int, Instructions), STUArray s Int Int, OrbitLog)
forall s. ((Int, Instructions), STUArray s Int Int, OrbitLog)
noSource
                                              | Bool
otherwise = do
                let prep :: (Int, (a, Instructions))
-> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog)
prep (Int
sourceIndex,(a
_dopa,Instructions
instructions)) = {-# SCC "goNext.findTrans.prep" #-} do
                      pos <- ST s (STUArray s Int Int)
-> (STUArray s Int Int -> ST s (STUArray s Int Int))
-> Maybe (STUArray s Int Int)
-> ST s (STUArray s Int Int)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> ST s (STUArray s Int Int)
forall a. String -> a
err (String -> ST s (STUArray s Int Int))
-> String -> ST s (STUArray s Int Int)
forall a b. (a -> b) -> a -> b
$ String
"findTrans,1 : "String -> String -> String
forall a. [a] -> [a] -> [a]
++(Int, Int, SetIndex) -> String
forall a. Show a => a -> String
show (Int
sourceIndex,Int
destIndex,SetIndex
did')) STUArray s Int Int -> ST s (STUArray s Int Int)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return
                               (Maybe (STUArray s Int Int) -> ST s (STUArray s Int Int))
-> ST s (Maybe (STUArray s Int Int)) -> ST s (STUArray s Int Int)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MScratch s -> STArray s Int (Maybe (STUArray s Int Int))
forall s. MScratch s -> STArray s Int (Maybe (STUArray s Int Int))
m_pos MScratch s
s1 STArray s Int (Maybe (STUArray s Int Int))
-> Int -> ST s (Maybe (STUArray s Int Int))
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
sourceIndex
                      orbit <- m_orbit s1 !! sourceIndex
                      let orbit' = OrbitLog
-> ((Int -> OrbitLog -> OrbitLog) -> OrbitLog)
-> Maybe (Int -> OrbitLog -> OrbitLog)
-> OrbitLog
forall b a. b -> (a -> b) -> Maybe a -> b
maybe OrbitLog
orbit (\ Int -> OrbitLog -> OrbitLog
f -> Int -> OrbitLog -> OrbitLog
f Int
offset OrbitLog
orbit) (Instructions -> Maybe (Int -> OrbitLog -> OrbitLog)
newOrbits Instructions
instructions)
                      return ((sourceIndex,instructions),pos,orbit')
                    challenge :: ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog)
challenge x1 :: ((Int, Instructions), STUArray s Int Int, OrbitLog)
x1@((Int
_si1,Instructions
ins1),STUArray s Int Int
_p1,OrbitLog
_o1) x2 :: ((Int, Instructions), STUArray s Int Int, OrbitLog)
x2@((Int
_si2,Instructions
ins2),STUArray s Int Int
_p2,OrbitLog
_o2) = {-# SCC "goNext.findTrans.challenge" #-} do
                      check <- C s
forall s. C s
comp Int
offset ((Int, Instructions), STUArray s Int Int, OrbitLog)
x1 (Instructions -> [(Int, Action)]
newPos Instructions
ins1) ((Int, Instructions), STUArray s Int Int, OrbitLog)
x2 (Instructions -> [(Int, Action)]
newPos Instructions
ins2)
                      if check==LT then return x2 else return x1
                first_rest <- ((Int, (a, Instructions))
 -> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog))
-> [(Int, (a, Instructions))]
-> ST s [((Int, Instructions), STUArray s Int Int, OrbitLog)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Int, (a, Instructions))
-> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog)
forall {s} {a}.
(MArray (STArray s) (Maybe (STUArray s Int Int)) (ST s),
 MArray (STArray s) OrbitLog (ST s)) =>
(Int, (a, Instructions))
-> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog)
prep (IntMap (a, Instructions) -> [(Int, (a, Instructions))]
forall a. IntMap a -> [(Int, a)]
IMap.toList IntMap (a, Instructions)
sources)
                let first:rest = first_rest
                set which destIndex =<< foldM challenge first rest
          let dl :: [(Int, IntMap (DoPa, Instructions))]
dl = DTrans -> [(Int, IntMap (DoPa, Instructions))]
forall a. IntMap a -> [(Int, a)]
IMap.toList DTrans
dtrans
          ((Int, IntMap (DoPa, Instructions)) -> ST s ())
-> [(Int, IntMap (DoPa, Instructions))] -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Int, IntMap (DoPa, Instructions)) -> ST s ()
forall {a}. (Int, IntMap (a, Instructions)) -> ST s ()
findTransTo [(Int, IntMap (DoPa, Instructions))]
dl
          -- findTrans part 2
          let performTransTo :: (Int, b) -> ST s ()
performTransTo (Int
destIndex,b
_sources) = {-# SCC "goNext.findTrans.performTransTo" #-} do
                x@((sourceIndex,_instructions),_pos,_orbit') <- STArray s Int ((Int, Instructions), STUArray s Int Int, OrbitLog)
which STArray s Int ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> Int -> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog)
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
destIndex
                unless (sourceIndex == (-1)) $
                  (updateCopy x offset s2 destIndex)
          ((Int, IntMap (DoPa, Instructions)) -> ST s ())
-> [(Int, IntMap (DoPa, Instructions))] -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Int, IntMap (DoPa, Instructions)) -> ST s ()
forall {b}. (Int, b) -> ST s ()
performTransTo [(Int, IntMap (DoPa, Instructions))]
dl
          -- findTrans part 3
          let offset' :: Int
offset' = Int -> Int
forall a. Enum a => a -> a
succ Int
offset in Int -> ST s [MatchArray] -> ST s [MatchArray]
forall a b. a -> b -> b
seq Int
offset' (ST s [MatchArray] -> ST s [MatchArray])
-> ST s [MatchArray] -> ST s [MatchArray]
forall a b. (a -> b) -> a -> b
$ MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Int
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s2 MScratch s
s1 SetIndex
did' DT
dt' Int
offset' Char
prev' text
input'

-- The "newWinnerThenProceed" can find both a new non-empty winner and
-- a new empty winner.  A new non-empty winner can cause some of the
-- NFA states that comprise the DFA state to be eliminated, and if the
-- startState is eliminated then it must then be respawned.  And
-- imperative flag setting and resetting style is used.
--
-- A non-empty winner from the startState might obscure a potential
-- empty winner (form the startState at the current offset).  This
-- winEmpty possibility is also checked for. (unit test pattern ".*")
-- (further test "(.+|.+.)*" on "aa\n")

        {-# INLINE processWinner #-}
        processWinner :: MScratch s -> IntMap Instructions -> Position -> ST s ()
        processWinner MScratch s
s1 IntMap Instructions
w Int
offset = {-# SCC "goNext.newWinnerThenProceed" #-} do
          let prep :: (Int, Instructions)
-> ST s (Int, ((Int, Instructions), STUArray s Int Int, OrbitLog))
prep x :: (Int, Instructions)
x@(Int
sourceIndex,Instructions
instructions) = {-# SCC "goNext.newWinnerThenProceed.prep" #-} do
                pos <- ST s (STUArray s Int Int)
-> (STUArray s Int Int -> ST s (STUArray s Int Int))
-> Maybe (STUArray s Int Int)
-> ST s (STUArray s Int Int)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> ST s (STUArray s Int Int)
forall a. String -> a
err String
"newWinnerThenProceed,1") STUArray s Int Int -> ST s (STUArray s Int Int)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (STUArray s Int Int) -> ST s (STUArray s Int Int))
-> ST s (Maybe (STUArray s Int Int)) -> ST s (STUArray s Int Int)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MScratch s -> STArray s Int (Maybe (STUArray s Int Int))
forall s. MScratch s -> STArray s Int (Maybe (STUArray s Int Int))
m_pos MScratch s
s1 STArray s Int (Maybe (STUArray s Int Int))
-> Int -> ST s (Maybe (STUArray s Int Int))
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
sourceIndex
                startPos <- pos !! 0
                orbit <- m_orbit s1 !! sourceIndex
                let orbit' = OrbitLog
-> ((Int -> OrbitLog -> OrbitLog) -> OrbitLog)
-> Maybe (Int -> OrbitLog -> OrbitLog)
-> OrbitLog
forall b a. b -> (a -> b) -> Maybe a -> b
maybe OrbitLog
orbit (\ Int -> OrbitLog -> OrbitLog
f -> Int -> OrbitLog -> OrbitLog
f Int
offset OrbitLog
orbit) (Instructions -> Maybe (Int -> OrbitLog -> OrbitLog)
newOrbits Instructions
instructions)
                return (startPos,(x,pos,orbit'))
              challenge :: ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog)
challenge x1 :: ((Int, Instructions), STUArray s Int Int, OrbitLog)
x1@((Int
_si1,Instructions
ins1),STUArray s Int Int
_p1,OrbitLog
_o1) x2 :: ((Int, Instructions), STUArray s Int Int, OrbitLog)
x2@((Int
_si2,Instructions
ins2),STUArray s Int Int
_p2,OrbitLog
_o2) = {-# SCC "goNext.newWinnerThenProceed.challenge" #-} do
                check <- C s
forall s. C s
comp Int
offset ((Int, Instructions), STUArray s Int Int, OrbitLog)
x1 (Instructions -> [(Int, Action)]
newPos Instructions
ins1) ((Int, Instructions), STUArray s Int Int, OrbitLog)
x2 (Instructions -> [(Int, Action)]
newPos Instructions
ins2)
                if check==LT then return x2 else return x1
          prep'd <- ((Int, Instructions)
 -> ST s (Int, ((Int, Instructions), STUArray s Int Int, OrbitLog)))
-> [(Int, Instructions)]
-> ST
     s [(Int, ((Int, Instructions), STUArray s Int Int, OrbitLog))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Int, Instructions)
-> ST s (Int, ((Int, Instructions), STUArray s Int Int, OrbitLog))
forall {s}.
(MArray (STUArray s) Int (ST s),
 MArray (STArray s) (Maybe (STUArray s Int Int)) (ST s),
 MArray (STArray s) OrbitLog (ST s)) =>
(Int, Instructions)
-> ST s (Int, ((Int, Instructions), STUArray s Int Int, OrbitLog))
prep (IntMap Instructions -> [(Int, Instructions)]
forall a. IntMap a -> [(Int, a)]
IMap.toList IntMap Instructions
w)
          case map snd prep'd of
            [] -> () -> ST s ()
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            (((Int, Instructions), STUArray s Int Int, OrbitLog)
first:[((Int, Instructions), STUArray s Int Int, OrbitLog)]
rest) -> Int
-> ((Int, Instructions), STUArray s Int Int, OrbitLog) -> ST s ()
forall a c.
Int -> ((a, Instructions), STUArray s Int Int, c) -> ST s ()
newWinner Int
offset (((Int, Instructions), STUArray s Int Int, OrbitLog) -> ST s ())
-> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> ST s ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (((Int, Instructions), STUArray s Int Int, OrbitLog)
 -> ((Int, Instructions), STUArray s Int Int, OrbitLog)
 -> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog))
-> ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> [((Int, Instructions), STUArray s Int Int, OrbitLog)]
-> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog)
forall {s}.
((Int, Instructions), STUArray s Int Int, OrbitLog)
-> ((Int, Instructions), STUArray s Int Int, OrbitLog)
-> ST s ((Int, Instructions), STUArray s Int Int, OrbitLog)
challenge ((Int, Instructions), STUArray s Int Int, OrbitLog)
first [((Int, Instructions), STUArray s Int Int, OrbitLog)]
rest

        newWinner :: Position -> ((a, Instructions), STUArray s Tag Position, c) -> ST s ()
        newWinner Int
preTag ((a
_sourceIndex,Instructions
winInstructions),STUArray s Int Int
oldPos,c
_newOrbit) = {-# SCC "goNext.newWinner" #-} do
          newerPos <- (Int, Int) -> ST s (STUArray s Int Int)
forall s e.
MArray (STUArray s) e (ST s) =>
(Int, Int) -> ST s (STUArray s Int e)
newA_ (Int, Int)
b_tags
          copySTU oldPos newerPos
          doActions preTag newerPos (newPos winInstructions)
          putMQ (WScratch newerPos) winQ

        finalizeWinner :: ST s [MatchArray]
        finalizeWinner = do
          mWinner <- STRef s (Maybe (WScratch s)) -> ST s (Maybe (WScratch s))
forall s a. STRef s a -> ST s a
readSTRef (MQ s -> STRef s (Maybe (WScratch s))
forall s. MQ s -> STRef s (Maybe (WScratch s))
mq_mWin MQ s
winQ)
          case mWinner of
            Maybe (WScratch s)
Nothing -> [MatchArray] -> ST s [MatchArray]
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return []
            Just WScratch s
winner -> MQ s -> ST s ()
forall s. MQ s -> ST s ()
resetMQ MQ s
winQ ST s () -> ST s [MatchArray] -> ST s [MatchArray]
forall a b. ST s a -> ST s b -> ST s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (WScratch s -> ST s MatchArray)
-> [WScratch s] -> ST s [MatchArray]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Array Int [GroupInfo] -> WScratch s -> ST s MatchArray
forall s. Array Int [GroupInfo] -> WScratch s -> ST s MatchArray
tagsToGroupsST Array Int [GroupInfo]
aGroups) [WScratch s
winner]

    -- goNext then ends with the next statement
    next s1In s2In didIn dtIn offsetIn prevIn inputIn

{-# INLINE doActions #-}
doActions :: Position -> STUArray s Tag Position -> [(Tag, Action)] -> ST s ()
doActions :: forall s. Int -> STUArray s Int Int -> [(Int, Action)] -> ST s ()
doActions Int
preTag STUArray s Int Int
pos [(Int, Action)]
ins = ((Int, Action) -> ST s ()) -> [(Int, Action)] -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Int, Action) -> ST s ()
forall {s}.
MArray (STUArray s) Int (ST s) =>
(Int, Action) -> ST s ()
doAction [(Int, Action)]
ins where
  postTag :: Int
postTag = Int -> Int
forall a. Enum a => a -> a
succ Int
preTag
  doAction :: (Int, Action) -> ST s ()
doAction (Int
tag,Action
SetPre) = STUArray s Int Int -> Int -> Int -> ST s ()
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> e -> ST s ()
set STUArray s Int Int
pos Int
tag Int
preTag
  doAction (Int
tag,Action
SetPost) = STUArray s Int Int -> Int -> Int -> ST s ()
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> e -> ST s ()
set STUArray s Int Int
pos Int
tag Int
postTag
  doAction (Int
tag,SetVal Int
v) = STUArray s Int Int -> Int -> Int -> ST s ()
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> e -> ST s ()
set STUArray s Int Int
pos Int
tag Int
v

----

{-# INLINE mkTest #-}
mkTest :: Uncons text => Bool -> WhichTest -> Index -> Char -> text -> Bool
mkTest :: forall text.
Uncons text =>
Bool -> WhichTest -> Int -> Char -> text -> Bool
mkTest Bool
isMultiline = if Bool
isMultiline then WhichTest -> Int -> Char -> text -> Bool
forall text.
Uncons text =>
WhichTest -> Int -> Char -> text -> Bool
test_multiline else WhichTest -> Int -> Char -> text -> Bool
forall text.
Uncons text =>
WhichTest -> Int -> Char -> text -> Bool
test_singleline

----

{- MUTABLE WINNER QUEUE -}

newtype MQ s = MQ { forall s. MQ s -> STRef s (Maybe (WScratch s))
mq_mWin :: STRef s (Maybe (WScratch s)) }

newMQ :: S.ST s (MQ s)
newMQ :: forall s. ST s (MQ s)
newMQ = do
  mWin <- Maybe (WScratch s) -> ST s (STRef s (Maybe (WScratch s)))
forall a s. a -> ST s (STRef s a)
newSTRef Maybe (WScratch s)
forall a. Maybe a
Nothing
  return (MQ mWin)

resetMQ :: MQ s -> S.ST s ()
resetMQ :: forall s. MQ s -> ST s ()
resetMQ (MQ {mq_mWin :: forall s. MQ s -> STRef s (Maybe (WScratch s))
mq_mWin=STRef s (Maybe (WScratch s))
mWin}) = do
  STRef s (Maybe (WScratch s)) -> Maybe (WScratch s) -> ST s ()
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s (Maybe (WScratch s))
mWin Maybe (WScratch s)
forall a. Maybe a
Nothing

putMQ :: WScratch s -> MQ s -> S.ST s ()
putMQ :: forall s. WScratch s -> MQ s -> ST s ()
putMQ WScratch s
ws (MQ {mq_mWin :: forall s. MQ s -> STRef s (Maybe (WScratch s))
mq_mWin=STRef s (Maybe (WScratch s))
mWin}) = do
  STRef s (Maybe (WScratch s)) -> Maybe (WScratch s) -> ST s ()
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s (Maybe (WScratch s))
mWin (WScratch s -> Maybe (WScratch s)
forall a. a -> Maybe a
Just WScratch s
ws)

{- MUTABLE SCRATCH DATA STRUCTURES -}

data SScratch s = SScratch { forall s. SScratch s -> MScratch s
_s_1 :: !(MScratch s)
                           , forall s. SScratch s -> MScratch s
_s_2 :: !(MScratch s)
                           , forall s.
SScratch s
-> (MQ s, BlankScratch s,
    STArray s Int ((Int, Instructions), STUArray s Int Int, OrbitLog))
_s_rest :: !( MQ s
                                        , BlankScratch s
                                        , STArray s Index ((Index,Instructions),STUArray s Tag Position,OrbitLog)
                                        )
                           }
data MScratch s = MScratch { forall s. MScratch s -> STArray s Int (Maybe (STUArray s Int Int))
m_pos :: !(STArray s Index (Maybe (STUArray s Tag Position)))
                           , forall s. MScratch s -> STArray s Int OrbitLog
m_orbit :: !(STArray s Index OrbitLog)
                           }
newtype BlankScratch s = BlankScratch { forall s. BlankScratch s -> STUArray s Int Int
_blank_pos :: (STUArray s Tag Position)
                                      }
newtype WScratch s = WScratch { forall s. WScratch s -> STUArray s Int Int
w_pos :: (STUArray s Tag Position)
                              }

{- DEBUGGING HELPERS -}

{-
indent :: String -> String
indent xs = ' ':' ':xs

showMS :: MScratch s -> Index -> ST s String
showMS s i = do
  ma <- m_pos s !! i
  mc <- m_orbit s !! i
  a <- case ma of
        Nothing -> return "No pos"
        Just pos -> fmap show (getAssocs pos)
  let c = show mc
  return $ unlines [ "MScratch, index = "++show i
                   , indent a
                   , indent c]

showWS :: WScratch s -> ST s String
showWS (WScratch pos) = do
  a <- getAssocs pos
  return $ unlines [ "WScratch"
                   , indent (show a)]
-}
{- CREATING INITIAL MUTABLE SCRATCH DATA STRUCTURES -}

{-# INLINE newA #-}
newA :: (MArray (STUArray s) e (ST s)) => (Tag,Tag) -> e -> S.ST s (STUArray s Tag e)
newA :: forall s e.
MArray (STUArray s) e (ST s) =>
(Int, Int) -> e -> ST s (STUArray s Int e)
newA (Int, Int)
b_tags e
initial = (Int, Int) -> e -> ST s (STUArray s Int e)
forall i. Ix i => (i, i) -> e -> ST s (STUArray s i e)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Int, Int)
b_tags e
initial

{-# INLINE newA_ #-}
newA_ :: (MArray (STUArray s) e (ST s)) => (Tag,Tag) -> S.ST s (STUArray s Tag e)
newA_ :: forall s e.
MArray (STUArray s) e (ST s) =>
(Int, Int) -> ST s (STUArray s Int e)
newA_ (Int, Int)
b_tags = (Int, Int) -> ST s (STUArray s Int e)
forall i. Ix i => (i, i) -> ST s (STUArray s i e)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Int, Int)
b_tags

newScratch :: (Index,Index) -> (Tag,Tag) -> S.ST s (SScratch s)
newScratch :: forall s. (Int, Int) -> (Int, Int) -> ST s (SScratch s)
newScratch (Int, Int)
b_index (Int, Int)
b_tags = do
  s1 <- (Int, Int) -> ST s (MScratch s)
forall s. (Int, Int) -> ST s (MScratch s)
newMScratch (Int, Int)
b_index
  s2 <- newMScratch b_index
  winQ <- newMQ
  blank <- fmap BlankScratch (newA b_tags (-1))
  which <- (newArray b_index ((-1,err "newScratch which 1"),err "newScratch which 2",err "newScratch which 3"))
  return (SScratch s1 s2 (winQ,blank,which))

newMScratch :: (Index,Index) -> S.ST s (MScratch s)
newMScratch :: forall s. (Int, Int) -> ST s (MScratch s)
newMScratch (Int, Int)
b_index = do
  pos's <- (Int, Int)
-> Maybe (STUArray s Int Int)
-> ST s (STArray s Int (Maybe (STUArray s Int Int)))
forall i.
Ix i =>
(i, i)
-> Maybe (STUArray s Int Int)
-> ST s (STArray s i (Maybe (STUArray s Int Int)))
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Int, Int)
b_index Maybe (STUArray s Int Int)
forall a. Maybe a
Nothing
  orbit's <- newArray b_index Mon.mempty
  return (MScratch pos's orbit's)

{- COMPOSE A FUNCTION CLOSURE TO COMPARE TAG VALUES -}

newtype F s = F ([F s] -> C s)
type C s = Position
        -> ((Int, Instructions), STUArray s Tag Position, IntMap Orbits)
        -> [(Int, Action)]
        -> ((Int, Instructions), STUArray s Tag Position, IntMap Orbits)
        -> [(Int, Action)]
        -> ST s Ordering

{-# INLINE orderOf #-}
orderOf :: Action -> Action -> Ordering
orderOf :: Action -> Action -> Ordering
orderOf Action
post1 Action
post2 =
  case (Action
post1,Action
post2) of
    (Action
SetPre,Action
SetPre) -> Ordering
EQ
    (Action
SetPost,Action
SetPost) -> Ordering
EQ
    (Action
SetPre,Action
SetPost) -> Ordering
LT
    (Action
SetPost,Action
SetPre) -> Ordering
GT
    (SetVal Int
v1,SetVal Int
v2) -> Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
v1 Int
v2
    (Action, Action)
_ -> String -> Ordering
forall a. String -> a
err (String -> Ordering) -> String -> Ordering
forall a b. (a -> b) -> a -> b
$ String
"bestTrans.compareWith.choose sees incomparable "String -> String -> String
forall a. [a] -> [a] -> [a]
++(Action, Action) -> String
forall a. Show a => a -> String
show (Action
post1,Action
post2)

ditzyComp'3 :: forall s. Array Tag OP -> C s
ditzyComp'3 :: forall s. Array Int OP -> C s
ditzyComp'3 Array Int OP
aTagOP = C s
comp0 where
  (F [F s] -> C s
comp1:[F s]
compsRest) = Int -> [F s]
allcomps Int
1

  comp0 :: C s
  comp0 :: C s
comp0 Int
preTag x1 :: ((Int, Instructions), STUArray s Int Int, OrbitLog)
x1@((Int, Instructions)
_state1,STUArray s Int Int
pos1,OrbitLog
_orbit1') [(Int, Action)]
np1 x2 :: ((Int, Instructions), STUArray s Int Int, OrbitLog)
x2@((Int, Instructions)
_state2,STUArray s Int Int
pos2,OrbitLog
_orbit2') [(Int, Action)]
np2 = do
    c <- (Int -> Int -> Ordering) -> ST s Int -> ST s Int -> ST s Ordering
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (STUArray s Int Int
pos2STUArray s Int Int -> Int -> ST s Int
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!!Int
0) (STUArray s Int Int
pos1STUArray s Int Int -> Int -> ST s Int
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!!Int
0) -- reversed since Minimize
    case c of
      Ordering
EQ -> [F s] -> C s
comp1 [F s]
compsRest Int
preTag ((Int, Instructions), STUArray s Int Int, OrbitLog)
x1 [(Int, Action)]
np1 ((Int, Instructions), STUArray s Int Int, OrbitLog)
x2 [(Int, Action)]
np2
      Ordering
answer -> Ordering -> ST s Ordering
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
answer

  allcomps :: Tag -> [F s]
  allcomps :: Int -> [F s]
allcomps Int
tag | Int
tag Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
top = [([F s] -> C s) -> F s
forall s. ([F s] -> C s) -> F s
F (\ [F s]
_ Int
_ ((Int, Instructions), STUArray s Int Int, OrbitLog)
_ [(Int, Action)]
_ ((Int, Instructions), STUArray s Int Int, OrbitLog)
_ [(Int, Action)]
_ -> Ordering -> ST s Ordering
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
EQ)]
               | Bool
otherwise =
    case Array Int OP
aTagOP Array Int OP -> Int -> OP
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Int
tag of
      OP
Orbit -> ([F s] -> C s) -> F s
forall s. ([F s] -> C s) -> F s
F (Int -> [F s] -> C s
forall s1.
Int
-> [F s1]
-> Int
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ST s1 Ordering
challenge_Orb Int
tag) F s -> [F s] -> [F s]
forall a. a -> [a] -> [a]
: Int -> [F s]
allcomps (Int -> Int
forall a. Enum a => a -> a
succ Int
tag)
      OP
Maximize -> ([F s] -> C s) -> F s
forall s. ([F s] -> C s) -> F s
F (Int -> [F s] -> C s
forall s1.
Int
-> [F s1]
-> Int
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ST s1 Ordering
challenge_Max Int
tag) F s -> [F s] -> [F s]
forall a. a -> [a] -> [a]
: Int -> [F s]
allcomps (Int -> Int
forall a. Enum a => a -> a
succ Int
tag)
      OP
Ignore -> ([F s] -> C s) -> F s
forall s. ([F s] -> C s) -> F s
F (Int -> [F s] -> C s
forall s1.
Int
-> [F s1]
-> Int
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ST s1 Ordering
challenge_Ignore Int
tag) F s -> [F s] -> [F s]
forall a. a -> [a] -> [a]
: Int -> [F s]
allcomps (Int -> Int
forall a. Enum a => a -> a
succ Int
tag)
      OP
Minimize -> String -> [F s]
forall a. String -> a
err String
"allcomps Minimize"
   where top :: Int
top = (Int, Int) -> Int
forall a b. (a, b) -> b
snd (Array Int OP -> (Int, Int)
forall i. Ix i => Array i OP -> (i, i)
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> (i, i)
bounds Array Int OP
aTagOP)

  challenge_Ignore
    :: Int
    -> [F s1]
    -> Position
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ST s1 Ordering
  challenge_Ignore :: forall s1.
Int
-> [F s1]
-> Int
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ST s1 Ordering
challenge_Ignore !Int
tag (F [F s1] -> C s1
next:[F s1]
comps) Int
preTag ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1 [(Int, Action)]
np1 ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2 [(Int, Action)]
np2 =
    case [(Int, Action)]
np1 of
      ((Int
t1,Action
_):[(Int, Action)]
rest1) | Int
t1Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
tag ->
        case [(Int, Action)]
np2 of
          ((Int
t2,Action
_):[(Int, Action)]
rest2) | Int
t2Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
tag -> [F s1] -> C s1
next [F s1]
comps Int
preTag ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1 [(Int, Action)]
rest1 ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2 [(Int, Action)]
rest2
          [(Int, Action)]
_ -> [F s1] -> C s1
next [F s1]
comps Int
preTag ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1 [(Int, Action)]
rest1 ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2 [(Int, Action)]
np2
      [(Int, Action)]
_ -> do
        case [(Int, Action)]
np2 of
          ((Int
t2,Action
_):[(Int, Action)]
rest2) | Int
t2Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
tag -> [F s1] -> C s1
next [F s1]
comps Int
preTag ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1 [(Int, Action)]
np1 ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2 [(Int, Action)]
rest2
          [(Int, Action)]
_ ->  [F s1] -> C s1
next [F s1]
comps Int
preTag ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1 [(Int, Action)]
np1 ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2 [(Int, Action)]
np2
  challenge_Ignore Int
_ [] Int
_ ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
_ [(Int, Action)]
_ ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
_ [(Int, Action)]
_ = String -> ST s1 Ordering
forall a. String -> a
err String
"impossible 2347867"

  challenge_Max
    :: Int
    -> [F s1]
    -> Position
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ST s1 Ordering
  challenge_Max :: forall s1.
Int
-> [F s1]
-> Int
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ST s1 Ordering
challenge_Max !Int
tag (F [F s1] -> C s1
next:[F s1]
comps) Int
preTag x1 :: ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1@((Int, Instructions)
_state1,STUArray s1 Int Int
pos1,OrbitLog
_orbit1') [(Int, Action)]
np1 x2 :: ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2@((Int, Instructions)
_state2,STUArray s1 Int Int
pos2,OrbitLog
_orbit2') [(Int, Action)]
np2 =
    case [(Int, Action)]
np1 of
      ((Int
t1,Action
b1):[(Int, Action)]
rest1) | Int
t1Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
tag ->
        case [(Int, Action)]
np2 of
          ((Int
t2,Action
b2):[(Int, Action)]
rest2) | Int
t2Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
tag ->
            if Action
b1Action -> Action -> Bool
forall a. Eq a => a -> a -> Bool
==Action
b2 then [F s1] -> C s1
next [F s1]
comps Int
preTag ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1 [(Int, Action)]
rest1 ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2 [(Int, Action)]
rest2
              else Ordering -> ST s1 Ordering
forall a. a -> ST s1 a
forall (m :: * -> *) a. Monad m => a -> m a
return (Action -> Action -> Ordering
orderOf Action
b1 Action
b2)
          [(Int, Action)]
_ -> do
            p2 <- STUArray s1 Int Int
pos2 STUArray s1 Int Int -> Int -> ST s1 Int
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
tag
            let p1 = case Action
b1 of Action
SetPre -> Int
preTag
                                Action
SetPost -> Int -> Int
forall a. Enum a => a -> a
succ Int
preTag
                                SetVal Int
v -> Int
v
            if p1==p2 then next comps preTag x1 rest1 x2 np2
              else return (compare p1 p2)
      [(Int, Action)]
_ -> do
        p1 <- STUArray s1 Int Int
pos1 STUArray s1 Int Int -> Int -> ST s1 Int
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
tag
        case np2 of
          ((Int
t2,Action
b2):[(Int, Action)]
rest2) | Int
t2Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
tag -> do
            let p2 :: Int
p2 = case Action
b2 of Action
SetPre -> Int
preTag
                                Action
SetPost -> Int -> Int
forall a. Enum a => a -> a
succ Int
preTag
                                SetVal Int
v -> Int
v
            if Int
p1Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
p2 then [F s1] -> C s1
next [F s1]
comps Int
preTag ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1 [(Int, Action)]
np1 ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2 [(Int, Action)]
rest2
              else Ordering -> ST s1 Ordering
forall a. a -> ST s1 a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
p1 Int
p2)
          [(Int, Action)]
_ -> do
            p2 <- STUArray s1 Int Int
pos2 STUArray s1 Int Int -> Int -> ST s1 Int
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
tag
            if p1==p2 then next comps preTag x1 np1 x2 np2
              else return (compare p1 p2)
  challenge_Max Int
_ [] Int
_ ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
_ [(Int, Action)]
_ ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
_ [(Int, Action)]
_ = String -> ST s1 Ordering
forall a. String -> a
err String
"impossible 9384324"

  challenge_Orb
    :: Int
    -> [F s1]
    -> Position
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ST s1 Ordering
  challenge_Orb :: forall s1.
Int
-> [F s1]
-> Int
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
-> [(Int, Action)]
-> ST s1 Ordering
challenge_Orb !Int
tag (F [F s1] -> C s1
next:[F s1]
comps) Int
preTag x1 :: ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1@((Int, Instructions)
_state1,STUArray s1 Int Int
_pos1,OrbitLog
orbit1') [(Int, Action)]
np1 x2 :: ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2@((Int, Instructions)
_state2,STUArray s1 Int Int
_pos2,OrbitLog
orbit2') [(Int, Action)]
np2 =
    let s1 :: Maybe Orbits
s1 = Int -> OrbitLog -> Maybe Orbits
forall a. Int -> IntMap a -> Maybe a
IMap.lookup Int
tag OrbitLog
orbit1'
        s2 :: Maybe Orbits
s2 = Int -> OrbitLog -> Maybe Orbits
forall a. Int -> IntMap a -> Maybe a
IMap.lookup Int
tag OrbitLog
orbit2'
    in case (Maybe Orbits
s1,Maybe Orbits
s2) of
         (Maybe Orbits
Nothing,Maybe Orbits
Nothing) -> [F s1] -> C s1
next [F s1]
comps Int
preTag ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1 [(Int, Action)]
np1 ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2 [(Int, Action)]
np2
         (Just Orbits
o1,Just Orbits
o2) | Orbits -> Bool
inOrbit Orbits
o1 Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Orbits -> Bool
inOrbit Orbits
o2 ->
            case Maybe Int -> Maybe Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Maybe Int
ordinal Orbits
o1) (Orbits -> Maybe Int
ordinal Orbits
o2) Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
`mappend`
                 ViewL Int -> ViewL Int -> Ordering
comparePos (Seq Int -> ViewL Int
forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Int
getOrbits Orbits
o1)) (Seq Int -> ViewL Int
forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Int
getOrbits Orbits
o2)) of
              Ordering
EQ -> [F s1] -> C s1
next [F s1]
comps Int
preTag ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x1 [(Int, Action)]
np1 ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
x2 [(Int, Action)]
np2
              Ordering
answer -> Ordering -> ST s1 Ordering
forall a. a -> ST s1 a
forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
answer
         (Maybe Orbits, Maybe Orbits)
_ -> String -> ST s1 Ordering
forall a. String -> a
err (String -> ST s1 Ordering) -> String -> ST s1 Ordering
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines [ String
"challenge_Orb is too stupid to handle mismatched orbit data :"
                           , (Int, Int, [(Int, Action)], [(Int, Action)]) -> String
forall a. Show a => a -> String
show(Int
tag,Int
preTag,[(Int, Action)]
np1,[(Int, Action)]
np2)
                           , Maybe Orbits -> String
forall a. Show a => a -> String
show Maybe Orbits
s1
                           , Maybe Orbits -> String
forall a. Show a => a -> String
show Maybe Orbits
s2
                           ]
  challenge_Orb Int
_ [] Int
_ ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
_ [(Int, Action)]
_ ((Int, Instructions), STUArray s1 Int Int, OrbitLog)
_ [(Int, Action)]
_ = String -> ST s1 Ordering
forall a. String -> a
err String
"impossible 0298347"

comparePos :: (ViewL Position) -> (ViewL Position) -> Ordering
comparePos :: ViewL Int -> ViewL Int -> Ordering
comparePos ViewL Int
EmptyL ViewL Int
EmptyL = Ordering
EQ
comparePos ViewL Int
EmptyL ViewL Int
_      = Ordering
GT
comparePos ViewL Int
_      ViewL Int
EmptyL = Ordering
LT
comparePos (Int
p1 :< Seq Int
ps1) (Int
p2 :< Seq Int
ps2) =
  Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
p1 Int
p2 Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
`mappend` ViewL Int -> ViewL Int -> Ordering
comparePos (Seq Int -> ViewL Int
forall a. Seq a -> ViewL a
viewl Seq Int
ps1) (Seq Int -> ViewL Int
forall a. Seq a -> ViewL a
viewl Seq Int
ps2)

{- CONVERT WINNERS TO MATCHARRAY -}

tagsToGroupsST :: forall s. Array GroupIndex [GroupInfo] -> WScratch s -> S.ST s MatchArray
tagsToGroupsST :: forall s. Array Int [GroupInfo] -> WScratch s -> ST s MatchArray
tagsToGroupsST Array Int [GroupInfo]
aGroups (WScratch {w_pos :: forall s. WScratch s -> STUArray s Int Int
w_pos=STUArray s Int Int
pos})= do
  let b_max :: Int
b_max = (Int, Int) -> Int
forall a b. (a, b) -> b
snd (Array Int [GroupInfo] -> (Int, Int)
forall i. Ix i => Array i [GroupInfo] -> (i, i)
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> (i, i)
bounds (Array Int [GroupInfo]
aGroups))
  ma <- (Int, Int) -> (Int, Int) -> ST s (STArray s Int (Int, Int))
forall i.
Ix i =>
(i, i) -> (Int, Int) -> ST s (STArray s i (Int, Int))
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Int
0,Int
b_max) (-Int
1,Int
0) :: ST s (STArray s Int (MatchOffset,MatchLength))
  startPos0 <- pos !! 0
  stopPos0 <- pos !! 1
  set ma 0 (startPos0,stopPos0-startPos0)
  let act Int
_this_index [] = () -> ST s ()
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      act Int
this_index ((GroupInfo Int
_ Int
parent Int
start Int
stop Int
flagtag):[GroupInfo]
gs) = do
        flagVal <- STUArray s Int Int
pos STUArray s Int Int -> Int -> ST s Int
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
flagtag
        if (-1) == flagVal then act this_index gs
          else do
        startPos <- pos !! start
        stopPos <- pos !! stop
        (startParent,lengthParent) <- ma !! parent
        let ok = (Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
startParent Bool -> Bool -> Bool
&&
                  Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
lengthParent Bool -> Bool -> Bool
&&
                  Int
startParent Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
startPos Bool -> Bool -> Bool
&&
                  Int
stopPos Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
startPos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
lengthParent)
        if not ok then act this_index gs
          else set ma this_index (startPos,stopPos-startPos)
  forM_ (range (1,b_max)) $ (\Int
i -> Int -> [GroupInfo] -> ST s ()
forall {s}.
(MArray (STUArray s) Int (ST s),
 MArray (STArray s) (Int, Int) (ST s)) =>
Int -> [GroupInfo] -> ST s ()
act Int
i (Array Int [GroupInfo]
aGroupsArray Int [GroupInfo] -> Int -> [GroupInfo]
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
!Int
i))
  unsafeFreeze ma

{- MUTABLE TAGGED TRANSITION (returning Tag-0 value) -}

{-# INLINE spawnAt #-}
-- Reset the entry at "Index", or allocate such an entry.
-- set tag 0 to the "Position"
spawnAt :: (Tag,Tag) -> BlankScratch s -> Index -> MScratch s -> Position -> S.ST s ()
spawnAt :: forall s.
(Int, Int) -> BlankScratch s -> Int -> MScratch s -> Int -> ST s ()
spawnAt (Int, Int)
b_tags (BlankScratch STUArray s Int Int
blankPos) Int
i MScratch s
s1 Int
thisPos = do
  oldPos <- MScratch s -> STArray s Int (Maybe (STUArray s Int Int))
forall s. MScratch s -> STArray s Int (Maybe (STUArray s Int Int))
m_pos MScratch s
s1 STArray s Int (Maybe (STUArray s Int Int))
-> Int -> ST s (Maybe (STUArray s Int Int))
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Int -> ST s e
!! Int
i
  pos <- case oldPos of
           Maybe (STUArray s Int Int)
Nothing -> do
             pos' <- (Int, Int) -> ST s (STUArray s Int Int)
forall s e.
MArray (STUArray s) e (ST s) =>
(Int, Int) -> ST s (STUArray s Int e)
newA_ (Int, Int)
b_tags
             set (m_pos s1) i (Just pos')
             return pos'
           Just STUArray s Int Int
pos -> STUArray s Int Int -> ST s (STUArray s Int Int)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return STUArray s Int Int
pos
  copySTU blankPos pos
  set (m_orbit s1) i $! mempty
  set pos 0 thisPos

{-# INLINE updateCopy #-}
updateCopy :: ((Index, Instructions), STUArray s Tag Position, OrbitLog)
           -> Index
           -> MScratch s
           -> Int
           -> ST s ()
updateCopy :: forall s.
((Int, Instructions), STUArray s Int Int, OrbitLog)
-> Int -> MScratch s -> Int -> ST s ()
updateCopy ((Int
_i1,Instructions
instructions),STUArray s Int Int
oldPos,OrbitLog
newOrbit) Int
preTag MScratch s
s2 Int
i2 = do
  b_tags <- STUArray s Int Int -> ST s (Int, Int)
forall i. Ix i => STUArray s i Int -> ST s (i, i)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> m (i, i)
getBounds STUArray s Int Int
oldPos
  newerPos <- maybe (do
    a <- newA_ b_tags
    set (m_pos s2) i2 (Just a)
    return a) return =<< m_pos s2 !! i2
  copySTU oldPos newerPos
  doActions preTag newerPos (newPos instructions)
  set (m_orbit s2) i2 $! newOrbit

{- USING memcpy TO COPY STUARRAY DATA -}

-- #ifdef __GLASGOW_HASKELL__
foreign import ccall unsafe "memcpy"
    memcpyIO :: MutableByteArray# RealWorld -> MutableByteArray# RealWorld -> Int# -> IO (Ptr a)

memcpyST :: MutableByteArray# s -> MutableByteArray# s -> Int# -> State# s -> (# State# s, Ptr a #)
memcpyST :: forall s a.
MutableByteArray# s
-> MutableByteArray# s -> Int# -> State# s -> (# State# s, Ptr a #)
memcpyST = (MutableByteArray# RealWorld
 -> MutableByteArray# RealWorld -> Int# -> IO (Ptr (ZonkAny 0)))
-> MutableByteArray# s
-> MutableByteArray# s
-> Int#
-> State# s
-> (# State# s, Ptr a #)
forall a b. a -> b
unsafeCoerce# MutableByteArray# RealWorld
-> MutableByteArray# RealWorld -> Int# -> IO (Ptr (ZonkAny 0))
forall a.
MutableByteArray# RealWorld
-> MutableByteArray# RealWorld -> Int# -> IO (Ptr a)
memcpyIO

{-
Prelude Data.Array.Base> :i STUArray
data STUArray s i e
  = STUArray !i !i !Int (GHC.Prim.MutableByteArray# s)
  -- Defined in Data.Array.Base
-}
-- This has been updated for ghc 6.8.3 and still works with ghc 6.10.1
{-# INLINE copySTU #-}
copySTU :: (Show i,Ix i,MArray (STUArray s) e (S.ST s)) => STUArray s i e -> STUArray s i e -> S.ST s () -- (STUArray s i e)
copySTU :: forall i s e.
(Show i, Ix i, MArray (STUArray s) e (ST s)) =>
STUArray s i e -> STUArray s i e -> ST s ()
copySTU _source :: STUArray s i e
_source@(STUArray i
_ i
_ Int
_ MutableByteArray# s
msource) _destination :: STUArray s i e
_destination@(STUArray i
_ i
_ Int
_ MutableByteArray# s
mdest) =
-- do b1 <- getBounds s1
--  b2 <- getBounds s2
--  when (b1/=b2) (error ("\n\nWTF copySTU: "++show (b1,b2)))
  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 -> State# s -> (# State# s, Int# #)
forall d. MutableByteArray# d -> State# d -> (# State# d, Int# #)
getSizeofMutableByteArray# MutableByteArray# s
msource State# s
s1# of { (# State# s
s1'#, Int#
n# #) ->
    case MutableByteArray# s
-> MutableByteArray# s
-> Int#
-> State# s
-> (# State# s, Ptr (ZonkAny 1) #)
forall s a.
MutableByteArray# s
-> MutableByteArray# s -> Int# -> State# s -> (# State# s, Ptr a #)
memcpyST MutableByteArray# s
mdest MutableByteArray# s
msource Int#
n# State# s
s1'# of { (# State# s
s2#, Ptr (ZonkAny 1)
_ #) ->
    (# State# s
s2#, () #) }}
{-
-- #else /* !__GLASGOW_HASKELL__ */

copySTU :: (MArray (STUArray s) e (S.ST s))=> STUArray s Tag e -> STUArray s Tag e -> S.ST s (STUArray s i e)
copySTU source destination = do
  b@(start,stop) <- getBounds source
  b' <- getBounds destination
  -- traceCopy ("> copySTArray "++show b) $ do
  when (b/=b') (fail $ "Text.Regex.TDFA.RunMutState copySTUArray bounds mismatch"++show (b,b'))
  forM_ (range b) $ \index ->
    set destination index =<< source !! index
  return destination
-- #endif /* !__GLASGOW_HASKELL__ */
-}