Skip to content

Commit 46fe722

Browse files
committed
Optimisation: use Data.Sequence instead of lists.
refs #4.
1 parent 07b65cb commit 46fe722

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies:
2828
- binary
2929
- bytestring
3030
- bitwise
31+
- containers
3132

3233
library:
3334
source-dirs: src

src/Generator.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Data.Array
88
import Data.Int
99
import Data.List
1010
import Data.Ord
11+
import qualified Data.Sequence as Seq
12+
import Data.Sequence ((|>))
1113
import qualified Data.Array.BitArray as BA
1214

1315
import Trace
@@ -26,7 +28,7 @@ import Model
2628
- Number of bots can also be decreased sometimes.
2729
-}
2830

29-
type BotTrace = [Command]
31+
type BotTrace = Seq.Seq Command
3032

3133
type Step = Int
3234

@@ -52,7 +54,7 @@ initState model = GS model filled 0 [(0,[bid])] bots traces
5254
where
5355
bid = 0
5456
bots = array (0, maxBID) [(bid, Bot bid (0,0,0) []) | bid <- [0 .. maxBID]]
55-
traces = array (0, maxBID) [(bid, []) | bid <- [0 .. maxBID]]
57+
traces = array (0, maxBID) [(bid, Seq.empty) | bid <- [0 .. maxBID]]
5658
r = mfResolution model
5759
filled = BA.array ((0,0,0), (r-1,r-1,r-1)) [((x,y,z), False) | x <- [0..r-1], y <- [0..r-1], z <- [0..r-1]]
5860

@@ -84,7 +86,7 @@ issue :: BID -> Command -> Generator ()
8486
issue bid cmd = do
8587
checkBid bid
8688
trace <- getBotTrace bid
87-
let trace' = trace ++ [cmd]
89+
let trace' = trace |> cmd
8890
modify $ \st -> st {gsTraces = gsTraces st // [(bid, trace')]}
8991

9092
nearPlus :: P3 -> NearDiff -> P3
@@ -115,7 +117,7 @@ step = do
115117
let updates = mapMaybe update (indices traces)
116118
update bid = let trace = traces ! bid
117119
in if length trace < n'
118-
then Just (bid, trace ++ [Wait])
120+
then Just (bid, trace |> Wait)
119121
else Nothing
120122
let traces' = traces // updates
121123
modify $ \st -> st {gsStepNumber = n', gsTraces = traces'}
@@ -234,7 +236,7 @@ makeTrace model gen =
234236
botsCommandsAtStep step = map (botCommand step) [traces ! bid | bid <- botsAliveAtStep step]
235237
botCommand step trace =
236238
if step < length trace
237-
then trace !! step
239+
then trace `Seq.index` step
238240
else Wait
239241
in concatMap botsCommandsAtStep [0 .. maxLen-1]
240242

0 commit comments

Comments
 (0)