ArrayData.h
105 lines
| 2.8 KiB
| text/x-c
|
CLexer
Alexandre Leroux
|
r116 | #ifndef SCIQLOP_ARRAYDATA_H | ||
#define SCIQLOP_ARRAYDATA_H | ||||
r336 | #include <QReadLocker> | |||
#include <QReadWriteLock> | ||||
Alexandre Leroux
|
r116 | #include <QVector> | ||
/** | ||||
* @brief The ArrayData class represents a dataset for a data series. | ||||
* | ||||
* A dataset can be unidimensional or two-dimensional. This property is determined by the Dim | ||||
* template-parameter. | ||||
* | ||||
* @tparam Dim the dimension of the ArrayData (one or two) | ||||
* @sa IDataSeries | ||||
*/ | ||||
template <int Dim> | ||||
class ArrayData { | ||||
public: | ||||
/** | ||||
* Ctor for a unidimensional ArrayData | ||||
* @param nbColumns the number of values the ArrayData will hold | ||||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
explicit ArrayData(int nbColumns) : m_Data{1, QVector<double>{}} | ||||
{ | ||||
Alexandre Leroux
|
r344 | QWriteLocker locker{&m_Lock}; | ||
Alexandre Leroux
|
r116 | m_Data[0].resize(nbColumns); | ||
} | ||||
Alexandre Leroux
|
r344 | /// Copy ctor | ||
r336 | explicit ArrayData(const ArrayData &other) | |||
{ | ||||
Alexandre Leroux
|
r344 | QReadLocker otherLocker{&other.m_Lock}; | ||
QWriteLocker locker{&m_Lock}; | ||||
r336 | m_Data = other.m_Data; | |||
} | ||||
Alexandre Leroux
|
r116 | /** | ||
* Sets a data at a specified index. The index has to be valid to be effective | ||||
* @param index the index to which the data will be set | ||||
* @param data the data to set | ||||
* @remarks this method is only available for a unidimensional ArrayData | ||||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
void setData(int index, double data) noexcept | ||||
{ | ||||
Alexandre Leroux
|
r344 | QWriteLocker locker{&m_Lock}; | ||
Alexandre Leroux
|
r116 | if (index >= 0 && index < m_Data.at(0).size()) { | ||
m_Data[0].replace(index, data); | ||||
} | ||||
} | ||||
/** | ||||
* @return the data as a vector | ||||
* @remarks this method is only available for a unidimensional ArrayData | ||||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
r336 | QVector<double> data() const noexcept | |||
Alexandre Leroux
|
r116 | { | ||
Alexandre Leroux
|
r344 | QReadLocker locker{&m_Lock}; | ||
r272 | return m_Data[0]; | |||
Alexandre Leroux
|
r116 | } | ||
r217 | /** | |||
* @return the data as a vector | ||||
* @remarks this method is only available for a unidimensional ArrayData | ||||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
r336 | QVector<double> data(double tStart, double tEnd) const noexcept | |||
r217 | { | |||
Alexandre Leroux
|
r344 | QReadLocker locker{&m_Lock}; | ||
r217 | return m_Data.at(tStart); | |||
} | ||||
// TODO Comment | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
r272 | void merge(const ArrayData<1> &arrayData) | |||
r217 | { | |||
Alexandre Leroux
|
r344 | QWriteLocker locker{&m_Lock}; | ||
r217 | if (!m_Data.empty()) { | |||
Alexandre Leroux
|
r344 | QReadLocker otherLocker{&arrayData.m_Lock}; | ||
r272 | m_Data[0] += arrayData.data(); | |||
r217 | } | |||
} | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
r336 | int size() const | |||
r217 | { | |||
Alexandre Leroux
|
r344 | QReadLocker locker{&m_Lock}; | ||
r217 | return m_Data[0].size(); | |||
} | ||||
r336 | void clear() | |||
{ | ||||
Alexandre Leroux
|
r344 | QWriteLocker locker{&m_Lock}; | ||
r336 | m_Data.clear(); | |||
} | ||||
r217 | ||||
Alexandre Leroux
|
r116 | private: | ||
QVector<QVector<double> > m_Data; | ||||
r336 | mutable QReadWriteLock m_Lock; | |||
Alexandre Leroux
|
r116 | }; | ||
#endif // SCIQLOP_ARRAYDATA_H | ||||