Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F6017048
SetGenerator.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
SetGenerator.php
View Options
<?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
Details
Attached
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)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment