From 8fd83ae8b0609da107705f8f329777e36962a7f3 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 5 Feb 2026 17:04:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(trie):=20=E6=B7=BB=E5=8A=A0=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E6=A0=91=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现了Node和Trie类的基本结构 - 添加了insert方法用于插入单词 - 添加了search方法用于搜索完整单词 - 添加了startsWith方法用于前缀匹配 - 实现了字典树的完整功能逻辑 fix(compiler): 修复变量声明和数组访问的代码生成问题 - 为int、float、bool类型变量添加默认初始化值0 - 修复数组维度解析中的括号处理逻辑 - 解决赋值表达式上下文中的数组访问问题 example(prime): 添加素数计算性能测试示例 - 实现了NthPrime函数计算第n个素数 - 添加了时间测量功能 - 提供了30000个素数计算的性能测试用例 --- examples/prime.go | 40 +++++++++++++++++++++ src/Php/CompilerBase.php | 14 +++++--- tests/misc/2.phpt | 75 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 examples/prime.go create mode 100644 tests/misc/2.phpt diff --git a/examples/prime.go b/examples/prime.go new file mode 100644 index 00000000..0f3d027e --- /dev/null +++ b/examples/prime.go @@ -0,0 +1,40 @@ +package main + +import ( + "strconv" + "time" +) + +func NthPrime(n int) int { + + var ( + i int = 2 + j int = 1 + ) + + for { + j = j + 1; + if j > i/j { + n-- + if n == 0 { + break + } + j = 1 + } + if i%j == 0 { + i++; + j = 1; + } + } + return i +} + +func main() { + s := time.Now().UnixNano() / 1e6 + n := 300000 + result := NthPrime(n) + e := time.Now().UnixNano() / 1e6 + time := e - s + println("第" + strconv.Itoa(n) + "个素数的值是:" + strconv.Itoa(result) + " 耗时" + strconv.Itoa(int(time)) + "毫秒") +} +//第300000个素数的值是:4256233 耗时10417毫秒 \ No newline at end of file diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 3212a6bb..bf2a88f1 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -709,7 +709,11 @@ class CompilerBase extends \PhpAot\Core\Translator if (isset($this->arguments[$name])) { continue; } - $code .= $this->getIndent() . $type . ' ' . $name . ';' . PHP_EOL; + $code .= $this->getIndent() . $type . ' ' . $name; + if ($type === self::TYPE_INT or $type === self::TYPE_FLOAT or $type === self::TYPE_BOOL) { + $code .= ' = 0'; + } + $code .= ';' . PHP_EOL; } $code .= "\n"; $this->indentLevel--; @@ -1715,9 +1719,11 @@ class CompilerBase extends \PhpAot\Core\Translator return $var . '.newItem()'; } } else { - $dim = $this->trimBrackets($this->parseIdentifier($node->dim)); - - return $var . '.item(' . $dim . ', ' . $this->escapeBool($write) . ')'; + $oriInAssignExpr = $this->inAssignExpr; + $this->inAssignExpr = false; + $dim = $this->parseIdentifier($node->dim); + $this->inAssignExpr = $oriInAssignExpr; + return $var . '.item(' . $this->trimBrackets($dim) . ', ' . $this->escapeBool($write) . ')'; } } diff --git a/tests/misc/2.phpt b/tests/misc/2.phpt new file mode 100644 index 00000000..859b20d2 --- /dev/null +++ b/tests/misc/2.phpt @@ -0,0 +1,75 @@ +--TEST-- +mixed/2.phpt +--FILE-- +root = new Node(); + } + + function insert($word) + { + $node = $this->root; + + for ($i = 0; $i < strlen($word); $i++) { + if (!isset($node->child[$word[$i]])) { + $node->child[$word[$i]] = new Node(); + } + + $node = $node->child[$word[$i]]; + if ($i == strlen($word) - 1) { + $node->is_end = true; + } + } + } + + function search($word) + { + $node = $this->root; + for ($i = 0; $i < strlen($word); $i++) { + if (isset($node->child[$word[$i]])) { + $node = $node->child[$word[$i]]; + } else { + return false; + } + } + + return $node->is_end; + } + + function startsWith($prefix) + { + $node = $this->root; + for ($i = 0; $i < strlen($prefix); $i++) { + if (isset($node->child[$prefix[$i]])) { + $node = $node->child[$prefix[$i]]; + } else { + return false; + } + } + return true; + } +} + +function main() +{ + $Trie = new Trie(); + $Trie->insert('apple'); + var_dump($Trie->search("apple")); + var_dump($Trie->startsWith("apps")); +} +?> +--EXPECT-- +bool(true) +bool(false)