ArrayData.h
116 lines
| 3.2 KiB
| text/x-c
|
CLexer
|
r124 | #ifndef SCIQLOP_ARRAYDATA_H | ||
#define SCIQLOP_ARRAYDATA_H | ||||
r364 | #include <QReadLocker> | |||
#include <QReadWriteLock> | ||||
|
r124 | #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>{}} | ||||
{ | ||||
|
r372 | QWriteLocker locker{&m_Lock}; | ||
|
r124 | m_Data[0].resize(nbColumns); | ||
} | ||||
|
r392 | /** | ||
* Ctor for a unidimensional ArrayData | ||||
* @param data the data the ArrayData will hold | ||||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}} | ||||
{ | ||||
QWriteLocker locker{&m_Lock}; | ||||
m_Data[0] = std::move(data); | ||||
} | ||||
|
r372 | /// Copy ctor | ||
r364 | explicit ArrayData(const ArrayData &other) | |||
{ | ||||
|
r372 | QReadLocker otherLocker{&other.m_Lock}; | ||
QWriteLocker locker{&m_Lock}; | ||||
r364 | m_Data = other.m_Data; | |||
} | ||||
|
r124 | /** | ||
* 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 | ||||
{ | ||||
|
r372 | QWriteLocker locker{&m_Lock}; | ||
|
r124 | 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> > | ||||
r364 | QVector<double> data() const noexcept | |||
|
r124 | { | ||
|
r372 | QReadLocker locker{&m_Lock}; | ||
r294 | return m_Data[0]; | |||
|
r124 | } | ||
r233 | /** | |||
|
r449 | * @return the data as a vector, as a const reference | ||
r233 | * @remarks this method is only available for a unidimensional ArrayData | |||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
|
r449 | const QVector<double> &cdata() const noexcept | ||
r233 | { | |||
|
r372 | QReadLocker locker{&m_Lock}; | ||
|
r449 | return m_Data.at(0); | ||
r233 | } | |||
// TODO Comment | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
r294 | void merge(const ArrayData<1> &arrayData) | |||
r233 | { | |||
|
r372 | QWriteLocker locker{&m_Lock}; | ||
r233 | if (!m_Data.empty()) { | |||
|
r372 | QReadLocker otherLocker{&arrayData.m_Lock}; | ||
r294 | m_Data[0] += arrayData.data(); | |||
r233 | } | |||
} | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
r364 | int size() const | |||
r233 | { | |||
|
r372 | QReadLocker locker{&m_Lock}; | ||
r233 | return m_Data[0].size(); | |||
} | ||||
r364 | void clear() | |||
{ | ||||
|
r372 | QWriteLocker locker{&m_Lock}; | ||
r364 | m_Data.clear(); | |||
} | ||||
r233 | ||||
|
r124 | private: | ||
QVector<QVector<double> > m_Data; | ||||
r364 | mutable QReadWriteLock m_Lock; | |||
|
r124 | }; | ||
#endif // SCIQLOP_ARRAYDATA_H | ||||