Page MenuHomeWickedGov Phorge

SetGenerator.php
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

SetGenerator.php

<?php
namespace Eris\Generator;
use Eris\Generator;
use Eris\Generators;
use Eris\Random\RandomRange;
/**
* @param Generator $singleElementGenerator
* @return SetGenerator
*/
function set($singleElementGenerator)
{
return Generators::set($singleElementGenerator);
}
/**
* @psalm-template T
* @template-implements Generator<list<T>>
*/
class SetGenerator implements Generator
{
private $singleElementGenerator;
public function __construct(Generator $singleElementGenerator)
{
$this->singleElementGenerator = $singleElementGenerator;
}
public function __invoke($size, RandomRange $rand)
{
$setSize = rand(0, $size);
$set = [];
$input = [];
$trials = 0;
while (count($set) < $setSize && $trials < 2 * $setSize) {
$trials++;
$candidateNewElement = $this->singleElementGenerator->__invoke($size, $rand);
if (in_array($candidateNewElement->unbox(), $set, $strict = true)) {
continue;
}
$set[] = $candidateNewElement->unbox();
$input[] = $candidateNewElement;
}
return GeneratedValueSingle::fromValueAndInput($set, $input, 'set');
}
public function shrink(GeneratedValue $set)
{
if (count($set->input()) === 0) {
return $set;
}
$input = $set->input();
// TODO: make deterministic
// TODO: shrink also the elements, not just the size of the set
$indexOfElementToRemove = array_rand($input);
unset($input[$indexOfElementToRemove]);
$input = array_values($input);
return GeneratedValueSingle::fromValueAndInput(
array_map(function ($element) {
return $element->unbox();
}, $input),
array_values($input),
'set'
);
}
}

File Metadata

Mime Type
text/x-php
Expires
Oct 4 2026, 13:23 (8 w, 1 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
c4/c2/4ebe857b02cb2c7a0acfb8ef85dc
Default Alt Text
SetGenerator.php (1 KB)

Event Timeline