lizzie icon indicating copy to clipboard operation
lizzie copied to clipboard

Avoid use recursion

Open yzyray opened this issue 3 years ago • 3 comments

Recursion is slow and lead to StackOverflowError when saving extremly big analyed game like: test52_52.zip

yzyray avatar Sep 13 '21 02:09 yzyray

It seems that ( and ) are missing for variations. (;B[aa](;W[ba])(;W[ab])) is saved as (;B[aa];W[ba];W[ab]) wrongly.

  1. Paste the first SGF to Lizzie.
  2. Type Ctrl-C.
  3. Type Ctrl-V.

Then the variation tree is changed with this PR. It is not changed in the original Lizzie 0.7.4.

kaorahi avatar Sep 20 '21 15:09 kaorahi

a crude fix?

--- a/src/main/java/featurecat/lizzie/rules/SGFParser.java
+++ b/src/main/java/featurecat/lizzie/rules/SGFParser.java
@@ -684,14 +684,28 @@ public class SGFParser {
     // *  with 'xy' = coordinates ; or 'tt' for pass.
 
     // Write variation tree
+    BoardHistoryNode markerBeg = new BoardHistoryNode(null);
+    BoardHistoryNode markerEnd = new BoardHistoryNode(null);
     Stack<BoardHistoryNode> stack = new Stack<>();
     stack.push(history.getCurrentHistoryNode());
     while (!stack.isEmpty()) {
       BoardHistoryNode cur = stack.pop();
+      if (cur == markerBeg) {
+        builder.append('(');
+        continue;
+      }
+      if (cur == markerEnd) {
+        builder.append(')');
+        continue;
+      }
       builder.append(generateNode(board, cur));
+      boolean hasBrothers = (cur.numberOfChildren() > 1);
       if (cur.numberOfChildren() >= 1) {
-        for (int i = cur.numberOfChildren() - 1; i >= 0; i--)
+        for (int i = cur.numberOfChildren() - 1; i >= 0; i--) {
+          if (hasBrothers) stack.push(markerEnd);
           stack.push(cur.getVariations().get(i));
+          if (hasBrothers) stack.push(markerBeg);
+        }
       }
     }
     // close file

kaorahi avatar Sep 20 '21 16:09 kaorahi

Yes, I forgot the important "()",I think your fix is great.

yzyray avatar Sep 21 '21 03:09 yzyray