Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F7429893
SubsetGenerator.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
SubsetGenerator.php
View Options
<?php
namespace
Eris\Generator
;
// TODO: dependency on ForAll is bad,
// maybe inject the relative size?
use
Eris\Generators
;
use
Eris\Quantifier\ForAll
;
use
Eris\Random\RandomRange
;
use
Eris\Generator
;
/**
* @param array $input
* @return SubsetGenerator
*/
function
subset
(
$input
)
{
return
Generators
::
subset
(
$input
);
}
/**
* @psalm-template T
* @template-implements Generator<list<T>>
*/
class
SubsetGenerator
implements
Generator
{
private
$universe
;
public
function
__construct
(
array
$universe
)
{
$this
->
universe
=
$universe
;
}
public
function
__invoke
(
$size
,
RandomRange
$rand
)
{
$relativeSize
=
$size
/
ForAll
::
DEFAULT_MAX_SIZE
;
$maximumSubsetIndex
=
floor
(
pow
(
2
,
count
(
$this
->
universe
))
*
$relativeSize
);
$subsetIndex
=
$rand
->
rand
(
0
,
$maximumSubsetIndex
);
$binaryDescription
=
str_pad
(
decbin
(
$subsetIndex
),
count
(
$this
->
universe
),
"0"
,
STR_PAD_LEFT
);
$subset
=
[];
for
(
$i
=
0
;
$i
<
strlen
(
$binaryDescription
);
$i
++)
{
$elementPresent
=
$binaryDescription
[
$i
];
if
(
$elementPresent
==
"1"
)
{
$subset
[]
=
$this
->
universe
[
$i
];
}
}
return
GeneratedValueSingle
::
fromJustValue
(
$subset
,
'subset'
);
}
public
function
shrink
(
GeneratedValue
$set
)
{
// TODO: see SetGenerator::shrink()
if
(
count
(
$set
->
unbox
())
===
0
)
{
return
$set
;
}
$input
=
$set
->
input
();
// TODO: make deterministic by returning an array of GeneratedValues
$indexOfElementToRemove
=
array_rand
(
$input
);
unset
(
$input
[
$indexOfElementToRemove
]);
return
GeneratedValueSingle
::
fromJustValue
(
array_values
(
$input
),
'subset'
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sun, Nov 22, 10:13 (9 h, 59 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
84/d5/bdcdc499d361577da8c79bdda4cf
Default Alt Text
SubsetGenerator.php (1 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment