@@ -12,7 +12,6 @@ this data structure can be used to represent an ordered series of dates or times
12
12
330: Constantine move the capital to Constantinople.
13
13
*/
14
14
15
- using System . Collections ;
16
15
17
16
namespace DataStructures ;
18
17
@@ -23,83 +22,39 @@ namespace DataStructures;
23
22
/// <typeparam name="TValue">Value associated with a <see cref="DateTime" />.</typeparam>
24
23
public class Timeline < TValue > : ICollection < ( DateTime Time , TValue Value ) > , IEquatable < Timeline < TValue > >
25
24
{
26
- /// <summary>
27
- /// Inner collection storing the timeline events as key-tuples.
28
- /// </summary>
29
- private readonly List < ( DateTime Time , TValue Value ) > timeline = new ( ) ;
25
+ // Inner collection storing the timeline events as key-tuples.
26
+ private List < ( DateTime Time , TValue Value ) > timeline = [ ] ;
30
27
31
- /// <summary>
32
- /// Initializes a new instance of the <see cref="Timeline{TValue}"/> class.
33
- /// </summary>
34
- public Timeline ( )
35
- {
36
- }
28
+ // Default constructor
29
+ public Timeline ( ) { }
37
30
38
- /// <summary>
39
- /// Initializes a new instance of the <see cref="Timeline{TValue}"/> class populated with an initial event.
40
- /// </summary>
41
- /// <param name="time">The time at which the given event occurred.</param>
42
- /// <param name="value">The event's content.</param>
43
- public Timeline ( DateTime time , TValue value )
44
- => timeline = new List < ( DateTime , TValue ) >
45
- {
46
- ( time , value ) ,
47
- } ;
31
+ // Constructor with initial event
32
+ public Timeline ( DateTime time , TValue value ) => timeline = [ ( time , value ) ] ;
48
33
49
- /// <summary>
50
- /// Initializes a new instance of the <see cref="Timeline{TValue}"/> class containing the provided events
51
- /// ordered chronologically.
52
- /// </summary>
53
- /// <param name="timeline">The timeline to represent.</param>
54
- public Timeline ( params ( DateTime , TValue ) [ ] timeline )
55
- => this . timeline = timeline
56
- . OrderBy ( pair => pair . Item1 )
57
- . ToList ( ) ;
34
+ // Constructor with provided events, ordered chronologically
35
+ public Timeline ( params ( DateTime , TValue ) [ ] timeline ) => this . timeline = timeline . OrderBy ( pair => pair . Item1 ) . ToList ( ) ;
58
36
59
- /// <summary>
60
- /// Gets he number of unique times within this timeline.
61
- /// </summary>
62
- public int TimesCount
63
- => GetAllTimes ( ) . Length ;
37
+ // Gets the number of unique times within this timeline.
38
+ public int TimesCount => GetAllTimes ( ) . Length ;
64
39
65
- /// <summary>
66
- /// Gets all events that has occurred in this timeline.
67
- /// </summary>
68
- public int ValuesCount
69
- => GetAllValues ( ) . Length ;
40
+ // Gets all events that has occurred in this timeline.
41
+ public int ValuesCount => GetAllValues ( ) . Length ;
70
42
71
- /// <summary>
72
- /// Get all values associated with <paramref name="time" />.
73
- /// </summary>
74
- /// <param name="time">Time to get values for.</param>
75
- /// <returns>Values associated with <paramref name="time" />.</returns>
43
+ // Get or set all values associated with a time
76
44
public TValue [ ] this [ DateTime time ]
77
45
{
78
46
get => GetValuesByTime ( time ) ;
79
47
set
80
48
{
81
- var overridenEvents = timeline . Where ( @event => @event . Time == time ) . ToList ( ) ;
82
- foreach ( var @event in overridenEvents )
83
- {
84
- timeline . Remove ( @event ) ;
85
- }
86
-
49
+ timeline . RemoveAll ( @event => @event . Time == time ) ;
87
50
foreach ( var v in value )
88
- {
89
51
Add ( time , v ) ;
90
- }
91
52
}
92
53
}
93
54
94
- /// <inheritdoc />
95
- bool ICollection < ( DateTime Time , TValue Value ) > . IsReadOnly
96
- => false ;
55
+ bool ICollection < ( DateTime Time , TValue Value ) > . IsReadOnly => false ;
97
56
98
- /// <summary>
99
- /// Gets the count of pairs.
100
- /// </summary>
101
- public int Count
102
- => timeline . Count ;
57
+ public int Count => timeline . Count ;
103
58
104
59
/// <summary>
105
60
/// Clear the timeline, removing all events.
0 commit comments