Skip to content

Avoid use recursion#900

Open
yzyray wants to merge 2 commits into
featurecat:masterfrom
yzyray:avoid_recursion
Open

Avoid use recursion#900
yzyray wants to merge 2 commits into
featurecat:masterfrom
yzyray:avoid_recursion

Conversation

@yzyray

@yzyray yzyray commented Sep 13, 2021

Copy link
Copy Markdown
Contributor

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

@kaorahi

kaorahi commented Sep 20, 2021

Copy link
Copy Markdown
Contributor

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

kaorahi commented Sep 20, 2021

Copy link
Copy Markdown
Contributor

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

@yzyray

yzyray commented Sep 21, 2021

Copy link
Copy Markdown
Contributor Author

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

kaorahi pushed a commit to kaorahi/lizzie that referenced this pull request Oct 26, 2021
@kaorahi kaorahi mentioned this pull request Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants