ozdemir / subset-finder
一个简单的包,用于查找给定集合的子集。
1.0.3
2024-02-08 10:04 UTC
Requires
- php: ^8.1
- laravel/framework: ^10.0
Requires (Dev)
- nunomaduro/collision: ^7.0
- orchestra/testbench: ^v8.5
- pestphp/pest: ^2.0
README
SubsetFinder PHP 包,利用 Laravel 集合功能,使用户能够根据定义的标准高效地识别给定集合中的子集。在需要从较大集合中提取特定项目子集的场景中,如折扣计算算法或库存管理系统,此工具非常有价值。
安装
您可以通过 Composer 安装 SubsetFinder 包。
composer require ozdemir/subset-finder
用法
以下是如何使用 SubsetFinder 包的基本示例。
use Ozdemir\SubsetFinder\SubsetFinder; use Ozdemir\SubsetFinder\SubsetCollection; use Ozdemir\SubsetFinder\Subset; // Define your collection and subset criteria // Collection should be an instance of Illuminate\Support\Collection // and contain items that implement the Ozdemir\SubsetFinder\Subsetable interface. // example class that implements the Subsetable interface // if you use field names other than 'id' and 'quantity', you need to define them with defineProps method class Something implements Subsetable { public function __construct(public int $id, public int $quantity, public int $price) { } public function getId(): int { return $this->id; } public function getQuantity(): int { return $this->quantity; } } $collection = collect([ new Something(id: 1, quantity: 11, price: 15), new Something(id: 2, quantity: 6, price: 5), new Something(id: 3, quantity: 6, price: 5) // Add more items... ]); $subsetCollection = new SubsetCollection([ Subset::of([1, 2])->take(5), Subset::of([3])->take(2), // Add more criteria... ]); // Instantiate SubsetFinder $subsetter = new SubsetFinder($collection, $subsetCollection); // Optionally, configure sorting $subsetter->sortBy('price'); // Solve the problem $subsetter->solve(); // $subsets will contain the subsets that meet the criteria $subsets = $subsetter->getFoundSubsets(); // Illuminate\Support\Collection: // all:[ // Something(id: 2, quantity: 6, price: 5), // Something(id: 1, quantity: 9, price: 15), // Something(id: 3, quantity: 6, price: 5) // ] // $remaining will contain the items that were not selected for any subset $remaining = $subsetter->getRemaining(); // Illuminate\Support\Collection: // all:[ // Something(id: 1, quantity: 2, price: 15), // ] // Get the maximum quantity of sets that can be created from the collection. $subSetQuantity = $subsetter->getSubsetQuantity() // 3
您可以通过查看测试用例来获取更多示例。
配置
优先包含子集中的项目
// Seek subsets that meet the criteria $subsetCollection = new SubsetCollection([ Subset::of([1, 2, 3])->take(5), // Find a subset with a total quantity of 5 from items 1, 2, and 3 in the collection Subset::of([4, 5])->take(2), // Find a subset with a total quantity of 2 from items 4 and 5 in the collection Subset::of([12])->take(5), // Find a subset with a total quantity of 5 from item 12 in the collection // etc... ]); // When we have multiple applicable items for a subset, we can choose to prioritize the ones // with any field that exists in the main collection. $subsetter->sortBy('price'); $subsetter->solve();
定义数量、项目和 id 字段的名。
$collection = collect([ new Something() // let's say, we have an object with the ["name" => 1, "amount" => 11, "price" => 15] // Add more items... ]); // Find a subset with a total amount of 5 from items named 1 and 2 in the collection // this part doesn't change $setCollection = collect([ Subset::of([1, 2])->take(5) // define more... ]); // We need to define the field names for the 'quantity' and 'id' fields. $subsetter->defineProps( id: 'name', quantity: 'amount' ); $subsetter->solve();
测试
您可以使用以下命令运行测试:
composer test
贡献
欢迎贡献!如果您发现任何问题或有改进建议,请打开问题或在 GitHub 上创建拉取请求。
许可
此包是开源软件,根据 MIT 许可证授权。