crell/ordered-collection
Composer 安装命令:
composer require crell/ordered-collection
包简介
A fast and robust library for priority and topological sorting.
README 文档
README
Ordered Collection is what it says on the tin; it's a flexible tool for ordering arbitrary items based on either priority values or topological sorting (before/after). It contains two implementations, OrderedCollection and MultiOrderedCollection. The former is a bit faster, while the latter is considerably more powerful.
For more on Priority and Topological sorting, see this benchmark blog post comparing the results.
OrderedCollection
OrderedCollection supports ordering items by integer priorities, or a single before/after value. Internally, it converts the before/after information into priority values, and then sorts the whole list by priority. This is often faster than sorting topologically, but does not handle more than a single before/after entry per item, and cannot detect cyclic dependencies in the before/after ordering.
To use it first, create a new collection:
use Crell\OrderedCollection\OrderedCollection; $collection = new OrderedCollection();
Now, arbitrary items may be added to the collection with addItem(), addItemBefore(), and addItemAfter(). Each item may be an arbitrary value, and they do not need to be of the same type.
When adding an item, you may provide an $id. If you do not, one will be created on the fly. The ID that was used will be returned by the addItem*() method. The ID is necessary for before/after ordering. If the ID is already in use, a numeric suffix will be added automatically to ensure uniqueness.
// Adds an item with priority 3, and a random ID will be generated. $kirk = $collection->addItem('James T. Kirk', 3); // Adds an item with priority 5, and an ID of "picard". $picard = $collection->addItem('Jean-Luc Picard', 5, 'picard'); // Adds an item to some somewhere after another item, by its ID. // The ID for it will be auto-generated $sisko = $collection->addItemAfter($picard, 'Benjamin Sisko'); // Adds an item to some somewhere before another item, by its ID. // The new item's ID will be "janeway". $janeway = $collection->addItemBefore($kirk, 'Katheryn Janeway', 'janeway');
Once the items are added, they will be sorted automatically the first time the collection is iterated. OrderedCollection implements \IteratorAggregate, so you can use either foreach() or iterator_to_array() to get values back out.
foreach ($collection as $item) { print $item . PHP_EOL; }
In this case, would give the following output:
Katheryn Janeway
Jean-Luc Picard
Benjamin Sisko
James T. Kirk
MultiOrderedCollection
This second, more robust option is very similar to OrderedCollection, and both implement the same OrderableCollection interface. However, MultiOrderedCollection has an additional method, add(), that can handle priority, before, and after ordering, and supports multiple before/after entries on the same item. For that reason, using add() with MultiOrderedCollection is recommended, though the common interface will still work on both.
MultiOrderedCollection converts all priorities into "before" entries, and then sorts the collection topologically. This can be a little bit slower, but it supports multiple before/after directives on a single item and will also detect circular dependencies, which triggers a CycleFound exception.
The add() method's signature is like so:
add( mixed $item, ?string $id = null, ?int $priority = null, array $before = [], array $after = [], ): string
The return value is the ID that was assigned to the value, which may then be used in before/after ordering. Of note, the $before and $after parameters take an array, not a single ID. Also, because there are so many options, using named arguments is strongly recommended.
The example above, on MultiOrderedCollection, would look like this:
use Crell\OrderedCollection\MultiOrderedCollection; $collection = new MultiOrderedCollection(); // Adds an item with priority 3, and a random ID will be generated. $kirk = $collection->add('James T. Kirk', priority: 3); // Adds an item with priority 3, and an ID of "picard". $picard = $collection->add('Jean-Luc Picard', priority: 5, id: 'picard'); // Adds an item to some somewhere after another item, by its ID. // The ID for it will be auto-generated $sisko = $collection->add('Benjamin Sisko', after: ['picard']); // Adds an item to some somewhere before another item, by its ID. // The new item's ID will be "janeway". $janeway = $collection->add('Katheryn Janeway', before: [$kirk], id: 'janeway'); foreach ($collection as $item) { print $item . PHP_EOL; }
In this case, the output would be the same as before.
Katheryn Janeway
Jean-Luc Picard
Benjamin Sisko
James T. Kirk
Guarantees
OrderedCollection and MultiOrderedCollection provide the following guarantees about the resulting list of values
returned:
- Any item with a higher priority integer will come before an item with a lower priority integer.
- Any item listed as "before" another item will come before that item.
- Any item listed as "after" another item will come after that item.
They do not provide the following guarantees, so while they may happen you should not count on them.
- An item that comes "before" another may not come immediately before. There could still be other items that come between them.
- An item that comes "after" another may not come immediately after. There could still be other items that come between them.
- The order in which items are added is irrelevant. With
OrderedCollection, items that have the same priority will usually be returned in the order in which they were added, but that is not guaranteed. WithMultiOrderedCollection, they will most likely not be returned in the order they were added. In short, if you care about the order at all, specify it explicitly.
Types
The items being sorted are not used at all during the process. While the example above shows strings, you can use arrays, objects, strings, numbers, or whatever else you'd like. They may be of the same type, or different types. The collection objects don't care.
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please use the GitHub security reporting form rather than the issue queue.
Credits
License
The Lesser GPL version 3 or later. Please see License File for more information.
crell/ordered-collection 适用场景与选型建议
crell/ordered-collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 183.78k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「utility」 「sorting」 「topological sort」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 crell/ordered-collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 crell/ordered-collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 crell/ordered-collection 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
High-Performance TopSort/Dependency resolving algorithm
High-Performance TopSort/Dependency resolving algorithm (compatibility version to work with 7.0 - 7.2)
Syntactic sugar for PHP's sorting
A collection of enhancements and utilities for the Silverstripe UserForms module
Extends TYPO3 EXT:seo hreflang functionality
PHP7 utility classes
统计信息
- 总下载量: 183.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 30
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2023-03-25