hoa/heap

此包已被废弃且不再维护。没有建议的替代包。

Hoa\Heap 库。

dev-master / 0.x-dev 2017-11-06 12:46 UTC

This package is auto-updated.

Last update: 2021-09-20 08:31:04 UTC


README

Hoa

Build status Coverage Packagist License

Hoa 是一套 模块化可扩展结构化 的 PHP 库。此外,Hoa 致力于成为工业和学术界之间的桥梁。

Hoa\Heap

Help on IRC Help on Gitter Documentation Board

此库提供了一套高级堆,可以支持 标量数组对象闭包 作为项,而不仅仅是整数,其顺序取决于优先级参数。

Hoa\Heap\MinHoa\Heap\Max 类通过比较项目数字来解释优先级。但如果您想使用不同的排序算法,您可以自由地实现自己的类。

⚠️ 警告

默认迭代过程不会从堆中出队,就像常规使用一样。您必须使用生成器方法 toppop 来迭代并从堆中移除项。

安装

使用 Composer,要将此库包含到依赖项中,您需要要求 hoa/heap

$ composer require hoa/heap '~0.0'

有关更多信息,请参阅 源代码页面

测试

考虑到库已通过 Composer 安装,以下命令将运行测试套件

$ composer install
$ vendor/bin/hoa test:run

有关更多信息,请参阅 贡献者指南

快速使用

作为一个简要概述,我们建议查看一个简单的用例,使用 电话号码,这个电话号码必须按照严格的顺序发送到三个方法,检查转换格式化

假设我们没有访问迭代过程。但我们可以排序方法必须调用的顺序,以尊重我们的过程。

注册回调

首先,我们将创建我们的回调过程。

require_once dirname(dirname(__DIR__)) . '/vendor/autoload.php';

// First method used to check if phone number is correct.
$check = function($phone) {
    if (1 !== preg_match('/^\+?[0-9]+$/', $phone)) {
        throw new \Exception('Phone number not conform.');
    }

    return $phone;
};

// Second method used to convert number into object.
$transform = function($phone) {
    return (object)[
        'prefix'  => '+33',
        'country' => 'France',
        'number'  => $phone,
    ];
};

// Third method used to display formatted number.
$format = function(\StdClass $phone) {
    return $phone->prefix
        . ' '
        . wordwrap($phone->number, 3, ' ', true)
    ;
};

创建和填充堆

创建我们的堆,优先级最低(首先调用)。

$heap = new \Hoa\Heap\Min();

// Insert the callback method with the priority argument used for order Heap.
$heap->insert($transform, 20);
$heap->insert($check, 10);
$heap->insert($format, 30);


// Show the number of item in Heap.
var_dump(
    $heap->count()
);

/**
 * Will output:
 *     int(3)
 */

迭代堆

然后我们可以迭代我们的 Heap,并确保正确的调用顺序。将您的数字放入闭包中,并期望过程突变。

// Phone number as expected by first callback.
$number = '123001234';

foreach ($heap as $closure) {
    try {
        // Mutation of number by closure, execute in the priority order expected.
        $number = $closure($number);
    } catch (\Exception $e) {
        break;
    }
}

// Finally, we can display our formatted number.
var_dump($number);

/**
 * Will output:
 *     string(15) "+33 123 001 234"
 */

文档

Hoa\heap 的黑客手册 包含有关如何使用此库及其工作原理的详细信息。

要在本地生成文档,请执行以下命令:

$ composer require --dev hoa/devtools
$ vendor/bin/hoa devtools:documentation --open

更多文档可以在项目的网站上找到: hoa-project.net

获取帮助

获取帮助主要有两种方式:

贡献

你想贡献吗?谢谢!一个详细的 贡献指南 解释了你需要知道的一切。

许可

Hoa 采用新BSD许可(BSD-3-Clause)。请参阅 LICENSE 了解详细信息。

相关项目

尚未注册相关项目,如果您在使用它并希望被列出,请通过打开问题告诉我们!