From 67bfcab176028cd84758e66b0c9ecec64bef0af2 Mon Sep 17 00:00:00 2001 From: Calcitem Date: Mon, 4 May 2020 10:09:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=96=B0=E5=A2=9E=20deletSubTree()?= =?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E5=B9=B6=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ai/search.cpp | 26 +++++++++++++++----------- src/ai/search.h | 1 + 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/ai/search.cpp b/src/ai/search.cpp index e8d5b6ed..4cdb7152 100644 --- a/src/ai/search.cpp +++ b/src/ai/search.cpp @@ -523,6 +523,20 @@ void AIAlgorithm::deleteTree(Node *node) memmgr.memmgr_free(node); } +void AIAlgorithm::deleteSubTree(Node *node) +{ + int cs = node->childrenSize; + + for (int i = 0; i < cs; i++) { + Node *c = node->children[i]; + int size = c->childrenSize; + for (int j = 0; j < size; j++) { + deleteTree(c->children[j]); + } + c->childrenSize = 0; + } +} + void AIAlgorithm::setState(const StateInfo &g) { // 如果规则改变,重建hashmap @@ -989,19 +1003,9 @@ out: // 删除“孙子”节点,防止层数较深的时候节点树太大 #ifndef DONOT_DELETE_TREE - int cs = node->childrenSize; - for (int i = 0; i < cs; i++) { - Node *c = node->children[i]; - int size = c->childrenSize; - for (int j = 0; j < size; j++) { - deleteTree(c->children[j]); - } - c->childrenSize = 0; - } + deleteSubTree(node); #endif // DONOT_DELETE_TREE - - if (gameOptions.getIDSEnabled()) { #ifdef IDS_ADD_VALUE node->children[0]->value += 1; diff --git a/src/ai/search.h b/src/ai/search.h index 6cd35eaa..ba692862 100644 --- a/src/ai/search.h +++ b/src/ai/search.h @@ -238,6 +238,7 @@ public: /* TODO: Move to private or protected */ // 清空节点树 void deleteTree(Node *node); + void deleteSubTree(Node *node); // 构造根节点 void buildRoot();