##// END OF EJS Templates
Updates ArrayData iterator to be use single QVector
Updates ArrayData iterator to be use single QVector

File last commit:

r601:6cf9aea49f46
r601:6cf9aea49f46
Show More
ArrayData.h
298 lines | 9.1 KiB | text/x-c | CLexer
Alexandre Leroux
Creates ArrayData (struct that holds data as a vector of double)
r116 #ifndef SCIQLOP_ARRAYDATA_H
#define SCIQLOP_ARRAYDATA_H
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 #include "Data/ArrayDataIterator.h"
Alexandre Leroux
Adapts sort() method to 2-dim array data (1)...
r464 #include <Common/SortUtils.h>
Add current progression for thread fix
r336 #include <QReadLocker>
#include <QReadWriteLock>
Alexandre Leroux
Creates ArrayData (struct that holds data as a vector of double)
r116 #include <QVector>
Alexandre Leroux
Makes a Data series be sorted (2)...
r416
#include <memory>
Alexandre Leroux
Adapts sort() method to 2-dim array data (2)...
r465 template <int Dim>
class ArrayData;
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 using DataContainer = QVector<double>;
Alexandre Leroux
Adapts sort() method to 2-dim array data (2)...
r465
namespace arraydata_detail {
/// Struct used to sort ArrayData
template <int Dim>
struct Sort {
Alexandre Leroux
Updates sort() method to be compatible with a single vector
r600 static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, int nbComponents,
Alexandre Leroux
Adapts sort() method to 2-dim array data (2)...
r465 const std::vector<int> &sortPermutation)
{
Alexandre Leroux
Updates sort() method to be compatible with a single vector
r600 return std::make_shared<ArrayData<Dim> >(
SortUtils::sort(data, nbComponents, sortPermutation), nbComponents);
Alexandre Leroux
Adapts sort() method to 2-dim array data (2)...
r465 }
};
/// Specialization for uni-dimensional ArrayData
template <>
struct Sort<1> {
Alexandre Leroux
Updates sort() method to be compatible with a single vector
r600 static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, int nbComponents,
Alexandre Leroux
Adapts sort() method to 2-dim array data (2)...
r465 const std::vector<int> &sortPermutation)
{
Alexandre Leroux
Updates sort() method to be compatible with a single vector
r600 Q_UNUSED(nbComponents)
return std::make_shared<ArrayData<1> >(SortUtils::sort(data, 1, sortPermutation));
Alexandre Leroux
Adapts sort() method to 2-dim array data (2)...
r465 }
};
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 template <int Dim>
class IteratorValue : public ArrayDataIteratorValue::Impl {
public:
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
r601 explicit IteratorValue(const DataContainer &container, int nbComponents, bool begin)
: m_It{begin ? container.cbegin() : container.cend()}, m_NbComponents{nbComponents}
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 {
}
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);
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
r601 return std::tie(m_It, m_NbComponents) == std::tie(otherImpl.m_It, otherImpl.m_NbComponents);
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 }
catch (const std::bad_cast &) {
return false;
}
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
r601 void next() override { std::advance(m_It, m_NbComponents); }
void prev() override { std::advance(m_It, -m_NbComponents); }
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
r601 double at(int componentIndex) const override { return *(m_It + componentIndex); }
double first() const override { return *m_It; }
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 double min() const override
{
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
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
Replaces current iterator of ArrayData by the previous iterator created
r598 });
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
r601
return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 }
double max() const override
{
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
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
Replaces current iterator of ArrayData by the previous iterator created
r598 });
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
r601 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 }
private:
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
r601 std::vector<double> values() const
{
auto result = std::vector<double>{};
for (auto i = 0; i < m_NbComponents; ++i) {
result.push_back(*(m_It + i));
}
return result;
}
DataContainer::const_iterator m_It;
int m_NbComponents;
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 };
Alexandre Leroux
Adapts sort() method to 2-dim array data (2)...
r465 } // namespace arraydata_detail
Alexandre Leroux
Creates ArrayData (struct that holds data as a vector of double)
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
Creates ctor for two-dimensional ArrayData
r461 * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same
* number of values
Alexandre Leroux
Creates ArrayData (struct that holds data as a vector of double)
r116 *
* @tparam Dim the dimension of the ArrayData (one or two)
* @sa IDataSeries
*/
template <int Dim>
class ArrayData {
public:
Alexandre Leroux
Reorganizes methods in ArrayData...
r467 // ///// //
// Ctors //
// ///// //
Alexandre Leroux
Creates constructor for ScalarSeries that directly takes vectors...
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
Updates ArrayData to hold a single QVector
r599 explicit ArrayData(DataContainer data) : m_Data{std::move(data)}, m_NbComponents{1}
Alexandre Leroux
Creates constructor for ScalarSeries that directly takes vectors...
r361 {
}
Alexandre Leroux
Creates ctor for two-dimensional ArrayData
r461 /**
Alexandre Leroux
Updates ArrayData to hold a single QVector
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
Creates ctor for two-dimensional ArrayData
r461 * @param data the data the ArrayData will hold
Alexandre Leroux
Updates ArrayData to hold a single QVector
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
Creates ctor for two-dimensional ArrayData
r461 */
template <int D = Dim, typename = std::enable_if_t<D == 2> >
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 explicit ArrayData(DataContainer data, int nbComponents)
: m_Data{std::move(data)}, m_NbComponents{nbComponents}
Alexandre Leroux
Creates ctor for two-dimensional ArrayData
r461 {
if (nbComponents < 2) {
throw std::invalid_argument{
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 QString{"A multidimensional ArrayData must have at least 2 components (found: %1)"}
.arg(nbComponents)
Alexandre Leroux
Creates ctor for two-dimensional ArrayData
r461 .toStdString()};
}
Alexandre Leroux
Updates ArrayData to hold a single QVector
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
Creates ctor for two-dimensional ArrayData
r461 }
}
Alexandre Leroux
Review fixes
r344 /// Copy ctor
Add current progression for thread fix
r336 explicit ArrayData(const ArrayData &other)
{
Alexandre Leroux
Review fixes
r344 QReadLocker otherLocker{&other.m_Lock};
Add current progression for thread fix
r336 m_Data = other.m_Data;
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 m_NbComponents = other.m_NbComponents;
Add current progression for thread fix
r336 }
Alexandre Leroux
Reorganizes methods in ArrayData...
r467 // /////////////// //
// General methods //
// /////////////// //
Add merge API and implement it for the DataSeries
r217
Alexandre Leroux
Handles merge of two data series
r417 /**
Alexandre Leroux
Adapts add() method to 2-dim array data
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
Handles merge of two data series
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
Adapts add() method to 2-dim array data
r466 void add(const ArrayData<Dim> &other, bool prepend = false)
Add merge API and implement it for the DataSeries
r217 {
Alexandre Leroux
Review fixes
r344 QWriteLocker locker{&m_Lock};
Alexandre Leroux
Adapts add() method to 2-dim array data
r466 QReadLocker otherLocker{&other.m_Lock};
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 if (m_NbComponents != other.componentCount()) {
Alexandre Leroux
Adapts add() method to 2-dim array data
r466 return;
}
Alexandre Leroux
Handles merge of two data series
r417
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 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));
Alexandre Leroux
Handles merge of two data series
r417 }
Add merge API and implement it for the DataSeries
r217 }
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 else {
m_Data.append(other.m_Data);
}
Add merge API and implement it for the DataSeries
r217 }
Alexandre Leroux
Reorganizes methods in ArrayData...
r467 void clear()
{
QWriteLocker locker{&m_Lock};
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 m_Data.clear();
Alexandre Leroux
Reorganizes methods in ArrayData...
r467 }
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 int componentCount() const noexcept { return m_NbComponents; }
Alexandre Leroux
Reorganizes methods in ArrayData...
r467
Alexandre Leroux
Makes clear() and size() methods compatible with two-dimensional ArrayData
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
Add current progression for thread fix
r336 int size() const
Add merge API and implement it for the DataSeries
r217 {
Alexandre Leroux
Review fixes
r344 QReadLocker locker{&m_Lock};
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 return m_Data.size() / m_NbComponents;
Add merge API and implement it for the DataSeries
r217 }
Alexandre Leroux
Adapts sort() method to 2-dim array data (1)...
r464 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation)
Alexandre Leroux
Makes a Data series be sorted (2)...
r416 {
QReadLocker locker{&m_Lock};
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 return arraydata_detail::Sort<Dim>::sort(m_Data, m_NbComponents, sortPermutation);
Alexandre Leroux
Makes a Data series be sorted (2)...
r416 }
Alexandre Leroux
Correction on ArrayData::clear() method
r420
Alexandre Leroux
Creates iterator for ArrayData
r523 // ///////// //
// Iterators //
// ///////// //
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 ArrayDataIterator cbegin() const
{
return ArrayDataIterator{ArrayDataIteratorValue{
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
r601 std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, m_NbComponents, true)}};
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 }
ArrayDataIterator cend() const
{
Alexandre Leroux
Updates ArrayData iterator to be use single QVector
r601 return ArrayDataIterator{
ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim> >(
m_Data, m_NbComponents, false)}};
Alexandre Leroux
Replaces current iterator of ArrayData by the previous iterator created
r598 }
Alexandre Leroux
Creates iterator for ArrayData
r523
Alexandre Leroux
Reorganizes methods in ArrayData...
r467 // ///////////// //
// 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
Add current progression for thread fix
r336 {
Alexandre Leroux
Reorganizes methods in ArrayData...
r467 QReadLocker locker{&m_Lock};
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 return m_Data.at(index);
Alexandre Leroux
Reorganizes methods in ArrayData...
r467 }
Add current progression for thread fix
r336
Alexandre Leroux
Reorganizes methods in ArrayData...
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> >
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];
Alexandre Leroux
Makes clear() and size() methods compatible with two-dimensional ArrayData
r462 }
Add merge API and implement it for the DataSeries
r217
Alexandre Leroux
Adds unit tests for reading vectors in AMDA
r535 // ///////////// //
// 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;
}
Alexandre Leroux
Creates ArrayData (struct that holds data as a vector of double)
r116 private:
Alexandre Leroux
Adapts sort() method to 2-dim array data (2)...
r465 DataContainer m_Data;
Alexandre Leroux
Updates ArrayData to hold a single QVector
r599 /// Number of components (lines). Is always 1 in a 1-dim ArrayData
int m_NbComponents;
Add current progression for thread fix
r336 mutable QReadWriteLock m_Lock;
Alexandre Leroux
Creates ArrayData (struct that holds data as a vector of double)
r116 };
#endif // SCIQLOP_ARRAYDATA_H