calabrothers/php-ds-tree
Composer 安装命令:
composer require calabrothers/php-ds-tree
包简介
PHP Tree Support
README 文档
README
PHP Tree Support
A class to support bitmask operations.
Install
composer require calabrothers/php-ds-tree
Test
composer install
composer test
Tree Library
Referring to the example tree:
We can introduce the following relevant information to discuss library functionality:
| Set | Value |
|---|---|
| Root Node | A |
| Leaves | {H, I, E, F, G} |
Relationship with other nodes:
| Set | Value |
|---|---|
| Children(A) | { B , C } |
| Parent(B) | A |
| Siblings(D) | { E, F, G } |
| Descendants(B) | { D, E, H, I } |
| Anchestors(H) | { D, B, A } |
For each of this set, an additional set is defined including the argument itself, hence:
| Set | Value |
|---|---|
| ChildrenAndSelf(A) | { A, B , C } |
| SiblingsAndSelf(D) | { D, E, F, G } |
| DescendantsAndSelf(B) | { B, D, E, H, I } |
| AnchestorsAndSelf(H) | { D, B, A } |
Static functions:
As Ds\Deque object, the Tree library provides access to the following operations:
- apply (change the value of the node with a callable)
- filter (select a subset of nodes where the condition is specified with a a callable)
- map (evaluate the nodes value to compute the result given with a callable)
These basic operation are provided as static functions and they operate with a given set of nodes. A magic function provide access to all the previously defined sets.
Example: Let us calculate
y = 2 x sum( Descendants(A) )
First we need to get a set of nodes given as Descendants(A), then apply a map to double the value. Finally we should sum the elements of the resulting vector.
// Let build some tree...
/*
A(1)
/ \
B(2) C(3)
/ \
D(4) E(5)
*/
$oA = new TreeNode(1);
$oB = new TreeNode(2);
$oC = new TreeNode(3);
$oD = new TreeNode(4);
$oE = new TreeNode(5);
Connect the nodes to form the tree:
// Connects the nodes
$oC->attachChild($oD);
$oC->attachChild($oE);
$oA->attachChild($oB);
$oA->attachChild($oC);
Compute the result:
// Compute 2 x Value, forall the Descendants(A)
$aValue = TreeNode::mapSet(
$oA->getDescendants(),
function ($oValue) {
return 2 * $oValue;
}
);
echo $aValue->sum()."\n"; // 28
Referring to the same tree, we can for example get the list of nodes having even value as:
// Get nodes having even value
$aEven = TreeNode::filterSet(
$oA->getNodes(),
function ($oValue) {
return $oValue % 2 == 0;
}
);
echo "(".$aEven[0]->getValue().",".$aEven[1]->getValue().")"; // (2,4)
Tree Builder
Library provides a convenient class to help construction of trees.
The builder requires a callable, responsible to build new nodes. For example considering a tree of integer values:
$oTreeC = new TreeBuilder(
function (int $nNumber) : int {
return $nNumber;
}
);
With this information, everytime the function begin() is called, a new node is added, forwarding the parameters of the begin() function to the callable function specified in the TreeBuilder constructor.
$oTreeC
->begin(1)
->begin(2)
->end()
->begin(3)
->begin(4)
->end()
->begin(5)
->end()
->end()
->end();
It is possible for all the types with a proper __toString() method to see the node values. For instance, in this case:
echo $oTree;
// Result:
┌1
└────2
└────3
└────4
└────5
The TreeBuilder is interesting when combined with custom node class, for example, let us consider to build a Tree having as nodes the object of following class:
class TreeNodeExample {
public $nX;
public $szY;
public function __construct(int $nX, string $szY) {
$this->nX = $nX;
$this->szY = $szY;
}
public function myMultiply(int $nZ) {
$this->nX *= $nZ;
$this->szY = implode("+", array_fill(0, $nZ,$this->szY));
}
public function __toString():string {
return "($this->nX|$this->szY)";
}
}
Then we can use a TreeBuilder as:
$oTreeC = new TreeBuilder(
function (int $nX, string $szY) : TreeNodeExample {
return new TreeNodeExample($nX, $szY);
}
);
$oTreeC
->begin(1, 'one')
->begin(2, 'two')
->end()
->begin(3, 'three')
->begin(4, 'four')
->myMultiply(2) // This will call the method of TreeNodeExample! :)
->end()
->begin(5, 'five')
->myMultiply(3)
->end()
->end()
->end();
echo $oTreeC;
┌(1|one)
└────(2|two)
└────(3|three)
└────(8|four+four)
└────(15|five+five+five)
Notes
I strongly reccomend to use PHP Ds extension rather than its equivalent Php version. Check https://github.com/php-ds/ext-ds for more information.
Credits
Support quality code
License
The MIT License (MIT). Please see LICENSE for more information.
calabrothers/php-ds-tree 适用场景与选型建议
calabrothers/php-ds-tree 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「tree」 「library」 「ds」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 calabrothers/php-ds-tree 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 calabrothers/php-ds-tree 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 calabrothers/php-ds-tree 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
jsTree widget for yii2
Doctrine2 behavior traits - slowhop fork
Inbox pattern process implementation for your Laravel Applications
A fast and dynamic Merkle tree implementation
Data structure collections, helpers and utilities for yii2 ActiveRecord models(trees, sorts and etc.)
Core library that defines common interfaces used by the rest of the intahwebz..
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-19