@@ -22,43 +22,38 @@ private static readonly (string[] Prefixes, string[] Suffixes)[] _parts = new[]
22
22
23
23
internal static IEnumerable < Todo > GenerateTodos ( int count = 5 )
24
24
{
25
- var titleMap = new List < ( int Row , int Prefix , int Suffix ) > ( ) ;
25
+ var titleCount = _parts . Sum ( row => row . Prefixes . Length * row . Suffixes . Length ) ;
26
+ var titleMap = new ( int Row , int Prefix , int Suffix ) [ titleCount ] ;
27
+ var mapCount = 0 ;
26
28
for ( var i = 0 ; i < _parts . Length ; i ++ )
27
29
{
28
30
var prefixes = _parts [ i ] . Prefixes ;
29
31
var suffixes = _parts [ i ] . Suffixes ;
30
- for ( int j = 0 ; j < prefixes . Length ; j ++ )
32
+ for ( var j = 0 ; j < prefixes . Length ; j ++ )
31
33
{
32
- for ( int k = 0 ; k < suffixes . Length ; k ++ )
34
+ for ( var k = 0 ; k < suffixes . Length ; k ++ )
33
35
{
34
- titleMap . Add ( ( i , j , k ) ) ;
36
+ titleMap [ mapCount ++ ] = ( i , j , k ) ;
35
37
}
36
38
}
37
39
}
38
40
39
- var random = new Random ( ) ;
41
+ Random . Shared . Shuffle ( titleMap ) ;
40
42
41
43
for ( var id = 1 ; id <= count ; id ++ )
42
44
{
45
+ var ( rowIndex , prefixIndex , suffixIndex ) = titleMap [ id ] ;
46
+ var ( prefixes , suffixes ) = _parts [ rowIndex ] ;
43
47
yield return new Todo
44
48
{
45
49
Id = id ,
46
- Title = GetNextTitle ( ) ,
47
- DueBy = random . Next ( - 200 , 365 ) switch
50
+ Title = string . Join ( ' ' , prefixes [ prefixIndex ] , suffixes [ suffixIndex ] ) ,
51
+ DueBy = Random . Shared . Next ( - 200 , 365 ) switch
48
52
{
49
53
< 0 => null ,
50
54
var days => DateOnly . FromDateTime ( DateTime . Now . AddDays ( days ) )
51
55
}
52
56
} ;
53
-
54
- string GetNextTitle ( )
55
- {
56
- var index = random . Next ( 0 , titleMap . Count - 1 ) ;
57
- var map = titleMap [ index ] ;
58
- var row = _parts [ map . Row ] ;
59
- titleMap . RemoveAt ( index ) ;
60
- return string . Join ( ' ' , row . Prefixes [ map . Prefix ] , row . Suffixes [ map . Suffix ] ) ;
61
- }
62
57
}
63
58
}
64
59
}
0 commit comments