@@ -12,7 +12,6 @@ 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 ;
1615
1716namespace DataStructures ;
1817
@@ -23,83 +22,39 @@ namespace DataStructures;
2322/// <typeparam name="TValue">Value associated with a <see cref="DateTime" />.</typeparam>
2423public class Timeline < TValue > : ICollection < ( DateTime Time , TValue Value ) > , IEquatable < Timeline < TValue > >
2524{
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 = [ ] ;
3027
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 ( ) { }
3730
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 ) ] ;
4833
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 ( ) ;
5836
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 ;
6439
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 ;
7042
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
7644 public TValue [ ] this [ DateTime time ]
7745 {
7846 get => GetValuesByTime ( time ) ;
7947 set
8048 {
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 ) ;
8750 foreach ( var v in value )
88- {
8951 Add ( time , v ) ;
90- }
9152 }
9253 }
9354
94- /// <inheritdoc />
95- bool ICollection < ( DateTime Time , TValue Value ) > . IsReadOnly
96- => false ;
55+ bool ICollection < ( DateTime Time , TValue Value ) > . IsReadOnly => false ;
9756
98- /// <summary>
99- /// Gets the count of pairs.
100- /// </summary>
101- public int Count
102- => timeline . Count ;
57+ public int Count => timeline . Count ;
10358
10459 /// <summary>
10560 /// Clear the timeline, removing all events.
0 commit comments