2using System.Collections;
3using System.Collections.Generic;
5using System.Xml.Serialization;
17[XmlType(TypeName =
"doubleTimeSeries")]
22 private double[] _values = Array.Empty<
double>();
44 double defaultValue = 0.0)
46 Resize(periodCount, defaultValue);
58 get => (
double[])_values.Clone();
59 set => ReplaceValues(value ?? Array.Empty<
double>());
77 public double this[
int period]
81 int index = ConvertPeriodToIndex(period);
82 return _values[index];
86 ValidateFiniteValue(value, nameof(value));
88 int index = ConvertPeriodToIndex(period);
91 if (_values[index].Equals(value))
96 _values[index] = value;
119 double defaultValue = 0.0)
123 throw new ArgumentOutOfRangeException(
126 "The number of periods cannot be negative.");
129 ValidateFiniteValue(defaultValue, nameof(defaultValue));
132 if (periodCount == _values.Length)
137 int previousPeriodCount = _values.Length;
138 var resizedValues =
new double[periodCount];
141 int copiedValueCount = Math.Min(
145 if (copiedValueCount > 0)
154 if (periodCount > previousPeriodCount)
160 periodCount - previousPeriodCount);
163 _values = resizedValues;
175 ValidateFiniteValue(value, nameof(value));
178 if (_values.All(currentValue => currentValue.Equals(value)))
183 Array.Fill(_values, value);
202 this[period] = value;
217 public void Add(
double value)
219 ValidateFiniteValue(value, nameof(value));
221 int previousPeriodCount = _values.Length;
225 previousPeriodCount + 1);
227 _values[previousPeriodCount] = value;
239 if (_values.Length == 0)
244 _values = Array.Empty<
double>();
267 return ((IEnumerable<double>)_values).GetEnumerator();
270 IEnumerator IEnumerable.GetEnumerator()
280 private void ReplaceValues(
double[] values)
283 foreach (
double value
in values)
285 ValidateFiniteValue(value, nameof(values));
289 if (_values.SequenceEqual(values))
295 bool periodCountChanged =
296 _values.Length != values.Length;
299 _values = (
double[])values.Clone();
304 if (periodCountChanged)
318 private int ConvertPeriodToIndex(
int period)
320 if (period < 1 || period > _values.Length)
322 throw new ArgumentOutOfRangeException(
325 $
"The period must be between 1 and {_values.Length}.");
340 private static void ValidateFiniteValue(
342 string parameterName)
344 if (
double.IsNaN(value) ||
345 double.IsInfinity(value))
347 throw new ArgumentOutOfRangeException(
350 "A time-series value must be a finite number.");
void Clear()
Removes all values from the time series.
double GetValue(int period)
Gets the value associated with a planning period.
void Resize(int periodCount, double defaultValue=0.0)
Resizes the time series.
double[] Values
Gets or replaces all values of the time series.
IEnumerator< double > GetEnumerator()
Returns an enumerator over the period values.
DoubleTimeSeries Clone()
Creates an independent copy of this time series.
DoubleTimeSeries(int periodCount, double defaultValue=0.0)
Initializes a time series with the specified number of periods.
int PeriodCount
Gets the number of periods currently represented.
void SetValue(int period, double value)
Sets the value associated with a planning period.
DoubleTimeSeries()
Initializes an empty time series.
void Add(double value)
Appends a value to the end of the time series.
void Fill(double value)
Assigns the same value to every planning period.
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.