##// END OF EJS Templates
Adapts sort() method to 2-dim array data (2)...
Adapts sort() method to 2-dim array data (2) Makes sort method for 2-dim array data, and specializes the method for 1-dim array data with the existing function

File last commit:

r465:c4a0c9f4308e
r465:c4a0c9f4308e
Show More
ArrayData.h
201 lines | 6.0 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
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;
using DataContainer = QVector<QVector<double> >;
namespace arraydata_detail {
/// Struct used to sort ArrayData
template <int Dim>
struct Sort {
static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data,
const std::vector<int> &sortPermutation)
{
auto nbComponents = data.size();
auto sortedData = DataContainer(nbComponents);
for (auto i = 0; i < nbComponents; ++i) {
sortedData[i] = SortUtils::sort(data.at(i), sortPermutation);
}
return std::make_shared<ArrayData<Dim> >(std::move(sortedData));
}
};
/// Specialization for uni-dimensional ArrayData
template <>
struct Sort<1> {
static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data,
const std::vector<int> &sortPermutation)
{
return std::make_shared<ArrayData<1> >(SortUtils::sort(data.at(0), sortPermutation));
}
};
} // 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
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> >
explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}}
{
m_Data[0] = std::move(data);
}
Alexandre Leroux
Creates ctor for two-dimensional ArrayData
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> >
Alexandre Leroux
Adapts sort() method to 2-dim array data (2)...
r465 explicit ArrayData(DataContainer data)
Alexandre Leroux
Creates ctor for two-dimensional ArrayData
r461 {
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 {
Alexandre Leroux
Adapts sort() method to 2-dim array data (2)...
r465 m_Data = DataContainer{nbComponents, QVector<double>{}};
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
Makes a Data series be sorted (2)...
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
Creates ArrayData (struct that holds data as a vector of double)
r116 /**
* @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> >
Add current progression for thread fix
r336 QVector<double> data() const noexcept
Alexandre Leroux
Creates ArrayData (struct that holds data as a vector of double)
r116 {
Alexandre Leroux
Review fixes
r344 QReadLocker locker{&m_Lock};
Update ArrayData to make it better
r272 return m_Data[0];
Alexandre Leroux
Creates ArrayData (struct that holds data as a vector of double)
r116 }
Add merge API and implement it for the DataSeries
r217 /**
Alexandre Leroux
Makes a Data series be sorted (1)
r415 * @return the data as a vector, as a const reference
Add merge API and implement it for the DataSeries
r217 * @remarks this method is only available for a unidimensional ArrayData
*/
template <int D = Dim, typename = std::enable_if_t<D == 1> >
Alexandre Leroux
Makes a Data series be sorted (1)
r415 const QVector<double> &cdata() const noexcept
Add merge API and implement it for the DataSeries
r217 {
Alexandre Leroux
Review fixes
r344 QReadLocker locker{&m_Lock};
Alexandre Leroux
Makes a Data series be sorted (1)
r415 return m_Data.at(0);
Add merge API and implement it for the DataSeries
r217 }
Alexandre Leroux
Handles merge of two data series
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
*/
Add merge API and implement it for the DataSeries
r217 template <int D = Dim, typename = std::enable_if_t<D == 1> >
Alexandre Leroux
Handles merge of two data series
r417 void add(const ArrayData<1> &other, bool prepend = false)
Add merge API and implement it for the DataSeries
r217 {
Alexandre Leroux
Review fixes
r344 QWriteLocker locker{&m_Lock};
Add merge API and implement it for the DataSeries
r217 if (!m_Data.empty()) {
Alexandre Leroux
Handles merge of two data series
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();
}
Add merge API and implement it for the DataSeries
r217 }
}
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};
Add merge API and implement it for the DataSeries
r217 return m_Data[0].size();
}
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
Adapts sort() method to 2-dim array data (2)...
r465 return arraydata_detail::Sort<Dim>::sort(m_Data, sortPermutation);
Alexandre Leroux
Makes a Data series be sorted (2)...
r416 }
Alexandre Leroux
Correction on ArrayData::clear() method
r420
Add current progression for thread fix
r336 void clear()
{
Alexandre Leroux
Review fixes
r344 QWriteLocker locker{&m_Lock};
Add current progression for thread fix
r336
Alexandre Leroux
Makes clear() and size() methods compatible with two-dimensional ArrayData
r462 auto nbComponents = m_Data.size();
for (auto i = 0; i < nbComponents; ++i) {
m_Data[i].clear();
}
}
Add merge API and implement it for the DataSeries
r217
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;
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