PIVector reshape... not works

This commit is contained in:
Andrey
2022-04-19 16:33:13 +03:00
parent 8c6db321cf
commit d4294e3d95
3 changed files with 66 additions and 46 deletions

View File

@@ -211,14 +211,6 @@ public:
return *this;
}
//! \~\brief
//! \~english Reshape order enum for \a reshape() function.
//! \~russian Порядок обхода для функции изменения размерности \a reshape().
enum ReshapeOrder {
byRow,
byColumn
};
class iterator {
friend class PIVector<T>;
private:
@@ -1939,22 +1931,36 @@ public:
return ret;
}
inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols, ReshapeOrder order = byRow) const {
PIVector<PIVector<T>> ret;
if (isEmpty()) return ret;
//! \brief
//! \~english Changes the dimension of the array, creates a two-dimensional array from a one-dimensional array.
//! \~russian Изменяет размерность массива, из одномерного массива создает двухмерный.
//! \~\details
//! \~russian
//! \~english
//! \~\code
//! PIVector<int> v{1, 2, 3, 4};
//! PIVector<PIVector<int>> m1 = v.reshape(2,2);
//! piCout << m1; // {{1, 2}, {3, 4}}
//! PIVector<PIVector<int>> m2 = v.reshape(2,2, ReshapeByColumn);
//! piCout << m2; // {{1, 3}, {2, 4}}
//! \endcode
//! \~\sa \a map(), \a reduce()
inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols, ReshapeOrder order = ReshapeByRow) const {
#ifndef NDEBUG
if (rows*cols != piv_size) {
printf("error with PIVector<%s>::reshape\n", __PIP_TYPENAME__(T));
}
#endif
assert(rows*cols == piv_size);
PIVector<PIVector<T>> ret;
if (isEmpty()) return ret;
ret.resize(rows);
if (order == byRow) {
if (order == ReshapeByRow) {
for (size_t r = 0; r < rows; r++) {
ret[r] = PIVector<T>(&(piv_data[r*cols]), cols);
}
}
if (order == byColumn) {
if (order == ReshapeByColumn) {
for (size_t r = 0; r < rows; r++) {
ret[r].resize(cols);
for (size_t c = 0; c < cols; c++) {
@@ -1968,18 +1974,18 @@ public:
template<typename C, typename std::enable_if<
std::is_same<T, PIVector<C>>::value
, int>::type = 0>
inline PIVector<C> reshape(ReshapeOrder order = byRow) const {
inline PIVector<C> flat(ReshapeOrder order = ReshapeByRow) const {
PIVector<C> ret;
if (isEmpty()) return ret;
size_t rows = size();
size_t cols = at(0).size();
ret.reserve(rows * cols);
if (order == byRow) {
if (order == ReshapeByRow) {
for (size_t r = 0; r < rows; r++) {
ret.append(at(r));
}
}
if (order == byColumn) {
if (order == ReshapeByColumn) {
for (size_t c = 0; c < cols; c++) {
for (size_t r = 0; r < rows; r++) {
ret << at(r)[c];
@@ -1990,6 +1996,37 @@ public:
return ret;
}
template<typename C, typename std::enable_if<
std::is_same<T, PIVector<C>>::value
, int>::type = 0>
inline PIVector<PIVector<C>> reshape(size_t rows, size_t cols, ReshapeOrder order = ReshapeByRow) const {
#ifndef NDEBUG
if (rows*cols != piv_size) {
printf("error with PIVector<%s>::reshape\n", __PIP_TYPENAME__(T));
}
#endif
assert(rows*cols == piv_size);
PIVector<PIVector<C>> ret;
if (isEmpty()) return ret;
// size_t old_rows = size();
// size_t old_cols = at(0).size();
// ret.resize(rows);
// if (order == ReshapeByRow) {
// for (size_t r = 0; r < rows; r++) {
// ret.append(at(r));
// }
// }
// if (order == ReshapeByColumn) {
// for (size_t c = 0; c < cols; c++) {
// for (size_t r = 0; r < rows; r++) {
// ret << at(r)[c];
// }
// }
// }
// ret.resize(rows * cols);
return ret;
}
private:
inline void _reset() {piv_size = piv_rsize = 0; piv_data = 0;}
inline size_t asize(size_t s) {
@@ -2086,7 +2123,6 @@ private:
};
#ifdef PIP_STD_IOSTREAM
//! \~english Output operator to [std::ostream](https://en.cppreference.com/w/cpp/io/basic_ostream).
//! \~russian Оператор вывода в [std::ostream](https://ru.cppreference.com/w/cpp/io/basic_ostream).
@@ -2124,5 +2160,4 @@ inline PICout operator <<(PICout s, const PIVector<T> & v) {
template<typename T>
inline void piSwap(PIVector<T> & f, PIVector<T> & s) {f.swap(s);}
#endif // PIVECTOR_H