ArrayData.h
296 lines
| 8.8 KiB
| text/x-c
|
CLexer
|
r124 | #ifndef SCIQLOP_ARRAYDATA_H | ||
#define SCIQLOP_ARRAYDATA_H | ||||
|
r641 | #include "Data/ArrayDataIterator.h" | ||
|
r504 | #include <Common/SortUtils.h> | ||
r364 | #include <QReadLocker> | |||
#include <QReadWriteLock> | ||||
|
r124 | #include <QVector> | ||
|
r450 | |||
#include <memory> | ||||
|
r505 | template <int Dim> | ||
class ArrayData; | ||||
|
r642 | using DataContainer = QVector<double>; | ||
|
r505 | |||
namespace arraydata_detail { | ||||
/// Struct used to sort ArrayData | ||||
template <int Dim> | ||||
struct Sort { | ||||
|
r643 | static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, int nbComponents, | ||
|
r505 | const std::vector<int> &sortPermutation) | ||
{ | ||||
|
r643 | return std::make_shared<ArrayData<Dim> >( | ||
SortUtils::sort(data, nbComponents, sortPermutation), nbComponents); | ||||
|
r505 | } | ||
}; | ||||
/// Specialization for uni-dimensional ArrayData | ||||
template <> | ||||
struct Sort<1> { | ||||
|
r643 | static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, int nbComponents, | ||
|
r505 | const std::vector<int> &sortPermutation) | ||
{ | ||||
|
r643 | Q_UNUSED(nbComponents) | ||
return std::make_shared<ArrayData<1> >(SortUtils::sort(data, 1, sortPermutation)); | ||||
|
r505 | } | ||
}; | ||||
|
r641 | template <int Dim> | ||
class IteratorValue : public ArrayDataIteratorValue::Impl { | ||||
public: | ||||
explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{} | ||||
{ | ||||
for (auto i = 0; i < container.size(); ++i) { | ||||
m_Its.push_back(begin ? container.at(i).cbegin() : container.at(i).cend()); | ||||
} | ||||
} | ||||
IteratorValue(const IteratorValue &other) = default; | ||||
std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override | ||||
{ | ||||
return std::make_unique<IteratorValue<Dim> >(*this); | ||||
} | ||||
bool equals(const ArrayDataIteratorValue::Impl &other) const override try { | ||||
const auto &otherImpl = dynamic_cast<const IteratorValue &>(other); | ||||
return m_Its == otherImpl.m_Its; | ||||
} | ||||
catch (const std::bad_cast &) { | ||||
return false; | ||||
} | ||||
void next() override | ||||
{ | ||||
for (auto &it : m_Its) { | ||||
++it; | ||||
} | ||||
} | ||||
void prev() override | ||||
{ | ||||
for (auto &it : m_Its) { | ||||
--it; | ||||
} | ||||
} | ||||
double at(int componentIndex) const override { return *m_Its.at(componentIndex); } | ||||
double first() const override { return *m_Its.front(); } | ||||
double min() const override | ||||
{ | ||||
auto end = m_Its.cend(); | ||||
auto it = std::min_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) { | ||||
return SortUtils::minCompareWithNaN(*it1, *it2); | ||||
}); | ||||
return it != end ? **it : std::numeric_limits<double>::quiet_NaN(); | ||||
} | ||||
double max() const override | ||||
{ | ||||
auto end = m_Its.cend(); | ||||
auto it = std::max_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) { | ||||
return SortUtils::maxCompareWithNaN(*it1, *it2); | ||||
}); | ||||
return it != end ? **it : std::numeric_limits<double>::quiet_NaN(); | ||||
} | ||||
private: | ||||
std::vector<DataContainer::value_type::const_iterator> m_Its; | ||||
}; | ||||
|
r505 | } // namespace arraydata_detail | ||
|
r124 | /** | ||
* @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 | ||||
|
r501 | * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same | ||
* number of values | ||||
|
r124 | * | ||
* @tparam Dim the dimension of the ArrayData (one or two) | ||||
* @sa IDataSeries | ||||
*/ | ||||
template <int Dim> | ||||
class ArrayData { | ||||
public: | ||||
|
r507 | // ///// // | ||
// Ctors // | ||||
// ///// // | ||||
|
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> > | ||||
|
r642 | explicit ArrayData(DataContainer data) : m_Data{std::move(data)}, m_NbComponents{1} | ||
|
r392 | { | ||
} | ||||
|
r501 | /** | ||
|
r642 | * Ctor for a two-dimensional ArrayData. The number of components (number of lines) must be | ||
* greater than 2 and must be a divisor of the total number of data in the vector | ||||
|
r501 | * @param data the data the ArrayData will hold | ||
|
r642 | * @param nbComponents the number of components | ||
* @throws std::invalid_argument if the number of components is less than 2 or is not a divisor | ||||
* of the size of the data | ||||
|
r501 | */ | ||
template <int D = Dim, typename = std::enable_if_t<D == 2> > | ||||
|
r642 | explicit ArrayData(DataContainer data, int nbComponents) | ||
: m_Data{std::move(data)}, m_NbComponents{nbComponents} | ||||
|
r501 | { | ||
if (nbComponents < 2) { | ||||
throw std::invalid_argument{ | ||||
|
r642 | QString{"A multidimensional ArrayData must have at least 2 components (found: %1)"} | ||
.arg(nbComponents) | ||||
|
r501 | .toStdString()}; | ||
} | ||||
|
r642 | if (m_Data.size() % m_NbComponents != 0) { | ||
throw std::invalid_argument{QString{ | ||||
"The number of components (%1) is inconsistent with the total number of data (%2)"} | ||||
.arg(m_Data.size(), nbComponents) | ||||
.toStdString()}; | ||||
|
r501 | } | ||
} | ||||
|
r372 | /// Copy ctor | ||
r364 | explicit ArrayData(const ArrayData &other) | |||
{ | ||||
|
r372 | QReadLocker otherLocker{&other.m_Lock}; | ||
r364 | m_Data = other.m_Data; | |||
|
r642 | m_NbComponents = other.m_NbComponents; | ||
r364 | } | |||
|
r507 | // /////////////// // | ||
// General methods // | ||||
// /////////////// // | ||||
r233 | ||||
|
r451 | /** | ||
|
r506 | * Merges into the array data an other array data. The two array datas must have the same number | ||
* of components so the merge can be done | ||||
|
r451 | * @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 | ||||
*/ | ||||
|
r506 | void add(const ArrayData<Dim> &other, bool prepend = false) | ||
r233 | { | |||
|
r372 | QWriteLocker locker{&m_Lock}; | ||
|
r506 | QReadLocker otherLocker{&other.m_Lock}; | ||
|
r642 | if (m_NbComponents != other.componentCount()) { | ||
|
r506 | return; | ||
} | ||||
|
r451 | |||
|
r642 | if (prepend) { | ||
auto otherDataSize = other.m_Data.size(); | ||||
m_Data.insert(m_Data.begin(), otherDataSize, 0.); | ||||
for (auto i = 0; i < otherDataSize; ++i) { | ||||
m_Data.replace(i, other.m_Data.at(i)); | ||||
|
r451 | } | ||
r233 | } | |||
|
r642 | else { | ||
m_Data.append(other.m_Data); | ||||
} | ||||
r233 | } | |||
|
r507 | void clear() | ||
{ | ||||
QWriteLocker locker{&m_Lock}; | ||||
|
r642 | m_Data.clear(); | ||
|
r507 | } | ||
|
r642 | int componentCount() const noexcept { return m_NbComponents; } | ||
|
r507 | |||
|
r502 | /// @return the size (i.e. number of values) of a single component | ||
/// @remarks in a case of a two-dimensional ArrayData, each component has the same size | ||||
r364 | int size() const | |||
r233 | { | |||
|
r372 | QReadLocker locker{&m_Lock}; | ||
|
r642 | return m_Data.size() / m_NbComponents; | ||
r233 | } | |||
|
r504 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation) | ||
|
r450 | { | ||
QReadLocker locker{&m_Lock}; | ||||
|
r642 | return arraydata_detail::Sort<Dim>::sort(m_Data, m_NbComponents, sortPermutation); | ||
|
r450 | } | ||
|
r454 | |||
|
r555 | // ///////// // | ||
// Iterators // | ||||
// ///////// // | ||||
|
r641 | ArrayDataIterator cbegin() const | ||
{ | ||||
return ArrayDataIterator{ArrayDataIteratorValue{ | ||||
std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, true)}}; | ||||
} | ||||
ArrayDataIterator cend() const | ||||
{ | ||||
return ArrayDataIterator{ArrayDataIteratorValue{ | ||||
std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, false)}}; | ||||
} | ||||
|
r555 | |||
|
r507 | // ///////////// // | ||
// 1-dim methods // | ||||
// ///////////// // | ||||
/** | ||||
* @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 | ||||
r364 | { | |||
|
r507 | QReadLocker locker{&m_Lock}; | ||
|
r642 | return m_Data.at(index); | ||
|
r507 | } | ||
r364 | ||||
|
r507 | /** | ||
* @return the data as a vector, as a const reference | ||||
* @remarks this method is only available for a unidimensional ArrayData | ||||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 1> > | ||||
const QVector<double> &cdata() const noexcept | ||||
{ | ||||
QReadLocker locker{&m_Lock}; | ||||
return m_Data.at(0); | ||||
} | ||||
/** | ||||
* @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> > | ||||
QVector<double> data() const noexcept | ||||
{ | ||||
QReadLocker locker{&m_Lock}; | ||||
return m_Data[0]; | ||||
|
r502 | } | ||
r233 | ||||
|
r568 | // ///////////// // | ||
// 2-dim methods // | ||||
// ///////////// // | ||||
/** | ||||
* @return the data | ||||
* @remarks this method is only available for a two-dimensional ArrayData | ||||
*/ | ||||
template <int D = Dim, typename = std::enable_if_t<D == 2> > | ||||
DataContainer data() const noexcept | ||||
{ | ||||
QReadLocker locker{&m_Lock}; | ||||
return m_Data; | ||||
} | ||||
|
r124 | private: | ||
|
r505 | DataContainer m_Data; | ||
|
r642 | /// Number of components (lines). Is always 1 in a 1-dim ArrayData | ||
int m_NbComponents; | ||||
r364 | mutable QReadWriteLock m_Lock; | |||
|
r124 | }; | ||
#endif // SCIQLOP_ARRAYDATA_H | ||||