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