ArrayData.h
186 lines
| 5.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> | ||
Alexandre Leroux
|
r416 | |||
#include <memory> | ||||
Alexandre Leroux
|
r116 | /** | ||
* @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 | ||||
Alexandre Leroux
|
r461 | * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same | ||
* number of values | ||||
Alexandre Leroux
|
r116 | * | ||
* @tparam Dim the dimension of the ArrayData (one or two) | ||||
* @sa IDataSeries | ||||
*/ | ||||
template <int Dim> | ||||
class ArrayData { | ||||
public: | ||||
Alexandre Leroux
|
r361 | /** | ||
* 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>{}} | ||||
{ | ||||
m_Data[0] = std::move(data); | ||||
} | ||||
Alexandre Leroux
|
r461 | /** | ||
* Ctor for a two-dimensional ArrayData. The number of components (number of vectors) must be | ||||
* greater than 2 and each component must have the same number of values | ||||
* @param data the data the ArrayData will hold | ||||
* @throws std::invalid_argument if the number of components is less than 2 | ||||
* @remarks if the number of values is not the same for each component, no value is set | ||||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 2> > | ||||
explicit ArrayData(QVector<QVector<double> > data) | ||||
{ | ||||
auto nbComponents = data.size(); | ||||
if (nbComponents < 2) { | ||||
throw std::invalid_argument{ | ||||
QString{"A multidimensional ArrayData must have at least 2 components (found: %1"} | ||||
.arg(data.size()) | ||||
.toStdString()}; | ||||
} | ||||
auto nbValues = data.front().size(); | ||||
if (std::all_of(data.cbegin(), data.cend(), [nbValues](const auto &component) { | ||||
return component.size() == nbValues; | ||||
})) { | ||||
m_Data = std::move(data); | ||||
} | ||||
else { | ||||
m_Data = QVector<QVector<double> >{nbComponents, QVector<double>{}}; | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r344 | /// Copy ctor | ||
r336 | explicit ArrayData(const ArrayData &other) | |||
{ | ||||
Alexandre Leroux
|
r344 | QReadLocker otherLocker{&other.m_Lock}; | ||
r336 | m_Data = other.m_Data; | |||
} | ||||
Alexandre Leroux
|
r416 | /** | ||
* @return the data at a specified index | ||||
* @remarks index must be a valid position | ||||
* @remarks this method is only available for a unidimensional ArrayData | ||||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
double at(int index) const noexcept | ||||
{ | ||||
QReadLocker locker{&m_Lock}; | ||||
return m_Data[0].at(index); | ||||
} | ||||
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 | /** | |||
Alexandre Leroux
|
r415 | * @return the data as a vector, as a const reference | ||
r217 | * @remarks this method is only available for a unidimensional ArrayData | |||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
Alexandre Leroux
|
r415 | const QVector<double> &cdata() const noexcept | ||
r217 | { | |||
Alexandre Leroux
|
r344 | QReadLocker locker{&m_Lock}; | ||
Alexandre Leroux
|
r415 | return m_Data.at(0); | ||
r217 | } | |||
Alexandre Leroux
|
r417 | /** | ||
* Merges into the array data an other array data | ||||
* @param other the array data to merge with | ||||
* @param prepend if true, the other array data is inserted at the beginning, otherwise it is | ||||
* inserted at the end | ||||
* @remarks this method is only available for a unidimensional ArrayData | ||||
*/ | ||||
r217 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |||
Alexandre Leroux
|
r417 | void add(const ArrayData<1> &other, bool prepend = false) | ||
r217 | { | |||
Alexandre Leroux
|
r344 | QWriteLocker locker{&m_Lock}; | ||
r217 | if (!m_Data.empty()) { | |||
Alexandre Leroux
|
r417 | QReadLocker otherLocker{&other.m_Lock}; | ||
if (prepend) { | ||||
const auto &otherData = other.data(); | ||||
const auto otherDataSize = otherData.size(); | ||||
auto &data = m_Data[0]; | ||||
data.insert(data.begin(), otherDataSize, 0.); | ||||
for (auto i = 0; i < otherDataSize; ++i) { | ||||
data.replace(i, otherData.at(i)); | ||||
} | ||||
} | ||||
else { | ||||
m_Data[0] += other.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(); | |||
} | ||||
Alexandre Leroux
|
r416 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||
std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> sortPermutation) | ||||
{ | ||||
QReadLocker locker{&m_Lock}; | ||||
const auto &data = m_Data.at(0); | ||||
// Inits result | ||||
auto sortedData = QVector<double>{}; | ||||
sortedData.resize(data.size()); | ||||
std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(), | ||||
[&data](int i) { return data[i]; }); | ||||
return std::make_shared<ArrayData<Dim> >(std::move(sortedData)); | ||||
} | ||||
Alexandre Leroux
|
r420 | |||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
r336 | void clear() | |||
{ | ||||
Alexandre Leroux
|
r344 | QWriteLocker locker{&m_Lock}; | ||
Alexandre Leroux
|
r420 | m_Data[0].clear(); | ||
r336 | } | |||
r217 | ||||
Alexandre Leroux
|
r116 | private: | ||
QVector<QVector<double> > m_Data; | ||||
r336 | mutable QReadWriteLock m_Lock; | |||
Alexandre Leroux
|
r116 | }; | ||
#endif // SCIQLOP_ARRAYDATA_H | ||||