2using System.Collections;
3using System.Collections.Generic;
5using System.Xml.Serialization;
17[XmlType(TypeName =
"integerTimeSeries")]
22 private int[] _values = Array.Empty<
int>();
47 Resize(periodCount, defaultValue);
59 get => (
int[])_values.Clone();
60 set => ReplaceValues(value ?? Array.Empty<
int>());
79 public int this[
int period]
83 int index = ConvertPeriodToIndex(period);
84 return _values[index];
88 int index = ConvertPeriodToIndex(period);
91 if (_values[index] == value)
96 _values[index] = value;
119 int defaultValue = 0)
123 throw new ArgumentOutOfRangeException(
126 "The number of periods cannot be negative.");
130 if (periodCount == _values.Length)
135 int previousPeriodCount = _values.Length;
136 var resizedValues =
new int[periodCount];
139 int copiedValueCount = Math.Min(
143 if (copiedValueCount > 0)
152 if (periodCount > previousPeriodCount)
158 periodCount - previousPeriodCount);
161 _values = resizedValues;
177 if (_values.All(currentValue => currentValue == value))
182 Array.Fill(_values, value);
201 this[period] = value;
236 _values[previousLength] =
249 if (_values.Length == 0)
267 return ((IEnumerable<int>)_values).GetEnumerator();
270 IEnumerator IEnumerable.GetEnumerator()
279 private void ReplaceValues(
int[] values)
282 if (_values.SequenceEqual(values))
288 bool periodCountChanged =
289 _values.Length != values.Length;
292 _values = (
int[])values.Clone();
297 if (periodCountChanged)
311 private int ConvertPeriodToIndex(
int period)
313 if (period < 1 || period > _values.Length)
315 throw new ArgumentOutOfRangeException(
318 $
"The period must be between 1 and {_values.Length}.");
void Fill(int value)
Assigns the same value to every planning period.
void Resize(int periodCount, int defaultValue=0)
Resizes the time series.
void Add(int value)
Adds a value to the end of the time series.
int GetValue(int period)
Gets the value associated with a planning period.
IntegerTimeSeries Clone()
Creates an independent copy of this time series.
int PeriodCount
Gets the number of planning periods represented by this time series.
int[] Values
Gets or replaces all values of the time series.
void Clear()
Removes all values from the time series.
IntegerTimeSeries(int periodCount, int defaultValue=0)
Initializes a time series with the specified number of periods.
void SetValue(int period, int value)
Sets the value associated with a planning period.
IEnumerator< int > GetEnumerator()
Returns an enumerator over the period values.
IntegerTimeSeries()
Initializes an empty time series.
Base class for model objects that notify listeners when one of their properties changes.
virtual void OnPropertyChanged([CallerMemberName] string? propertyName=null)
Raises the PropertyChanged event.