@@ -12,6 +12,7 @@ this data structure can be used to represent an ordered series of dates or times
1212 330: Constantine move the capital to Constantinople.
1313*/
1414
15+ using System . Collections ;
1516
1617namespace DataStructures ;
1718
@@ -22,39 +23,83 @@ namespace DataStructures;
2223/// <typeparam name="TValue">Value associated with a <see cref="DateTime" />.</typeparam>
2324public class Timeline < TValue > : ICollection < ( DateTime Time , TValue Value ) > , IEquatable < Timeline < TValue > >
2425{
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 ( ) ;
2730
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+ }
3037
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+ } ;
3348
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 ( ) ;
3658
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 ;
3964
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 ;
4270
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>
4476 public TValue [ ] this [ DateTime time ]
4577 {
4678 get => GetValuesByTime ( time ) ;
4779 set
4880 {
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+
5087 foreach ( var v in value )
88+ {
5189 Add ( time , v ) ;
90+ }
5291 }
5392 }
5493
55- bool ICollection < ( DateTime Time , TValue Value ) > . IsReadOnly => false ;
94+ /// <inheritdoc />
95+ bool ICollection < ( DateTime Time , TValue Value ) > . IsReadOnly
96+ => false ;
5697
57- public int Count => timeline . Count ;
98+ /// <summary>
99+ /// Gets the count of pairs.
100+ /// </summary>
101+ public int Count
102+ => timeline . Count ;
58103
59104 /// <summary>
60105 /// Clear the timeline, removing all events.
0 commit comments