ArrayData.h
374 lines
| 11.6 KiB
| text/x-c
|
CLexer
Alexandre Leroux
|
r116 | #ifndef SCIQLOP_ARRAYDATA_H | ||
#define SCIQLOP_ARRAYDATA_H | ||||
Alexandre Leroux
|
r598 | #include "Data/ArrayDataIterator.h" | ||
Alexandre Leroux
|
r464 | #include <Common/SortUtils.h> | ||
r336 | #include <QReadLocker> | |||
#include <QReadWriteLock> | ||||
Alexandre Leroux
|
r116 | #include <QVector> | ||
Alexandre Leroux
|
r416 | |||
#include <memory> | ||||
Alexandre Leroux
|
r465 | template <int Dim> | ||
class ArrayData; | ||||
Alexandre Leroux
|
r644 | using DataContainer = std::vector<double>; | ||
Alexandre Leroux
|
r465 | |||
namespace arraydata_detail { | ||||
/// Struct used to sort ArrayData | ||||
template <int Dim> | ||||
struct Sort { | ||||
Alexandre Leroux
|
r600 | static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, int nbComponents, | ||
Alexandre Leroux
|
r465 | const std::vector<int> &sortPermutation) | ||
{ | ||||
Alexandre Leroux
|
r600 | return std::make_shared<ArrayData<Dim> >( | ||
SortUtils::sort(data, nbComponents, sortPermutation), nbComponents); | ||||
Alexandre Leroux
|
r465 | } | ||
}; | ||||
/// Specialization for uni-dimensional ArrayData | ||||
template <> | ||||
struct Sort<1> { | ||||
Alexandre Leroux
|
r600 | static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, int nbComponents, | ||
Alexandre Leroux
|
r465 | const std::vector<int> &sortPermutation) | ||
{ | ||||
Alexandre Leroux
|
r600 | Q_UNUSED(nbComponents) | ||
return std::make_shared<ArrayData<1> >(SortUtils::sort(data, 1, sortPermutation)); | ||||
Alexandre Leroux
|
r465 | } | ||
}; | ||||
Alexandre Leroux
|
r626 | template <int Dim, bool IsConst> | ||
class IteratorValue; | ||||
template <int Dim, bool IsConst> | ||||
struct IteratorValueBuilder { | ||||
}; | ||||
template <int Dim> | ||||
struct IteratorValueBuilder<Dim, true> { | ||||
using DataContainerIterator = DataContainer::const_iterator; | ||||
Alexandre Leroux
|
r627 | |||
static void swap(IteratorValue<Dim, true> &o1, IteratorValue<Dim, true> &o2) {} | ||||
Alexandre Leroux
|
r626 | }; | ||
Alexandre Leroux
|
r598 | template <int Dim> | ||
Alexandre Leroux
|
r626 | struct IteratorValueBuilder<Dim, false> { | ||
using DataContainerIterator = DataContainer::iterator; | ||||
Alexandre Leroux
|
r627 | |||
static void swap(IteratorValue<Dim, false> &o1, IteratorValue<Dim, false> &o2) | ||||
{ | ||||
for (auto i = 0; i < o1.m_NbComponents; ++i) { | ||||
std::iter_swap(o1.m_It + i, o2.m_It + i); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r626 | }; | ||
template <int Dim, bool IsConst> | ||||
Alexandre Leroux
|
r598 | class IteratorValue : public ArrayDataIteratorValue::Impl { | ||
public: | ||||
Alexandre Leroux
|
r626 | friend class ArrayData<Dim>; | ||
friend class IteratorValueBuilder<Dim, IsConst>; | ||||
using DataContainerIterator = | ||||
typename IteratorValueBuilder<Dim, IsConst>::DataContainerIterator; | ||||
template <bool IC = IsConst, typename = std::enable_if_t<IC == true> > | ||||
Alexandre Leroux
|
r601 | explicit IteratorValue(const DataContainer &container, int nbComponents, bool begin) | ||
: m_It{begin ? container.cbegin() : container.cend()}, m_NbComponents{nbComponents} | ||||
Alexandre Leroux
|
r598 | { | ||
} | ||||
Alexandre Leroux
|
r626 | template <bool IC = IsConst, typename = std::enable_if_t<IC == false> > | ||
explicit IteratorValue(DataContainer &container, int nbComponents, bool begin) | ||||
: m_It{begin ? container.begin() : container.end()}, m_NbComponents{nbComponents} | ||||
{ | ||||
} | ||||
Alexandre Leroux
|
r598 | IteratorValue(const IteratorValue &other) = default; | ||
std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override | ||||
{ | ||||
Alexandre Leroux
|
r626 | return std::make_unique<IteratorValue<Dim, IsConst> >(*this); | ||
Alexandre Leroux
|
r598 | } | ||
Alexandre Leroux
|
r640 | int distance(const ArrayDataIteratorValue::Impl &other) const override try { | ||
/// @todo ALX : validate | ||||
const auto &otherImpl = dynamic_cast<const IteratorValue &>(other); | ||||
return std::distance(otherImpl.m_It, m_It) / m_NbComponents; | ||||
} | ||||
catch (const std::bad_cast &) { | ||||
return 0; | ||||
} | ||||
Alexandre Leroux
|
r598 | bool equals(const ArrayDataIteratorValue::Impl &other) const override try { | ||
const auto &otherImpl = dynamic_cast<const IteratorValue &>(other); | ||||
Alexandre Leroux
|
r601 | return std::tie(m_It, m_NbComponents) == std::tie(otherImpl.m_It, otherImpl.m_NbComponents); | ||
Alexandre Leroux
|
r598 | } | ||
catch (const std::bad_cast &) { | ||||
return false; | ||||
} | ||||
Alexandre Leroux
|
r640 | bool lowerThan(const ArrayDataIteratorValue::Impl &other) const override try { | ||
const auto &otherImpl = dynamic_cast<const IteratorValue &>(other); | ||||
return m_It < otherImpl.m_It; | ||||
} | ||||
catch (const std::bad_cast &) { | ||||
return false; | ||||
} | ||||
std::unique_ptr<ArrayDataIteratorValue::Impl> advance(int offset) const override | ||||
{ | ||||
auto result = clone(); | ||||
Alexandre Leroux
|
r647 | result->next(offset); | ||
Alexandre Leroux
|
r640 | return result; | ||
} | ||||
Alexandre Leroux
|
r647 | void next(int offset) override { std::advance(m_It, offset * m_NbComponents); } | ||
Alexandre Leroux
|
r601 | void prev() override { std::advance(m_It, -m_NbComponents); } | ||
Alexandre Leroux
|
r598 | |||
Alexandre Leroux
|
r601 | double at(int componentIndex) const override { return *(m_It + componentIndex); } | ||
double first() const override { return *m_It; } | ||||
Alexandre Leroux
|
r598 | double min() const override | ||
{ | ||||
Alexandre Leroux
|
r601 | auto values = this->values(); | ||
auto end = values.cend(); | ||||
auto it = std::min_element(values.cbegin(), end, [](const auto &v1, const auto &v2) { | ||||
return SortUtils::minCompareWithNaN(v1, v2); | ||||
Alexandre Leroux
|
r598 | }); | ||
Alexandre Leroux
|
r601 | |||
return it != end ? *it : std::numeric_limits<double>::quiet_NaN(); | ||||
Alexandre Leroux
|
r598 | } | ||
double max() const override | ||||
{ | ||||
Alexandre Leroux
|
r601 | auto values = this->values(); | ||
auto end = values.cend(); | ||||
auto it = std::max_element(values.cbegin(), end, [](const auto &v1, const auto &v2) { | ||||
return SortUtils::maxCompareWithNaN(v1, v2); | ||||
Alexandre Leroux
|
r598 | }); | ||
Alexandre Leroux
|
r601 | return it != end ? *it : std::numeric_limits<double>::quiet_NaN(); | ||
Alexandre Leroux
|
r598 | } | ||
Alexandre Leroux
|
r621 | QVector<double> values() const override | ||
Alexandre Leroux
|
r601 | { | ||
Alexandre Leroux
|
r621 | auto result = QVector<double>{}; | ||
Alexandre Leroux
|
r601 | for (auto i = 0; i < m_NbComponents; ++i) { | ||
result.push_back(*(m_It + i)); | ||||
} | ||||
return result; | ||||
} | ||||
Alexandre Leroux
|
r627 | void swap(ArrayDataIteratorValue::Impl &other) override | ||
{ | ||||
auto &otherImpl = dynamic_cast<IteratorValue &>(other); | ||||
IteratorValueBuilder<Dim, IsConst>::swap(*this, otherImpl); | ||||
} | ||||
Alexandre Leroux
|
r621 | private: | ||
Alexandre Leroux
|
r626 | DataContainerIterator m_It; | ||
Alexandre Leroux
|
r601 | int m_NbComponents; | ||
Alexandre Leroux
|
r598 | }; | ||
Alexandre Leroux
|
r465 | } // namespace arraydata_detail | ||
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
|
r467 | // ///// // | ||
// Ctors // | ||||
// ///// // | ||||
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> > | ||||
Alexandre Leroux
|
r599 | explicit ArrayData(DataContainer data) : m_Data{std::move(data)}, m_NbComponents{1} | ||
Alexandre Leroux
|
r361 | { | ||
} | ||||
Alexandre Leroux
|
r461 | /** | ||
Alexandre Leroux
|
r599 | * 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 | ||||
Alexandre Leroux
|
r461 | * @param data the data the ArrayData will hold | ||
Alexandre Leroux
|
r599 | * @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 | ||||
Alexandre Leroux
|
r461 | */ | ||
template <int D = Dim, typename = std::enable_if_t<D == 2> > | ||||
Alexandre Leroux
|
r599 | explicit ArrayData(DataContainer data, int nbComponents) | ||
: m_Data{std::move(data)}, m_NbComponents{nbComponents} | ||||
Alexandre Leroux
|
r461 | { | ||
if (nbComponents < 2) { | ||||
throw std::invalid_argument{ | ||||
Alexandre Leroux
|
r599 | QString{"A multidimensional ArrayData must have at least 2 components (found: %1)"} | ||
.arg(nbComponents) | ||||
Alexandre Leroux
|
r461 | .toStdString()}; | ||
} | ||||
Alexandre Leroux
|
r599 | 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()}; | ||||
Alexandre Leroux
|
r461 | } | ||
} | ||||
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
|
r599 | m_NbComponents = other.m_NbComponents; | ||
r336 | } | |||
Alexandre Leroux
|
r467 | // /////////////// // | ||
// General methods // | ||||
// /////////////// // | ||||
r217 | ||||
Alexandre Leroux
|
r417 | /** | ||
Alexandre Leroux
|
r466 | * 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 | ||||
Alexandre Leroux
|
r417 | * @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 | ||||
*/ | ||||
Alexandre Leroux
|
r466 | void add(const ArrayData<Dim> &other, bool prepend = false) | ||
r217 | { | |||
Alexandre Leroux
|
r344 | QWriteLocker locker{&m_Lock}; | ||
Alexandre Leroux
|
r466 | QReadLocker otherLocker{&other.m_Lock}; | ||
Alexandre Leroux
|
r599 | if (m_NbComponents != other.componentCount()) { | ||
Alexandre Leroux
|
r466 | return; | ||
} | ||||
Alexandre Leroux
|
r417 | |||
Alexandre Leroux
|
r646 | insert(other.cbegin(), other.cend(), prepend); | ||
r217 | } | |||
Alexandre Leroux
|
r467 | void clear() | ||
{ | ||||
QWriteLocker locker{&m_Lock}; | ||||
Alexandre Leroux
|
r599 | m_Data.clear(); | ||
Alexandre Leroux
|
r467 | } | ||
Alexandre Leroux
|
r599 | int componentCount() const noexcept { return m_NbComponents; } | ||
Alexandre Leroux
|
r467 | |||
Alexandre Leroux
|
r462 | /// @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 | ||||
r336 | int size() const | |||
r217 | { | |||
Alexandre Leroux
|
r344 | QReadLocker locker{&m_Lock}; | ||
Alexandre Leroux
|
r599 | return m_Data.size() / m_NbComponents; | ||
r217 | } | |||
Alexandre Leroux
|
r660 | /// @return the total size (i.e. number of values) of the array data | ||
int totalSize() const | ||||
{ | ||||
QReadLocker locker{&m_Lock}; | ||||
return m_Data.size(); | ||||
} | ||||
Alexandre Leroux
|
r464 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation) | ||
Alexandre Leroux
|
r416 | { | ||
QReadLocker locker{&m_Lock}; | ||||
Alexandre Leroux
|
r599 | return arraydata_detail::Sort<Dim>::sort(m_Data, m_NbComponents, sortPermutation); | ||
Alexandre Leroux
|
r416 | } | ||
Alexandre Leroux
|
r420 | |||
Alexandre Leroux
|
r523 | // ///////// // | ||
// Iterators // | ||||
// ///////// // | ||||
Alexandre Leroux
|
r626 | ArrayDataIterator begin() | ||
{ | ||||
return ArrayDataIterator{ | ||||
ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, false> >( | ||||
m_Data, m_NbComponents, true)}}; | ||||
} | ||||
ArrayDataIterator end() | ||||
{ | ||||
return ArrayDataIterator{ | ||||
ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, false> >( | ||||
m_Data, m_NbComponents, false)}}; | ||||
} | ||||
Alexandre Leroux
|
r598 | ArrayDataIterator cbegin() const | ||
{ | ||||
Alexandre Leroux
|
r626 | return ArrayDataIterator{ | ||
ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, true> >( | ||||
m_Data, m_NbComponents, true)}}; | ||||
Alexandre Leroux
|
r598 | } | ||
Alexandre Leroux
|
r626 | |||
Alexandre Leroux
|
r598 | ArrayDataIterator cend() const | ||
{ | ||||
Alexandre Leroux
|
r601 | return ArrayDataIterator{ | ||
Alexandre Leroux
|
r626 | ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, true> >( | ||
Alexandre Leroux
|
r601 | m_Data, m_NbComponents, false)}}; | ||
Alexandre Leroux
|
r598 | } | ||
Alexandre Leroux
|
r523 | |||
Alexandre Leroux
|
r628 | void erase(ArrayDataIterator first, ArrayDataIterator last) | ||
{ | ||||
auto firstImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, false> *>(first->impl()); | ||||
auto lastImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, false> *>(last->impl()); | ||||
if (firstImpl && lastImpl) { | ||||
m_Data.erase(firstImpl->m_It, lastImpl->m_It); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r646 | void insert(ArrayDataIterator first, ArrayDataIterator last, bool prepend = false) | ||
Alexandre Leroux
|
r623 | { | ||
Alexandre Leroux
|
r646 | auto firstImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, true> *>(first->impl()); | ||
auto lastImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, true> *>(last->impl()); | ||||
if (firstImpl && lastImpl) { | ||||
auto insertIt = prepend ? m_Data.begin() : m_Data.end(); | ||||
m_Data.insert(insertIt, firstImpl->m_It, lastImpl->m_It); | ||||
} | ||||
Alexandre Leroux
|
r623 | } | ||
Alexandre Leroux
|
r467 | |||
/** | ||||
* @return the data at a specified index | ||||
* @remarks index must be a valid position | ||||
*/ | ||||
double at(int index) const noexcept | ||||
r336 | { | |||
Alexandre Leroux
|
r467 | QReadLocker locker{&m_Lock}; | ||
Alexandre Leroux
|
r599 | return m_Data.at(index); | ||
Alexandre Leroux
|
r467 | } | ||
r336 | ||||
Alexandre Leroux
|
r602 | // ///////////// // | ||
// 1-dim methods // | ||||
// ///////////// // | ||||
Alexandre Leroux
|
r467 | /** | ||
* @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> > | ||||
Alexandre Leroux
|
r644 | DataContainer cdata() const noexcept | ||
Alexandre Leroux
|
r535 | { | ||
return m_Data; | ||||
} | ||||
Alexandre Leroux
|
r116 | private: | ||
Alexandre Leroux
|
r465 | DataContainer m_Data; | ||
Alexandre Leroux
|
r599 | /// Number of components (lines). Is always 1 in a 1-dim ArrayData | ||
int m_NbComponents; | ||||
r336 | mutable QReadWriteLock m_Lock; | |||
Alexandre Leroux
|
r116 | }; | ||
#endif // SCIQLOP_ARRAYDATA_H | ||||