Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F6093371
SequenceGenerator.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
SequenceGenerator.php
View Options
<?php
namespace
Eris\Generator
;
use
Eris\Generator
;
use
Eris\Generators
;
use
Eris\Random\RandomRange
;
function
seq
(
Generator
$singleElementGenerator
)
{
return
Generators
::
seq
(
$singleElementGenerator
);
}
/**
* @psalm-template T
* @template-implements Generator<list<T>>
*/
class
SequenceGenerator
implements
Generator
{
private
$singleElementGenerator
;
public
function
__construct
(
Generator
$singleElementGenerator
)
{
$this
->
singleElementGenerator
=
$singleElementGenerator
;
}
public
function
__invoke
(
$size
,
RandomRange
$rand
)
{
$sequenceLength
=
$rand
->
rand
(
0
,
$size
);
return
$this
->
vector
(
$sequenceLength
)->
__invoke
(
$size
,
$rand
);
}
public
function
shrink
(
GeneratedValue
$sequence
)
{
$options
=
[];
if
(
count
(
$sequence
->
unbox
())
>
0
)
{
$options
[]
=
$this
->
shrinkInSize
(
$sequence
);
// TODO: try to shrink the elements also of longer sequences
if
(
count
(
$sequence
->
unbox
())
<
10
)
{
// a size which is computationally acceptable
$shrunkElements
=
$this
->
shrinkTheElements
(
$sequence
);
foreach
(
$shrunkElements
as
$shrunkValue
)
{
$options
[]
=
$shrunkValue
;
}
}
}
return
new
GeneratedValueOptions
(
$options
);
}
private
function
shrinkInSize
(
$sequence
)
{
if
(
count
(
$sequence
->
unbox
())
===
0
)
{
return
$sequence
;
}
$input
=
$sequence
->
input
();
$indexOfElementToRemove
=
array_rand
(
$input
);
unset
(
$input
[
$indexOfElementToRemove
]);
$input
=
array_values
(
$input
);
return
GeneratedValueSingle
::
fromValueAndInput
(
array_map
(
function
(
$element
)
{
return
$element
->
unbox
();
},
$input
),
$input
,
'sequence'
);
}
/**
* @return GeneratedValueOptions
*/
private
function
shrinkTheElements
(
$sequence
)
{
return
$this
->
vector
(
count
(
$sequence
->
unbox
()))->
shrink
(
$sequence
);
}
private
function
vector
(
$size
)
{
return
new
VectorGenerator
(
$size
,
$this
->
singleElementGenerator
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Oct 6 2026, 00:29 (6 w, 1 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
ad/7a/28aec0e95ab8d0c3041b9fdb0747
Default Alt Text
SequenceGenerator.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment