]> git.f4mnq.fr Git - cells.git/commitdiff
Optimisation de l'avancée de l'automate
authorAmélia Coutard-Sander <git@f4mnq.fr>
Wed, 7 Jan 2026 13:38:54 +0000 (14:38 +0100)
committerAmélia Coutard-Sander <git@f4mnq.fr>
Wed, 7 Jan 2026 13:53:54 +0000 (14:53 +0100)
automata/automata.ml

index b257f3c978c6c7a6d6834877ac2e3ba89ef217f3..361a977889ab15420325b3c17788bebfa1d7414f 100644 (file)
@@ -110,13 +110,42 @@ let update (type t) (m : (module Automaton with type t = t)) (d, board) =
         ( M.transition (M.map (Fun.const d) M.neighbours),
           List.fold_left
             (fun b (cx, cy) ->
+              let getcc dx dy =
+                      match CoordMap.find_opt (cx + dx, cy + dy) board with
+                      | Some chunk -> fun x y -> chunk.(x).(y)
+                      | None -> fun _ _ -> d
+              in
+              let getll = getcc (-1) (-1)
+              and getle = getcc (-1) 0
+              and getlg = getcc (-1) 1
+              and getel = getcc 0 (-1)
+              and getee = getcc 0 0
+              and geteg = getcc 0 1
+              and getgl = getcc 1 (-1)
+              and getge = getcc 1 0
+              and getgg = getcc 1 1 in
+              let get x y =
+                      match () with
+                      | () when x < 0 -> (
+                              match () with
+                              | () when y < 0 -> getll (x + chunk_size) (y + chunk_size)
+                              | () when y < chunk_size -> getle (x + chunk_size) y
+                              | () -> getlg (x + chunk_size) (y - chunk_size))
+                      | () when x < chunk_size -> (
+                              match () with
+                              | () when y < 0 -> getel x (y + chunk_size)
+                              | () when y < chunk_size -> getee x y
+                              | () -> geteg x (y - chunk_size))
+                      | () -> (
+                              match () with
+                              | () when y < 0 -> getgl (x - chunk_size) (y + chunk_size)
+                              | () when y < chunk_size -> getge (x - chunk_size) y
+                              | () -> getgg (x - chunk_size) (y - chunk_size))
+              in
               CoordMap.add (cx, cy)
                 (Array.init chunk_size (fun x ->
                      Array.init chunk_size (fun y ->
-                         M.transition
-                           (M.map
-                              (fun (dx, dy) -> get ((cx * chunk_size) + x + dx) ((cy * chunk_size) + y + dy) (d, board))
-                              M.neighbours))))
+                         M.transition (M.map (fun (dx, dy) -> get (x + dx) (y + dy)) M.neighbours))))
                 b)
             CoordMap.empty ccoords
           |> CoordMap.filter (fun _ -> Array.exists (Array.exists (fun c -> c <> d))) )