{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 902
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
#endif
module Text.Regex.TDFA.NewDFA.Engine(execMatch) where
import Control.Monad(when,forM,forM_,liftM2,foldM,join,filterM)
import Data.Array.Base(unsafeRead,unsafeWrite,STUArray(..))
import GHC.Arr(STArray(..))
import GHC.ST(ST(..))
import GHC.Exts(MutableByteArray#,RealWorld,Int#,sizeofMutableByteArray#,State#)
import Unsafe.Coerce (unsafeCoerce)
import Prelude hiding ((!!))
import Data.Array.MArray(MArray(..))
import Data.Array.Unsafe(unsafeFreeze)
import Data.Array.IArray(Array,bounds,assocs,Ix(rangeSize,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 qualified Data.IntSet as ISet(toAscList)
import Data.Array.IArray((!))
import Data.List(partition,sort,foldl',sortBy,groupBy)
import Data.STRef(STRef,newSTRef,readSTRef,writeSTRef)
import qualified Control.Monad.ST.Lazy as L(ST,runST,strictToLazyST)
import qualified Control.Monad.ST.Strict as S(ST)
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 qualified Text.Regex.TDFA.IntArrTrieSet as Trie(lookupAsc)
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 qualified Text.Regex.TDFA.NewDFA.Engine_FA as FA(execMatch)
import qualified Text.Regex.TDFA.NewDFA.Engine_NC as NC(execMatch)
import qualified Text.Regex.TDFA.NewDFA.Engine_NC_FA as NC_FA(execMatch)
err :: String -> a
err :: forall a. [Char] -> a
err [Char]
s = forall a. [Char] -> [Char] -> a
common_error [Char]
"Text.Regex.TDFA.NewDFA.Engine" [Char]
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 -> Position -> ST s e
(!!) = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Position -> 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 -> Position -> e -> ST s ()
set = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Position -> e -> m ()
unsafeWrite
{-# 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 :: Uncons text => Regex -> Position -> Char -> text -> [MatchArray]
execMatch :: forall text.
Uncons text =>
Regex -> Position -> Char -> text -> [MatchArray]
execMatch r :: Regex
r@(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 -> Position
regex_init = Position
startState
, regex_b_index :: Regex -> (Position, Position)
regex_b_index = (Position, Position)
b_index
, regex_b_tags :: Regex -> (Position, Position)
regex_b_tags = (Position, Position)
b_tags_all
, regex_trie :: Regex -> TrieSet DFA
regex_trie = TrieSet DFA
trie
, regex_tags :: Regex -> Array Position OP
regex_tags = Array Position OP
aTags
, regex_groups :: Regex -> Array Position [GroupInfo]
regex_groups = Array Position [GroupInfo]
aGroups
, regex_isFrontAnchored :: Regex -> Bool
regex_isFrontAnchored = Bool
frontAnchored
, regex_compOptions :: Regex -> CompOption
regex_compOptions = CompOption { multiline :: CompOption -> Bool
multiline = Bool
newline }
, regex_execOptions :: Regex -> ExecOption
regex_execOptions = ExecOption { captureGroups :: ExecOption -> Bool
captureGroups = Bool
capture }})
Position
offsetIn Char
prevIn text
inputIn = case (Bool
subCapture,Bool
frontAnchored) of
(Bool
True ,Bool
False) -> forall a. (forall s. ST s a) -> a
L.runST forall s. ST s [MatchArray]
runCaptureGroup
(Bool
True ,Bool
True) -> forall text.
Uncons text =>
Regex -> Position -> Char -> text -> [MatchArray]
FA.execMatch Regex
r Position
offsetIn Char
prevIn text
inputIn
(Bool
False ,Bool
False) -> forall text.
Uncons text =>
Regex -> Position -> Char -> text -> [MatchArray]
NC.execMatch Regex
r Position
offsetIn Char
prevIn text
inputIn
(Bool
False ,Bool
True) -> forall text.
Uncons text =>
Regex -> Position -> Char -> text -> [MatchArray]
NC_FA.execMatch Regex
r Position
offsetIn Char
prevIn text
inputIn
where
subCapture :: Bool
subCapture :: Bool
subCapture = Bool
capture Bool -> Bool -> Bool
&& (Position
1forall a. Ord a => a -> a -> Bool
<=forall a. Ix a => (a, a) -> Position
rangeSize (forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> (i, i)
bounds Array Position [GroupInfo]
aGroups))
b_tags :: (Tag,Tag)
!b_tags :: (Position, Position)
b_tags = (Position, Position)
b_tags_all
orbitTags :: [Tag]
!orbitTags :: [Position]
orbitTags = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter ((OP
Orbitforall a. Eq a => a -> a -> Bool
==)forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> [(i, e)]
assocs forall a b. (a -> b) -> a -> b
$ Array Position OP
aTags
!test :: WhichTest -> Position -> Char -> text -> Bool
test = forall text.
Uncons text =>
Bool -> WhichTest -> Position -> Char -> text -> Bool
mkTest Bool
newline
comp :: C s
comp :: forall s. C s
comp = {-# SCC "matchHere.comp" #-} forall s. Array Position OP -> C s
ditzyComp'3 Array Position OP
aTags
runCaptureGroup :: L.ST s [MatchArray]
runCaptureGroup :: forall s. ST s [MatchArray]
runCaptureGroup = {-# SCC "runCaptureGroup" #-} do
ST s [MatchArray]
obtainNext <- forall s a. ST s a -> ST s a
L.strictToLazyST forall s. ST s (ST s [MatchArray])
constructNewEngine
let loop :: ST s [MatchArray]
loop = do [MatchArray]
vals <- forall s a. ST s a -> ST s a
L.strictToLazyST ST s [MatchArray]
obtainNext
if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [MatchArray]
vals
then forall (m :: * -> *) a. Monad m => a -> m a
return []
else do [MatchArray]
valsRest <- ST s [MatchArray]
loop
forall (m :: * -> *) a. Monad m => a -> m a
return ([MatchArray]
vals forall a. [a] -> [a] -> [a]
++ [MatchArray]
valsRest)
ST s [MatchArray]
loop
constructNewEngine :: S.ST s (S.ST s [MatchArray])
constructNewEngine :: forall s. ST s (ST s [MatchArray])
constructNewEngine = {-# SCC "constructNewEngine" #-} do
STRef s (ST s [MatchArray])
storeNext <- forall a s. a -> ST s (STRef s a)
newSTRef forall a. HasCallStack => a
undefined
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s (ST s [MatchArray])
storeNext (forall s. STRef s (ST s [MatchArray]) -> ST s [MatchArray]
goNext STRef s (ST s [MatchArray])
storeNext)
let obtainNext :: ST s [MatchArray]
obtainNext = forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (forall s a. STRef s a -> ST s a
readSTRef STRef s (ST s [MatchArray])
storeNext)
forall (m :: * -> *) a. Monad m => a -> m a
return ST s [MatchArray]
obtainNext
goNext :: STRef s (ST s [MatchArray]) -> ST s [MatchArray]
goNext :: forall s. STRef s (ST s [MatchArray]) -> ST s [MatchArray]
goNext STRef s (ST s [MatchArray])
storeNext = {-# SCC "goNext" #-} do
(SScratch MScratch s
s1In MScratch s
s2In (MQ s
winQ,BlankScratch s
blank,STArray
s
Position
((Position, Instructions), STUArray s Position Position, OrbitLog)
which)) <- forall s.
(Position, Position) -> (Position, Position) -> ST s (SScratch s)
newScratch (Position, Position)
b_index (Position, Position)
b_tags
Position
_ <- forall s.
(Position, Position)
-> BlankScratch s
-> Position
-> MScratch s
-> Position
-> ST s Position
spawnStart (Position, Position)
b_tags BlankScratch s
blank Position
startState MScratch s
s1In Position
offsetIn
STRef s Bool
eliminatedStateFlag <- forall a s. a -> ST s (STRef s a)
newSTRef Bool
False
STRef s Bool
eliminatedRespawnFlag <- forall a s. a -> ST s (STRef s a)
newSTRef Bool
False
let next :: MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s1 MScratch s
s2 SetIndex
did DT
dt Position
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 -> Position -> Char -> text -> Bool
test WhichTest
wt Position
offset Char
prev text
input
then MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s1 MScratch s
s2 SetIndex
did DT
a Position
offset Char
prev text
input
else MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s1 MScratch s
s2 SetIndex
did DT
b Position
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}
| forall a. IntMap a -> Bool
IMap.null IntMap Instructions
w ->
case forall a. Uncons a => a -> Maybe (Char, a)
uncons text
input of
Maybe (Char, text)
Nothing -> ST s [MatchArray]
finalizeWinners
Just (Char
c,text
input') ->
case forall a. a -> Char -> CharMap a -> a
CMap.findWithDefault Transition
o Char
c CharMap Transition
t of
Transition {trans_many :: Transition -> DFA
trans_many=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} ->
MScratch s
-> MScratch s
-> SetIndex
-> SetIndex
-> DT
-> DTrans
-> Position
-> Char
-> text
-> ST s [MatchArray]
findTrans MScratch s
s1 MScratch s
s2 SetIndex
did SetIndex
did' DT
dt' DTrans
dtrans Position
offset Char
c text
input'
| Bool
otherwise -> do
(SetIndex
did',DT
dt') <- MScratch s
-> SetIndex
-> DT
-> IntMap Instructions
-> Position
-> ST s (SetIndex, DT)
processWinner MScratch s
s1 SetIndex
did DT
dt IntMap Instructions
w Position
offset
MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next' MScratch s
s1 MScratch s
s2 SetIndex
did' DT
dt' Position
offset Char
prev text
input
next' :: MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next' MScratch s
s1 MScratch s
s2 SetIndex
did DT
dt Position
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 -> Position -> Char -> text -> Bool
test WhichTest
wt Position
offset Char
prev text
input
then MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next' MScratch s
s1 MScratch s
s2 SetIndex
did DT
a Position
offset Char
prev text
input
else MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next' MScratch s
s1 MScratch s
s2 SetIndex
did DT
b Position
offset Char
prev text
input
Simple' {dt_trans :: DT -> CharMap Transition
dt_trans=CharMap Transition
t, dt_other :: DT -> Transition
dt_other=Transition
o} ->
case forall a. Uncons a => a -> Maybe (Char, a)
uncons text
input of
Maybe (Char, text)
Nothing -> ST s [MatchArray]
finalizeWinners
Just (Char
c,text
input') ->
case forall a. a -> Char -> CharMap a -> a
CMap.findWithDefault Transition
o Char
c CharMap Transition
t of
Transition {trans_many :: Transition -> DFA
trans_many=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} ->
MScratch s
-> MScratch s
-> SetIndex
-> SetIndex
-> DT
-> DTrans
-> Position
-> Char
-> text
-> ST s [MatchArray]
findTrans MScratch s
s1 MScratch s
s2 SetIndex
did SetIndex
did' DT
dt' DTrans
dtrans Position
offset Char
c text
input'
compressOrbits :: MScratch s -> SetIndex -> Position -> ST s ()
compressOrbits MScratch s
s1 SetIndex
did Position
offset = do
let getStart :: Position -> ST s (Position, Position)
getStart Position
state = do Position
start <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. [Char] -> a
err [Char]
"compressOrbit,1") (forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
0) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
state
forall (m :: * -> *) a. Monad m => a -> m a
return (Position
state,Position
start)
cutoff :: Position
cutoff = Position
offset forall a. Num a => a -> a -> a
- Position
50
[(Position, Position)]
ss <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {s}.
(MArray (STUArray s) Position (ST s),
MArray
(STArray s) (Maybe (STUArray s Position Position)) (ST s)) =>
Position -> ST s (Position, Position)
getStart (SetIndex -> [Position]
ISet.toAscList SetIndex
did)
let compressOrbit :: Position -> ST s ()
compressOrbit Position
tag = do
[Maybe ((Position, Position), Orbits)]
mos <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(Position, Position)]
ss ( \ p :: (Position, Position)
p@(Position
state,Position
_start) -> do
Maybe Orbits
mo <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Position -> IntMap a -> Maybe a
IMap.lookup Position
tag) (forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
state)
case Maybe Orbits
mo of
Just Orbits
orbits | Orbits -> Position
basePos Orbits
orbits forall a. Ord a => a -> a -> Bool
< Position
cutoff -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just ((Position, Position)
p,Orbits
orbits))
| Bool
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Maybe Orbits
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing )
let compressGroup :: [((Position, b), Orbits)] -> ST s ()
compressGroup [((Position
state,b
_),Orbits
orbit)] | forall a. Seq a -> Bool
Seq.null (Orbits -> Seq Position
getOrbits Orbits
orbit) = forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise =
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1) Position
state
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Position -> a -> IntMap a -> IntMap a
IMap.insert Position
tag forall a b. (a -> b) -> a -> b
$! (Orbits
orbit { ordinal :: Maybe Position
ordinal = forall a. Maybe a
Nothing, getOrbits :: Seq Position
getOrbits = forall a. Monoid a => a
mempty}))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
state
compressGroup [((Position, b), Orbits)]
gs = do
let sortPos :: (a, Orbits) -> (a, Orbits) -> Ordering
sortPos (a
_,Orbits
b1) (a
_,Orbits
b2) = forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Maybe Position
ordinal Orbits
b1) (Orbits -> Maybe Position
ordinal Orbits
b2) forall a. Monoid a => a -> a -> a
`mappend`
forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Bool
inOrbit Orbits
b2) (Orbits -> Bool
inOrbit Orbits
b1) forall a. Monoid a => a -> a -> a
`mappend`
ViewL Position -> ViewL Position -> Ordering
comparePos (forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Position
getOrbits Orbits
b1)) (forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Position
getOrbits Orbits
b2))
groupPos :: (a, Orbits) -> (a, Orbits) -> Bool
groupPos (a
_,Orbits
b1) (a
_,Orbits
b2) = Orbits -> Maybe Position
ordinal Orbits
b1 forall a. Eq a => a -> a -> Bool
== Orbits -> Maybe Position
ordinal Orbits
b2 Bool -> Bool -> Bool
&& Orbits -> Seq Position
getOrbits Orbits
b1 forall a. Eq a => a -> a -> Bool
== Orbits -> Seq Position
getOrbits Orbits
b2
gs' :: [(Position, [((Position, b), Orbits)])]
gs' = forall a b. [a] -> [b] -> [(a, b)]
zip [(Position
1::Int)..] (forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy forall {a} {a}. (a, Orbits) -> (a, Orbits) -> Bool
groupPos forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy forall {a} {a}. (a, Orbits) -> (a, Orbits) -> Ordering
sortPos forall a b. (a -> b) -> a -> b
$ [((Position, b), Orbits)]
gs)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Position, [((Position, b), Orbits)])]
gs' forall a b. (a -> b) -> a -> b
$ \ (!Position
n,[((Position, b), Orbits)]
eqs) -> do
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [((Position, b), Orbits)]
eqs forall a b. (a -> b) -> a -> b
$ \ ((Position
state,b
_),Orbits
orbit) ->
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1) Position
state
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Position -> a -> IntMap a -> IntMap a
IMap.insert Position
tag forall a b. (a -> b) -> a -> b
$! (Orbits
orbit { ordinal :: Maybe Position
ordinal = forall a. a -> Maybe a
Just Position
n, getOrbits :: Seq Position
getOrbits = forall a. Monoid a => a
mempty }))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
state
let sorter :: ((a, a), Orbits) -> ((a, a), Orbits) -> Ordering
sorter ((a
_,a
a1),Orbits
b1) ((a
_,a
a2),Orbits
b2) = forall a. Ord a => a -> a -> Ordering
compare a
a1 a
a2 forall a. Monoid a => a -> a -> a
`mappend` forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Position
basePos Orbits
b1) (Orbits -> Position
basePos Orbits
b2)
grouper :: ((a, a), Orbits) -> ((a, a), Orbits) -> Bool
grouper ((a
_,a
a1),Orbits
b1) ((a
_,a
a2),Orbits
b2) = a
a1forall a. Eq a => a -> a -> Bool
==a
a2 Bool -> Bool -> Bool
&& Orbits -> Position
basePos Orbits
b1 forall a. Eq a => a -> a -> Bool
== Orbits -> Position
basePos Orbits
b2
orbitGroups :: [[((Position, Position), Orbits)]]
orbitGroups = forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy forall {a} {a} {a}.
Eq a =>
((a, a), Orbits) -> ((a, a), Orbits) -> Bool
grouper forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy forall {a} {a} {a}.
Ord a =>
((a, a), Orbits) -> ((a, a), Orbits) -> Ordering
sorter forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [Maybe a] -> [a]
catMaybes forall a b. (a -> b) -> a -> b
$ [Maybe ((Position, Position), Orbits)]
mos
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {s} {b}.
MArray (STArray s) OrbitLog (ST s) =>
[((Position, b), Orbits)] -> ST s ()
compressGroup [[((Position, Position), Orbits)]]
orbitGroups
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {s}.
MArray (STArray s) OrbitLog (ST s) =>
Position -> ST s ()
compressOrbit [Position]
orbitTags
findTrans :: MScratch s
-> MScratch s
-> SetIndex
-> SetIndex
-> DT
-> DTrans
-> Position
-> Char
-> text
-> ST s [MatchArray]
findTrans MScratch s
s1 MScratch s
s2 SetIndex
did SetIndex
did' DT
dt' DTrans
dtrans Position
offset Char
prev' text
input' = {-# SCC "goNext.findTrans" #-} do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Position]
orbitTags) Bool -> Bool -> Bool
&& (Position
offset forall a. Integral a => a -> a -> a
`rem` Position
100 forall a. Eq a => a -> a -> Bool
== Position
99)) (forall {s} {s}.
(MArray (STUArray s) Position (ST s),
MArray (STArray s) (Maybe (STUArray s Position Position)) (ST s),
MArray (STArray s) OrbitLog (ST s)) =>
MScratch s -> SetIndex -> Position -> ST s ()
compressOrbits MScratch s
s1 SetIndex
did Position
offset)
let findTransTo :: (Position, IntMap (a, Instructions)) -> ST s ()
findTransTo (Position
destIndex,IntMap (a, Instructions)
sources) | forall a. IntMap a -> Bool
IMap.null IntMap (a, Instructions)
sources =
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STArray
s
Position
((Position, Instructions), STUArray s Position Position, OrbitLog)
which Position
destIndex ((-Position
1,Instructions { newPos :: [(Position, Action)]
newPos = [(Position
0,Action
SetPost)], newOrbits :: Maybe (Position -> OrbitLog -> OrbitLog)
newOrbits = forall a. Maybe a
Nothing })
,forall s. BlankScratch s -> STUArray s Position Position
blank_pos BlankScratch s
blank,forall a. Monoid a => a
mempty)
| Bool
otherwise = do
let prep :: (Position, (a, Instructions))
-> ST
s
((Position, Instructions), STUArray s Position Position, OrbitLog)
prep (Position
sourceIndex,(a
_dopa,Instructions
instructions)) = {-# SCC "goNext.findTrans.prep" #-} do
STUArray s Position Position
pos <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. [Char] -> a
err forall a b. (a -> b) -> a -> b
$ [Char]
"findTrans,1 : "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> [Char]
show (Position
sourceIndex,Position
destIndex,SetIndex
did')) forall (m :: * -> *) a. Monad m => a -> m a
return
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
sourceIndex
OrbitLog
orbit <- forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
sourceIndex
let orbit' :: OrbitLog
orbit' = forall b a. b -> (a -> b) -> Maybe a -> b
maybe OrbitLog
orbit (\ Position -> OrbitLog -> OrbitLog
f -> Position -> OrbitLog -> OrbitLog
f Position
offset OrbitLog
orbit) (Instructions -> Maybe (Position -> OrbitLog -> OrbitLog)
newOrbits Instructions
instructions)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Position
sourceIndex,Instructions
instructions),STUArray s Position Position
pos,OrbitLog
orbit')
challenge :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> ST
s
((Position, Instructions), STUArray s Position Position, OrbitLog)
challenge x1 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1@((Position
_si1,Instructions
ins1),STUArray s Position Position
_p1,OrbitLog
_o1) x2 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2@((Position
_si2,Instructions
ins2),STUArray s Position Position
_p2,OrbitLog
_o2) = {-# SCC "goNext.findTrans.challenge" #-} do
Ordering
check <- forall s. C s
comp Position
offset ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 (Instructions -> [(Position, Action)]
newPos Instructions
ins1) ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 (Instructions -> [(Position, Action)]
newPos Instructions
ins2)
if Ordering
checkforall a. Eq a => a -> a -> Bool
==Ordering
LT then forall (m :: * -> *) a. Monad m => a -> m a
return ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 else forall (m :: * -> *) a. Monad m => a -> m a
return ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1
[((Position, Instructions), STUArray s Position Position,
OrbitLog)]
first_rest <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {s} {a}.
(MArray (STArray s) (Maybe (STUArray s Position Position)) (ST s),
MArray (STArray s) OrbitLog (ST s)) =>
(Position, (a, Instructions))
-> ST
s
((Position, Instructions), STUArray s Position Position, OrbitLog)
prep (forall a. IntMap a -> [(Position, a)]
IMap.toList IntMap (a, Instructions)
sources)
let ((Position, Instructions), STUArray s Position Position, OrbitLog)
first:[((Position, Instructions), STUArray s Position Position,
OrbitLog)]
rest = [((Position, Instructions), STUArray s Position Position,
OrbitLog)]
first_rest
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STArray
s
Position
((Position, Instructions), STUArray s Position Position, OrbitLog)
which Position
destIndex forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM forall {s}.
((Position, Instructions), STUArray s Position Position, OrbitLog)
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> ST
s
((Position, Instructions), STUArray s Position Position, OrbitLog)
challenge ((Position, Instructions), STUArray s Position Position, OrbitLog)
first [((Position, Instructions), STUArray s Position Position,
OrbitLog)]
rest
let dl :: [(Position, IntMap (DoPa, Instructions))]
dl = forall a. IntMap a -> [(Position, a)]
IMap.toList DTrans
dtrans
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {a}. (Position, IntMap (a, Instructions)) -> ST s ()
findTransTo [(Position, IntMap (DoPa, Instructions))]
dl
let performTransTo :: (Position, b) -> ST s Position
performTransTo (Position
destIndex,b
_) = {-# SCC "goNext.findTrans.performTransTo" #-} do
x :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x@((Position
sourceIndex,Instructions
_instructions),STUArray s Position Position
_pos,OrbitLog
_orbit') <- STArray
s
Position
((Position, Instructions), STUArray s Position Position, OrbitLog)
which forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
destIndex
if Position
sourceIndex forall a. Eq a => a -> a -> Bool
== (-Position
1)
then forall s.
(Position, Position)
-> BlankScratch s
-> Position
-> MScratch s
-> Position
-> ST s Position
spawnStart (Position, Position)
b_tags BlankScratch s
blank Position
destIndex MScratch s
s2 (forall a. Enum a => a -> a
succ Position
offset)
else forall s.
((Position, Instructions), STUArray s Position Position, OrbitLog)
-> Position -> MScratch s -> Position -> ST s Position
updateCopy ((Position, Instructions), STUArray s Position Position, OrbitLog)
x Position
offset MScratch s
s2 Position
destIndex
Position
earlyStart <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {b}. (Position, b) -> ST s Position
performTransTo [(Position, IntMap (DoPa, Instructions))]
dl
Position
earlyWin <- forall s a. STRef s a -> ST s a
readSTRef (forall s. MQ s -> STRef s Position
mq_earliest MQ s
winQ)
if Position
earlyWin forall a. Ord a => a -> a -> Bool
< Position
earlyStart
then do
[WScratch s]
winners <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\ [WScratch s]
rest WScratch s
ws -> WScratch s
ws forall a. a -> [a] -> [a]
: [WScratch s]
rest) []) forall a b. (a -> b) -> a -> b
$
forall s. Position -> MQ s -> ST s [WScratch s]
getMQ Position
earlyStart MQ s
winQ
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s (ST s [MatchArray])
storeNext (MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s2 MScratch s
s1 SetIndex
did' DT
dt' (forall a. Enum a => a -> a
succ Position
offset) Char
prev' text
input')
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall s.
Array Position [GroupInfo] -> WScratch s -> ST s MatchArray
tagsToGroupsST Array Position [GroupInfo]
aGroups) [WScratch s]
winners
else do
let offset' :: Position
offset' = forall a. Enum a => a -> a
succ Position
offset in seq :: forall a b. a -> b -> b
seq Position
offset' forall a b. (a -> b) -> a -> b
$ MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s2 MScratch s
s1 SetIndex
did' DT
dt' Position
offset' Char
prev' text
input'
{-# INLINE processWinner #-}
processWinner :: MScratch s
-> SetIndex
-> DT
-> IntMap Instructions
-> Position
-> ST s (SetIndex, DT)
processWinner MScratch s
s1 SetIndex
did DT
dt IntMap Instructions
w Position
offset = {-# SCC "goNext.newWinnerThenProceed" #-} do
let prep :: (Position, Instructions)
-> ST
s
(Position,
((Position, Instructions), STUArray s Position Position, OrbitLog))
prep x :: (Position, Instructions)
x@(Position
sourceIndex,Instructions
instructions) = {-# SCC "goNext.newWinnerThenProceed.prep" #-} do
STUArray s Position Position
pos <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. [Char] -> a
err [Char]
"newWinnerThenProceed,1") forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
sourceIndex
Position
startPos <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
0
OrbitLog
orbit <- forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
sourceIndex
let orbit' :: OrbitLog
orbit' = forall b a. b -> (a -> b) -> Maybe a -> b
maybe OrbitLog
orbit (\ Position -> OrbitLog -> OrbitLog
f -> Position -> OrbitLog -> OrbitLog
f Position
offset OrbitLog
orbit) (Instructions -> Maybe (Position -> OrbitLog -> OrbitLog)
newOrbits Instructions
instructions)
forall (m :: * -> *) a. Monad m => a -> m a
return (Position
startPos,((Position, Instructions)
x,STUArray s Position Position
pos,OrbitLog
orbit'))
challenge :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> ST
s
((Position, Instructions), STUArray s Position Position, OrbitLog)
challenge x1 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1@((Position
_si1,Instructions
ins1),STUArray s Position Position
_p1,OrbitLog
_o1) x2 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2@((Position
_si2,Instructions
ins2),STUArray s Position Position
_p2,OrbitLog
_o2) = {-# SCC "goNext.newWinnerThenProceed.challenge" #-} do
Ordering
check <- forall s. C s
comp Position
offset ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 (Instructions -> [(Position, Action)]
newPos Instructions
ins1) ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 (Instructions -> [(Position, Action)]
newPos Instructions
ins2)
if Ordering
checkforall a. Eq a => a -> a -> Bool
==Ordering
LT then forall (m :: * -> *) a. Monad m => a -> m a
return ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 else forall (m :: * -> *) a. Monad m => a -> m a
return ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1
[(Position,
((Position, Instructions), STUArray s Position Position,
OrbitLog))]
prep'd <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {s}.
(MArray (STUArray s) Position (ST s),
MArray (STArray s) (Maybe (STUArray s Position Position)) (ST s),
MArray (STArray s) OrbitLog (ST s)) =>
(Position, Instructions)
-> ST
s
(Position,
((Position, Instructions), STUArray s Position Position, OrbitLog))
prep (forall a. IntMap a -> [(Position, a)]
IMap.toList IntMap Instructions
w)
let ([(Position,
((Position, Instructions), STUArray s Position Position,
OrbitLog))]
emptyFalse,[(Position,
((Position, Instructions), STUArray s Position Position,
OrbitLog))]
emptyTrue) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((Position
offset forall a. Ord a => a -> a -> Bool
>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(Position,
((Position, Instructions), STUArray s Position Position,
OrbitLog))]
prep'd
Maybe [Position]
mayID <- {-# SCC "goNext.newWinnerThenProceed.mayID" #-}
case forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd [(Position,
((Position, Instructions), STUArray s Position Position,
OrbitLog))]
emptyFalse of
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
(((Position, Instructions), STUArray s Position Position, OrbitLog)
first:[((Position, Instructions), STUArray s Position Position,
OrbitLog)]
rest) -> do
best :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
best@((Position
_sourceIndex,Instructions
_instructions),STUArray s Position Position
bp,OrbitLog
_orbit') <- forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM forall {s}.
((Position, Instructions), STUArray s Position Position, OrbitLog)
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> ST
s
((Position, Instructions), STUArray s Position Position, OrbitLog)
challenge ((Position, Instructions), STUArray s Position Position, OrbitLog)
first [((Position, Instructions), STUArray s Position Position,
OrbitLog)]
rest
forall {a} {c}.
Position
-> ((a, Instructions), STUArray s Position Position, c) -> ST s ()
newWinner Position
offset ((Position, Instructions), STUArray s Position Position, OrbitLog)
best
Position
startWin <- STUArray s Position Position
bp forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
0
let states :: [Position]
states = SetIndex -> [Position]
ISet.toAscList SetIndex
did
keepState :: Position -> ST s Bool
keepState Position
i1 = do
STUArray s Position Position
pos <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. [Char] -> a
err [Char]
"newWinnerThenProceed,2") forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
i1
Position
startsAt <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
0
let keep :: Bool
keep = (Position
startsAt forall a. Ord a => a -> a -> Bool
<= Position
startWin) Bool -> Bool -> Bool
|| (Position
offset forall a. Ord a => a -> a -> Bool
<= Position
startsAt)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
keep) forall a b. (a -> b) -> a -> b
$ do
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s Bool
eliminatedStateFlag Bool
True
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Position
i1 forall a. Eq a => a -> a -> Bool
== Position
startState) (forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s Bool
eliminatedRespawnFlag Bool
True)
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
keep
[Position]
states' <- forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM Position -> ST s Bool
keepState [Position]
states
Bool
changed <- forall s a. STRef s a -> ST s a
readSTRef STRef s Bool
eliminatedStateFlag
if Bool
changed then forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just [Position]
states') else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
case [(Position,
((Position, Instructions), STUArray s Position Position,
OrbitLog))]
emptyTrue of
[] -> case forall a. Position -> IntMap a -> Maybe a
IMap.lookup Position
startState IntMap Instructions
w of
Maybe Instructions
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Instructions
ins -> Position -> Instructions -> ST s ()
winEmpty Position
offset Instructions
ins
[(Position,
((Position, Instructions), STUArray s Position Position, OrbitLog))
first] -> forall {a} {c}.
Position
-> ((a, Instructions), STUArray s Position Position, c) -> ST s ()
newWinner Position
offset (forall a b. (a, b) -> b
snd (Position,
((Position, Instructions), STUArray s Position Position, OrbitLog))
first)
[(Position,
((Position, Instructions), STUArray s Position Position,
OrbitLog))]
_ -> forall a. [Char] -> a
err [Char]
"newWinnerThenProceed,3 : too many emptyTrue values"
case Maybe [Position]
mayID of
Maybe [Position]
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return (SetIndex
did,DT
dt)
Just [Position]
states' -> do
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s Bool
eliminatedStateFlag Bool
False
Bool
respawn <- forall s a. STRef s a -> ST s a
readSTRef STRef s Bool
eliminatedRespawnFlag
DFA {d_id :: DFA -> SetIndex
d_id=SetIndex
did',d_dt :: DFA -> DT
d_dt=DT
dt'} <-
if Bool
respawn
then do
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s Bool
eliminatedRespawnFlag Bool
False
Position
_ <- forall s.
(Position, Position)
-> BlankScratch s
-> Position
-> MScratch s
-> Position
-> ST s Position
spawnStart (Position, Position)
b_tags BlankScratch s
blank Position
startState MScratch s
s1 (forall a. Enum a => a -> a
succ Position
offset)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall v. TrieSet v -> [Position] -> v
Trie.lookupAsc TrieSet DFA
trie (forall a. Ord a => [a] -> [a]
sort ([Position]
states'forall a. [a] -> [a] -> [a]
++[Position
startState])))
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall v. TrieSet v -> [Position] -> v
Trie.lookupAsc TrieSet DFA
trie [Position]
states')
forall (m :: * -> *) a. Monad m => a -> m a
return (SetIndex
did',DT
dt')
winEmpty :: Position -> Instructions -> ST s ()
winEmpty Position
preTag Instructions
winInstructions = {-# SCC "goNext.winEmpty" #-} do
STUArray s Position Position
newerPos <- forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> ST s (STUArray s Position e)
newA_ (Position, Position)
b_tags
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 (forall s. BlankScratch s -> STUArray s Position Position
blank_pos BlankScratch s
blank) STUArray s Position Position
newerPos
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STUArray s Position Position
newerPos Position
0 Position
preTag
forall s.
Position
-> STUArray s Position Position -> [(Position, Action)] -> ST s ()
doActions Position
preTag STUArray s Position Position
newerPos (Instructions -> [(Position, Action)]
newPos Instructions
winInstructions)
forall s. WScratch s -> MQ s -> ST s ()
putMQ (forall s. STUArray s Position Position -> WScratch s
WScratch STUArray s Position Position
newerPos) MQ s
winQ
newWinner :: Position
-> ((a, Instructions), STUArray s Position Position, c) -> ST s ()
newWinner Position
preTag ((a
_sourceIndex,Instructions
winInstructions),STUArray s Position Position
oldPos,c
_newOrbit) = {-# SCC "goNext.newWinner" #-} do
STUArray s Position Position
newerPos <- forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> ST s (STUArray s Position e)
newA_ (Position, Position)
b_tags
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 STUArray s Position Position
oldPos STUArray s Position Position
newerPos
forall s.
Position
-> STUArray s Position Position -> [(Position, Action)] -> ST s ()
doActions Position
preTag STUArray s Position Position
newerPos (Instructions -> [(Position, Action)]
newPos Instructions
winInstructions)
forall s. WScratch s -> MQ s -> ST s ()
putMQ (forall s. STUArray s Position Position -> WScratch s
WScratch STUArray s Position Position
newerPos) MQ s
winQ
finalizeWinners :: ST s [MatchArray]
finalizeWinners = do
[WScratch s]
winners <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\ [WScratch s]
rest MQA s
mqa -> forall s. MQA s -> WScratch s
mqa_ws MQA s
mqa forall a. a -> [a] -> [a]
: [WScratch s]
rest) []) forall a b. (a -> b) -> a -> b
$
forall s a. STRef s a -> ST s a
readSTRef (forall s. MQ s -> STRef s [MQA s]
mq_list MQ s
winQ)
forall s. MQ s -> ST s ()
resetMQ MQ s
winQ
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s (ST s [MatchArray])
storeNext (forall (m :: * -> *) a. Monad m => a -> m a
return [])
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall s.
Array Position [GroupInfo] -> WScratch s -> ST s MatchArray
tagsToGroupsST Array Position [GroupInfo]
aGroups) [WScratch s]
winners
MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s1In MScratch s
s2In SetIndex
didIn DT
dtIn Position
offsetIn Char
prevIn text
inputIn
{-# INLINE doActions #-}
doActions :: Position -> STUArray s Tag Position -> [(Tag, Action)] -> ST s ()
doActions :: forall s.
Position
-> STUArray s Position Position -> [(Position, Action)] -> ST s ()
doActions Position
preTag STUArray s Position Position
pos [(Position, Action)]
ins = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {s}.
MArray (STUArray s) Position (ST s) =>
(Position, Action) -> ST s ()
doAction [(Position, Action)]
ins where
postTag :: Position
postTag = forall a. Enum a => a -> a
succ Position
preTag
doAction :: (Position, Action) -> ST s ()
doAction (Position
tag,Action
SetPre) = forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STUArray s Position Position
pos Position
tag Position
preTag
doAction (Position
tag,Action
SetPost) = forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STUArray s Position Position
pos Position
tag Position
postTag
doAction (Position
tag,SetVal Position
v) = forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STUArray s Position Position
pos Position
tag Position
v
{-# INLINE mkTest #-}
mkTest :: Uncons text => Bool -> WhichTest -> Index -> Char -> text -> Bool
mkTest :: forall text.
Uncons text =>
Bool -> WhichTest -> Position -> Char -> text -> Bool
mkTest Bool
isMultiline = if Bool
isMultiline then forall text.
Uncons text =>
WhichTest -> Position -> Char -> text -> Bool
test_multiline else forall text.
Uncons text =>
WhichTest -> Position -> Char -> text -> Bool
test_singleline
data MQA s = MQA {forall s. MQA s -> Position
mqa_start :: !Position, forall s. MQA s -> WScratch s
mqa_ws :: !(WScratch s)}
data MQ s = MQ { forall s. MQ s -> STRef s Position
mq_earliest :: !(STRef s Position)
, forall s. MQ s -> STRef s [MQA s]
mq_list :: !(STRef s [MQA s])
}
newMQ :: S.ST s (MQ s)
newMQ :: forall s. ST s (MQ s)
newMQ = do
STRef s Position
earliest <- forall a s. a -> ST s (STRef s a)
newSTRef forall a. Bounded a => a
maxBound
STRef s [MQA s]
list <- forall a s. a -> ST s (STRef s a)
newSTRef []
forall (m :: * -> *) a. Monad m => a -> m a
return (forall s. STRef s Position -> STRef s [MQA s] -> MQ s
MQ STRef s Position
earliest STRef s [MQA s]
list)
resetMQ :: MQ s -> S.ST s ()
resetMQ :: forall s. MQ s -> ST s ()
resetMQ (MQ {mq_earliest :: forall s. MQ s -> STRef s Position
mq_earliest=STRef s Position
earliest,mq_list :: forall s. MQ s -> STRef s [MQA s]
mq_list=STRef s [MQA s]
list}) = do
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s Position
earliest forall a. Bounded a => a
maxBound
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s [MQA s]
list []
putMQ :: WScratch s -> MQ s -> S.ST s ()
putMQ :: forall s. WScratch s -> MQ s -> ST s ()
putMQ WScratch s
ws (MQ {mq_earliest :: forall s. MQ s -> STRef s Position
mq_earliest=STRef s Position
earliest,mq_list :: forall s. MQ s -> STRef s [MQA s]
mq_list=STRef s [MQA s]
list}) = do
Position
start <- forall s. WScratch s -> STUArray s Position Position
w_pos WScratch s
ws forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
0
let mqa :: MQA s
mqa = forall s. Position -> WScratch s -> MQA s
MQA Position
start WScratch s
ws
Position
startE <- forall s a. STRef s a -> ST s a
readSTRef STRef s Position
earliest
if Position
start forall a. Ord a => a -> a -> Bool
<= Position
startE
then forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s Position
earliest Position
start forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s [MQA s]
list [MQA s
mqa]
else do
[MQA s]
old <- forall s a. STRef s a -> ST s a
readSTRef STRef s [MQA s]
list
let !rest :: [MQA s]
rest = forall a. (a -> Bool) -> [a] -> [a]
dropWhile (\ MQA s
m -> Position
start forall a. Ord a => a -> a -> Bool
<= forall s. MQA s -> Position
mqa_start MQA s
m) [MQA s]
old
!new :: [MQA s]
new = MQA s
mqa forall a. a -> [a] -> [a]
: [MQA s]
rest
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s [MQA s]
list [MQA s]
new
getMQ :: Position -> MQ s -> ST s [WScratch s]
getMQ :: forall s. Position -> MQ s -> ST s [WScratch s]
getMQ Position
pos (MQ {mq_earliest :: forall s. MQ s -> STRef s Position
mq_earliest=STRef s Position
earliest,mq_list :: forall s. MQ s -> STRef s [MQA s]
mq_list=STRef s [MQA s]
list}) = do
[MQA s]
old <- forall s a. STRef s a -> ST s a
readSTRef STRef s [MQA s]
list
case forall a. (a -> Bool) -> [a] -> ([a], [a])
span (\MQA s
m -> Position
pos forall a. Ord a => a -> a -> Bool
<= forall s. MQA s -> Position
mqa_start MQA s
m) [MQA s]
old of
([],[MQA s]
ans) -> do
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s Position
earliest forall a. Bounded a => a
maxBound
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s [MQA s]
list []
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (a -> b) -> [a] -> [b]
map forall s. MQA s -> WScratch s
mqa_ws [MQA s]
ans)
([MQA s]
new,[MQA s]
ans) -> do
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s Position
earliest (forall s. MQA s -> Position
mqa_start (forall a. [a] -> a
last [MQA s]
new))
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s [MQA s]
list [MQA s]
new
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (a -> b) -> [a] -> [b]
map forall s. MQA s -> WScratch s
mqa_ws [MQA s]
ans)
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
Position
((Position, Instructions), STUArray s Position Position, 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 Position (Maybe (STUArray s Position Position))
m_pos :: !(STArray s Index (Maybe (STUArray s Tag Position)))
, forall s. MScratch s -> STArray s Position OrbitLog
m_orbit :: !(STArray s Index OrbitLog)
}
newtype BlankScratch s = BlankScratch { forall s. BlankScratch s -> STUArray s Position Position
blank_pos :: (STUArray s Tag Position)
}
newtype WScratch s = WScratch { forall s. WScratch s -> STUArray s Position Position
w_pos :: (STUArray s Tag Position)
}
{-# 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) =>
(Position, Position) -> e -> ST s (STUArray s Position e)
newA (Position, Position)
b_tags e
initial = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Position, Position)
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) =>
(Position, Position) -> ST s (STUArray s Position e)
newA_ (Position, Position)
b_tags = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Position, Position)
b_tags
newScratch :: (Index,Index) -> (Tag,Tag) -> S.ST s (SScratch s)
newScratch :: forall s.
(Position, Position) -> (Position, Position) -> ST s (SScratch s)
newScratch (Position, Position)
b_index (Position, Position)
b_tags = do
MScratch s
s1 <- forall s. (Position, Position) -> ST s (MScratch s)
newMScratch (Position, Position)
b_index
MScratch s
s2 <- forall s. (Position, Position) -> ST s (MScratch s)
newMScratch (Position, Position)
b_index
MQ s
winQ <- forall s. ST s (MQ s)
newMQ
BlankScratch s
blank <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s. STUArray s Position Position -> BlankScratch s
BlankScratch (forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> e -> ST s (STUArray s Position e)
newA (Position, Position)
b_tags (-Position
1))
STArray
s
Position
((Position, Instructions), STUArray s Position Position, OrbitLog)
which <- (forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Position, Position)
b_index ((-Position
1,forall a. [Char] -> a
err [Char]
"newScratch which 1"),forall a. [Char] -> a
err [Char]
"newScratch which 2",forall a. [Char] -> a
err [Char]
"newScratch which 3"))
forall (m :: * -> *) a. Monad m => a -> m a
return (forall s.
MScratch s
-> MScratch s
-> (MQ s, BlankScratch s,
STArray
s
Position
((Position, Instructions), STUArray s Position Position, OrbitLog))
-> SScratch s
SScratch MScratch s
s1 MScratch s
s2 (MQ s
winQ,BlankScratch s
blank,STArray
s
Position
((Position, Instructions), STUArray s Position Position, OrbitLog)
which))
newMScratch :: (Index,Index) -> S.ST s (MScratch s)
newMScratch :: forall s. (Position, Position) -> ST s (MScratch s)
newMScratch (Position, Position)
b_index = do
STArray s Position (Maybe (STUArray s Position Position))
pos's <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Position, Position)
b_index forall a. Maybe a
Nothing
STArray s Position OrbitLog
orbit's <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Position, Position)
b_index forall a. Monoid a => a
Mon.mempty
forall (m :: * -> *) a. Monad m => a -> m a
return (forall s.
STArray s Position (Maybe (STUArray s Position Position))
-> STArray s Position OrbitLog -> MScratch s
MScratch STArray s Position (Maybe (STUArray s Position Position))
pos's STArray s Position OrbitLog
orbit's)
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 Position
v1,SetVal Position
v2) -> forall a. Ord a => a -> a -> Ordering
compare Position
v1 Position
v2
(Action, Action)
_ -> forall a. [Char] -> a
err forall a b. (a -> b) -> a -> b
$ [Char]
"bestTrans.compareWith.choose sees incomparable "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> [Char]
show (Action
post1,Action
post2)
ditzyComp'3 :: forall s. Array Tag OP -> C s
ditzyComp'3 :: forall s. Array Position OP -> C s
ditzyComp'3 Array Position OP
aTagOP = C s
comp0 where
(F [F s] -> C s
comp1:[F s]
compsRest) = Position -> [F s]
allcomps Position
1
comp0 :: C s
comp0 :: C s
comp0 Position
preTag x1 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1@((Position, Instructions)
_state1,STUArray s Position Position
pos1,OrbitLog
_orbit1') [(Position, Action)]
np1 x2 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2@((Position, Instructions)
_state2,STUArray s Position Position
pos2,OrbitLog
_orbit2') [(Position, Action)]
np2 = do
Ordering
c <- forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall a. Ord a => a -> a -> Ordering
compare (STUArray s Position Position
pos2forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!!Position
0) (STUArray s Position Position
pos1forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!!Position
0)
case Ordering
c of
Ordering
EQ -> [F s] -> C s
comp1 [F s]
compsRest Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
np2
Ordering
answer -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
answer
allcomps :: Tag -> [F s]
allcomps :: Position -> [F s]
allcomps Position
tag | Position
tag forall a. Ord a => a -> a -> Bool
> Position
top = [forall s. ([F s] -> C s) -> F s
F (\ [F s]
_ Position
_ ((Position, Instructions), STUArray s Position Position, OrbitLog)
_ [(Position, Action)]
_ ((Position, Instructions), STUArray s Position Position, OrbitLog)
_ [(Position, Action)]
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
EQ)]
| Bool
otherwise =
case Array Position OP
aTagOP forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Position
tag of
OP
Orbit -> forall s. ([F s] -> C s) -> F s
F (forall {s}.
Position
-> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
challenge_Orb Position
tag) forall a. a -> [a] -> [a]
: Position -> [F s]
allcomps (forall a. Enum a => a -> a
succ Position
tag)
OP
Maximize -> forall s. ([F s] -> C s) -> F s
F (forall {s}.
Position
-> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
challenge_Max Position
tag) forall a. a -> [a] -> [a]
: Position -> [F s]
allcomps (forall a. Enum a => a -> a
succ Position
tag)
OP
Ignore -> forall s. ([F s] -> C s) -> F s
F (forall {s}.
Position
-> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
challenge_Ignore Position
tag) forall a. a -> [a] -> [a]
: Position -> [F s]
allcomps (forall a. Enum a => a -> a
succ Position
tag)
OP
Minimize -> forall a. [Char] -> a
err [Char]
"allcomps Minimize"
where top :: Position
top = forall a b. (a, b) -> b
snd (forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> (i, i)
bounds Array Position OP
aTagOP)
challenge_Ignore :: Position
-> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
challenge_Ignore !Position
tag (F [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next:[F s]
comps) Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
np2 =
case [(Position, Action)]
np1 of
((Position
t1,Action
_):[(Position, Action)]
rest1) | Position
t1forall a. Eq a => a -> a -> Bool
==Position
tag ->
case [(Position, Action)]
np2 of
((Position
t2,Action
_):[(Position, Action)]
rest2) | Position
t2forall a. Eq a => a -> a -> Bool
==Position
tag -> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next [F s]
comps Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
rest1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
rest2
[(Position, Action)]
_ -> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next [F s]
comps Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
rest1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
np2
[(Position, Action)]
_ -> do
case [(Position, Action)]
np2 of
((Position
t2,Action
_):[(Position, Action)]
rest2) | Position
t2forall a. Eq a => a -> a -> Bool
==Position
tag -> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next [F s]
comps Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
rest2
[(Position, Action)]
_ -> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next [F s]
comps Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
np2
challenge_Ignore Position
_ [] Position
_ ((Position, Instructions), STUArray s Position Position, OrbitLog)
_ [(Position, Action)]
_ ((Position, Instructions), STUArray s Position Position, OrbitLog)
_ [(Position, Action)]
_ = forall a. [Char] -> a
err [Char]
"impossible 2347867"
challenge_Max :: Position
-> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
challenge_Max !Position
tag (F [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next:[F s]
comps) Position
preTag x1 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1@((Position, Instructions)
_state1,STUArray s Position Position
pos1,OrbitLog
_orbit1') [(Position, Action)]
np1 x2 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2@((Position, Instructions)
_state2,STUArray s Position Position
pos2,OrbitLog
_orbit2') [(Position, Action)]
np2 =
case [(Position, Action)]
np1 of
((Position
t1,Action
b1):[(Position, Action)]
rest1) | Position
t1forall a. Eq a => a -> a -> Bool
==Position
tag ->
case [(Position, Action)]
np2 of
((Position
t2,Action
b2):[(Position, Action)]
rest2) | Position
t2forall a. Eq a => a -> a -> Bool
==Position
tag ->
if Action
b1forall a. Eq a => a -> a -> Bool
==Action
b2 then [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next [F s]
comps Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
rest1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
rest2
else forall (m :: * -> *) a. Monad m => a -> m a
return (Action -> Action -> Ordering
orderOf Action
b1 Action
b2)
[(Position, Action)]
_ -> do
Position
p2 <- STUArray s Position Position
pos2 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
tag
let p1 :: Position
p1 = case Action
b1 of Action
SetPre -> Position
preTag
Action
SetPost -> forall a. Enum a => a -> a
succ Position
preTag
SetVal Position
v -> Position
v
if Position
p1forall a. Eq a => a -> a -> Bool
==Position
p2 then [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next [F s]
comps Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
rest1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
np2
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ord a => a -> a -> Ordering
compare Position
p1 Position
p2)
[(Position, Action)]
_ -> do
Position
p1 <- STUArray s Position Position
pos1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
tag
case [(Position, Action)]
np2 of
((Position
t2,Action
b2):[(Position, Action)]
rest2) | Position
t2forall a. Eq a => a -> a -> Bool
==Position
tag -> do
let p2 :: Position
p2 = case Action
b2 of Action
SetPre -> Position
preTag
Action
SetPost -> forall a. Enum a => a -> a
succ Position
preTag
SetVal Position
v -> Position
v
if Position
p1forall a. Eq a => a -> a -> Bool
==Position
p2 then [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next [F s]
comps Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
rest2
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ord a => a -> a -> Ordering
compare Position
p1 Position
p2)
[(Position, Action)]
_ -> do
Position
p2 <- STUArray s Position Position
pos2 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
tag
if Position
p1forall a. Eq a => a -> a -> Bool
==Position
p2 then [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next [F s]
comps Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
np2
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ord a => a -> a -> Ordering
compare Position
p1 Position
p2)
challenge_Max Position
_ [] Position
_ ((Position, Instructions), STUArray s Position Position, OrbitLog)
_ [(Position, Action)]
_ ((Position, Instructions), STUArray s Position Position, OrbitLog)
_ [(Position, Action)]
_ = forall a. [Char] -> a
err [Char]
"impossible 9384324"
challenge_Orb :: Position
-> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
challenge_Orb !Position
tag (F [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next:[F s]
comps) Position
preTag x1 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1@((Position, Instructions)
_state1,STUArray s Position Position
_pos1,OrbitLog
orbit1') [(Position, Action)]
np1 x2 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2@((Position, Instructions)
_state2,STUArray s Position Position
_pos2,OrbitLog
orbit2') [(Position, Action)]
np2 =
let s1 :: Maybe Orbits
s1 = forall a. Position -> IntMap a -> Maybe a
IMap.lookup Position
tag OrbitLog
orbit1'
s2 :: Maybe Orbits
s2 = forall a. Position -> IntMap a -> Maybe a
IMap.lookup Position
tag OrbitLog
orbit2'
in case (Maybe Orbits
s1,Maybe Orbits
s2) of
(Maybe Orbits
Nothing,Maybe Orbits
Nothing) -> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next [F s]
comps Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
np2
(Just Orbits
o1,Just Orbits
o2) | Orbits -> Bool
inOrbit Orbits
o1 forall a. Eq a => a -> a -> Bool
== Orbits -> Bool
inOrbit Orbits
o2 ->
case forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Maybe Position
ordinal Orbits
o1) (Orbits -> Maybe Position
ordinal Orbits
o2) forall a. Monoid a => a -> a -> a
`mappend`
ViewL Position -> ViewL Position -> Ordering
comparePos (forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Position
getOrbits Orbits
o1)) (forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Position
getOrbits Orbits
o2)) of
Ordering
EQ -> [F s]
-> Position
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s Position Position,
OrbitLog)
-> [(Position, Action)]
-> ST s Ordering
next [F s]
comps Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
np2
Ordering
answer -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
answer
(Maybe Orbits, Maybe Orbits)
_ -> forall a. [Char] -> a
err forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
unlines [ [Char]
"challenge_Orb is too stupid to handle mismatched orbit data :"
, forall a. Show a => a -> [Char]
show(Position
tag,Position
preTag,[(Position, Action)]
np1,[(Position, Action)]
np2)
, forall a. Show a => a -> [Char]
show Maybe Orbits
s1
, forall a. Show a => a -> [Char]
show Maybe Orbits
s2
]
challenge_Orb Position
_ [] Position
_ ((Position, Instructions), STUArray s Position Position, OrbitLog)
_ [(Position, Action)]
_ ((Position, Instructions), STUArray s Position Position, OrbitLog)
_ [(Position, Action)]
_ = forall a. [Char] -> a
err [Char]
"impossible 0298347"
comparePos :: (ViewL Position) -> (ViewL Position) -> Ordering
comparePos :: ViewL Position -> ViewL Position -> Ordering
comparePos ViewL Position
EmptyL ViewL Position
EmptyL = Ordering
EQ
comparePos ViewL Position
EmptyL ViewL Position
_ = Ordering
GT
comparePos ViewL Position
_ ViewL Position
EmptyL = Ordering
LT
comparePos (Position
p1 :< Seq Position
ps1) (Position
p2 :< Seq Position
ps2) =
forall a. Ord a => a -> a -> Ordering
compare Position
p1 Position
p2 forall a. Monoid a => a -> a -> a
`mappend` ViewL Position -> ViewL Position -> Ordering
comparePos (forall a. Seq a -> ViewL a
viewl Seq Position
ps1) (forall a. Seq a -> ViewL a
viewl Seq Position
ps2)
tagsToGroupsST :: forall s. Array GroupIndex [GroupInfo] -> WScratch s -> S.ST s MatchArray
tagsToGroupsST :: forall s.
Array Position [GroupInfo] -> WScratch s -> ST s MatchArray
tagsToGroupsST Array Position [GroupInfo]
aGroups (WScratch {w_pos :: forall s. WScratch s -> STUArray s Position Position
w_pos=STUArray s Position Position
pos})= do
let b_max :: Position
b_max = forall a b. (a, b) -> b
snd (forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> (i, i)
bounds (Array Position [GroupInfo]
aGroups))
STArray s Position (Position, Position)
ma <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Position
0,Position
b_max) (-Position
1,Position
0) :: ST s (STArray s Int (MatchOffset,MatchLength))
Position
startPos0 <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
0
Position
stopPos0 <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
1
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STArray s Position (Position, Position)
ma Position
0 (Position
startPos0,Position
stopPos0forall a. Num a => a -> a -> a
-Position
startPos0)
let act :: Position -> [GroupInfo] -> ST s ()
act Position
_this_index [] = forall (m :: * -> *) a. Monad m => a -> m a
return ()
act Position
this_index ((GroupInfo Position
_ Position
parent Position
start Position
stop Position
flagtag):[GroupInfo]
gs) = do
Position
flagVal <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
flagtag
if (-Position
1) forall a. Eq a => a -> a -> Bool
== Position
flagVal then Position -> [GroupInfo] -> ST s ()
act Position
this_index [GroupInfo]
gs
else do
Position
startPos <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
start
Position
stopPos <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
stop
(Position
startParent,Position
lengthParent) <- STArray s Position (Position, Position)
ma forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
parent
let ok :: Bool
ok = (Position
0 forall a. Ord a => a -> a -> Bool
<= Position
startParent Bool -> Bool -> Bool
&&
Position
0 forall a. Ord a => a -> a -> Bool
<= Position
lengthParent Bool -> Bool -> Bool
&&
Position
startParent forall a. Ord a => a -> a -> Bool
<= Position
startPos Bool -> Bool -> Bool
&&
Position
stopPos forall a. Ord a => a -> a -> Bool
<= Position
startPos forall a. Num a => a -> a -> a
+ Position
lengthParent)
if Bool -> Bool
not Bool
ok then Position -> [GroupInfo] -> ST s ()
act Position
this_index [GroupInfo]
gs
else forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STArray s Position (Position, Position)
ma Position
this_index (Position
startPos,Position
stopPosforall a. Num a => a -> a -> a
-Position
startPos)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (forall a. Ix a => (a, a) -> [a]
range (Position
1,Position
b_max)) forall a b. (a -> b) -> a -> b
$ (\Position
i -> forall {s}.
(MArray (STUArray s) Position (ST s),
MArray (STArray s) (Position, Position) (ST s)) =>
Position -> [GroupInfo] -> ST s ()
act Position
i (Array Position [GroupInfo]
aGroupsforall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
!Position
i))
forall i (a :: * -> * -> *) e (m :: * -> *) (b :: * -> * -> *).
(Ix i, MArray a e m, IArray b e) =>
a i e -> m (b i e)
unsafeFreeze STArray s Position (Position, Position)
ma
{-# INLINE spawnStart #-}
spawnStart :: (Tag,Tag) -> BlankScratch s -> Index -> MScratch s -> Position -> S.ST s Position
spawnStart :: forall s.
(Position, Position)
-> BlankScratch s
-> Position
-> MScratch s
-> Position
-> ST s Position
spawnStart (Position, Position)
b_tags (BlankScratch STUArray s Position Position
blankPos) Position
i MScratch s
s1 Position
thisPos = do
Maybe (STUArray s Position Position)
oldPos <- forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
i
STUArray s Position Position
pos <- case Maybe (STUArray s Position Position)
oldPos of
Maybe (STUArray s Position Position)
Nothing -> do
STUArray s Position Position
pos' <- forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> ST s (STUArray s Position e)
newA_ (Position, Position)
b_tags
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1) Position
i (forall a. a -> Maybe a
Just STUArray s Position Position
pos')
forall (m :: * -> *) a. Monad m => a -> m a
return STUArray s Position Position
pos'
Just STUArray s Position Position
pos -> forall (m :: * -> *) a. Monad m => a -> m a
return STUArray s Position Position
pos
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 STUArray s Position Position
blankPos STUArray s Position Position
pos
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1) Position
i forall a b. (a -> b) -> a -> b
$! forall a. Monoid a => a
mempty
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STUArray s Position Position
pos Position
0 Position
thisPos
forall (m :: * -> *) a. Monad m => a -> m a
return Position
thisPos
{-# INLINE updateCopy #-}
updateCopy :: ((Index, Instructions), STUArray s Tag Position, OrbitLog)
-> Index
-> MScratch s
-> Int
-> ST s Position
updateCopy :: forall s.
((Position, Instructions), STUArray s Position Position, OrbitLog)
-> Position -> MScratch s -> Position -> ST s Position
updateCopy ((Position
_i1,Instructions
instructions),STUArray s Position Position
oldPos,OrbitLog
newOrbit) Position
preTag MScratch s
s2 Position
i2 = do
(Position, Position)
b_tags <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> m (i, i)
getBounds STUArray s Position Position
oldPos
STUArray s Position Position
newerPos <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (do
STUArray s Position Position
a <- forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> ST s (STUArray s Position e)
newA_ (Position, Position)
b_tags
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s2) Position
i2 (forall a. a -> Maybe a
Just STUArray s Position Position
a)
forall (m :: * -> *) a. Monad m => a -> m a
return STUArray s Position Position
a) forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s2 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
i2
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 STUArray s Position Position
oldPos STUArray s Position Position
newerPos
forall s.
Position
-> STUArray s Position Position -> [(Position, Action)] -> ST s ()
doActions Position
preTag STUArray s Position Position
newerPos (Instructions -> [(Position, Action)]
newPos Instructions
instructions)
forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s2) Position
i2 forall a b. (a -> b) -> a -> b
$! OrbitLog
newOrbit
STUArray s Position Position
newerPos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
0
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 = forall a b. a -> b
unsafeCoerce forall a.
MutableByteArray# RealWorld
-> MutableByteArray# RealWorld -> Int# -> IO (Ptr a)
memcpyIO
{-# 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 ()
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
_ Position
_ MutableByteArray# s
msource) _destination :: STUArray s i e
_destination@(STUArray i
_ i
_ Position
_ MutableByteArray# s
mdest) =
forall s a. STRep s a -> ST s a
ST forall a b. (a -> b) -> a -> b
$ \State# s
s1# ->
case forall d. MutableByteArray# d -> Int#
sizeofMutableByteArray# MutableByteArray# s
msource of { Int#
n# ->
case 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 Any
_ #) ->
(# State# s
s2#, () #) }}