feat(trie): 添加字典树数据结构实现

- 实现了Node和Trie类的基本结构
- 添加了insert方法用于插入单词
- 添加了search方法用于搜索完整单词
- 添加了startsWith方法用于前缀匹配
- 实现了字典树的完整功能逻辑

fix(compiler): 修复变量声明和数组访问的代码生成问题

- 为int、float、bool类型变量添加默认初始化值0
- 修复数组维度解析中的括号处理逻辑
- 解决赋值表达式上下文中的数组访问问题

example(prime): 添加素数计算性能测试示例

- 实现了NthPrime函数计算第n个素数
- 添加了时间测量功能
- 提供了30000个素数计算的性能测试用例
pull/1/head
韩天峰 7 months ago
parent 75a9309801
commit 8fd83ae8b0
  1. 40
      examples/prime.go
  2. 14
      src/Php/CompilerBase.php
  3. 75
      tests/misc/2.phpt

@ -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毫秒

@ -709,7 +709,11 @@ class CompilerBase extends \PhpAot\Core\Translator
if (isset($this->arguments[$name])) { if (isset($this->arguments[$name])) {
continue; 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"; $code .= "\n";
$this->indentLevel--; $this->indentLevel--;
@ -1715,9 +1719,11 @@ class CompilerBase extends \PhpAot\Core\Translator
return $var . '.newItem()'; return $var . '.newItem()';
} }
} else { } else {
$dim = $this->trimBrackets($this->parseIdentifier($node->dim)); $oriInAssignExpr = $this->inAssignExpr;
$this->inAssignExpr = false;
return $var . '.item(' . $dim . ', ' . $this->escapeBool($write) . ')'; $dim = $this->parseIdentifier($node->dim);
$this->inAssignExpr = $oriInAssignExpr;
return $var . '.item(' . $this->trimBrackets($dim) . ', ' . $this->escapeBool($write) . ')';
} }
} }

@ -0,0 +1,75 @@
--TEST--
mixed/2.phpt
--FILE--
<?php
class Node
{
public $is_end = false;
public $child = [];
}
class Trie
{
private $root = null;
function __construct()
{
$this->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)
Loading…
Cancel
Save