beaucal / beaucal-quick-union
提供有向或最短路径的并查集功能。
v1.0.0
2015-05-10 20:58 UTC
Requires
- php: >=5.4
- zendframework/zend-db: ~2.1
- zendframework/zend-servicemanager: ~2.1
Requires (Dev)
- phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-14 17:26:46 UTC
README
现在实现了100%的代码覆盖率。
提供有向或最短路径的并查集功能。
安装
- 在
application.config.php
中添加以下内容
'modules' => [..., 'BeaucalQuickUnion', ...];
- 将数据库导入到
data/beaucal_union.sql
CREATE TABLE IF NOT EXISTS `beaucal_union` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, `item` varchar(255) NOT NULL UNIQUE KEY, `set` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `beaucal_union` ADD INDEX(`set`); ALTER TABLE `beaucal_union` ADD FOREIGN KEY (`set`) REFERENCES `beaucal_union`(`item`) ON DELETE RESTRICT ON UPDATE CASCADE;
使用方法
// in controller $union = $this->getServiceLocator()->get('BeaucalQuickUnion'); $union->union('AAA', 'BBB'); echo $union->query('AAA'); // 'AAA' or 'BBB', random/set behaviour echo $union->query('AAA') == $union->query('BBB'); // TRUE echo $union->query('BBB') == $union->query('ZZZ'); // FALSE $union->union(new Order\Directed('AAA', 'ZZZ')); echo $union->query('AAA'); // 'ZZZ' echo $union->query('BBB') == $union->query('ZZZ'); // TRUE // change from random/set behaviour to known/directed // or change via beaucalquickunion.global.php: option order_class $union->getOptions()->setOrderClass('BeaucalQuickUnion\Order\Directed'); $union->union('PPP', 'QQQ'); echo $union->query('PPP'); // 'QQQ', no longer random
独立结构
如果你需要一个独立的并查集空间,只需在每个项目前加上命名空间,例如 union('JobID::123', 'JobID::456')
。
或者,为了完全分离,配置另一个并查集+适配器实例并更改其数据库表。
$adapterOptions = $serviceLocator->get('BeaucalQuickUnion\Options\DbAdapter'); $adapterOptions->setDbTable('beaucal_union_separate'); $gateway = new TableGateway( $adapterOptions->getDbTable(), $serviceLocator->get($adapterOptions->getDbAdapterClass()) ); $adapter = new DbAdapter($gateway, $adapterOptions); $union = new Union($adapter, $unionOptions);
内存适配器
如果你只需要为单个请求创建一个短生命周期的实例,请使用内存适配器,如下所示
// in beaucalquickunion.global.php $union = [ 'adapter_class' => 'BeaucalQuickUnion\Adapter\Memory', // ... ] // in controller $union = $this->getServiceLocator()->get('BeaucalQuickUnion'); // alternatively, a shortcut factory that doesn't require config $throttle = $this->getServiceLocator()->get('BeaucalQuickUnion_Memory');