finish PIVector doc

This commit is contained in:
Andrey
2022-04-20 16:50:36 +03:00
parent d4294e3d95
commit 8beaac5193
2 changed files with 49 additions and 30 deletions

View File

@@ -1936,7 +1936,13 @@ public:
//! \~russian Изменяет размерность массива, из одномерного массива создает двухмерный. //! \~russian Изменяет размерность массива, из одномерного массива создает двухмерный.
//! \~\details //! \~\details
//! \~russian //! \~russian
//! \param rows размер внешнего массива
//! \param cols размер внутренних массивов
//! \param order порядок обхода исходного массива, задаётся с помощью \a ReshapeOrder
//! \~english //! \~english
//! \param rows size external array
//! \param cols size internal arrays
//! \param order the order of traversing the source array is set using \a ReshapeOrder
//! \~\code //! \~\code
//! PIVector<int> v{1, 2, 3, 4}; //! PIVector<int> v{1, 2, 3, 4};
//! PIVector<PIVector<int>> m1 = v.reshape(2,2); //! PIVector<PIVector<int>> m1 = v.reshape(2,2);
@@ -1944,7 +1950,7 @@ public:
//! PIVector<PIVector<int>> m2 = v.reshape(2,2, ReshapeByColumn); //! PIVector<PIVector<int>> m2 = v.reshape(2,2, ReshapeByColumn);
//! piCout << m2; // {{1, 3}, {2, 4}} //! piCout << m2; // {{1, 3}, {2, 4}}
//! \endcode //! \endcode
//! \~\sa \a map(), \a reduce() //! \~\sa \a map(), \a reduce(), \a flatten()
inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols, ReshapeOrder order = ReshapeByRow) const { inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols, ReshapeOrder order = ReshapeByRow) const {
#ifndef NDEBUG #ifndef NDEBUG
if (rows*cols != piv_size) { if (rows*cols != piv_size) {
@@ -1971,10 +1977,25 @@ public:
return ret; return ret;
} }
//! \brief
//! \~english Changes the dimension of the array, creates a one-dimensional array from a two-dimensional array.
//! \~russian Изменяет размерность массива, из двухмерный массива создает одномерный.
//! \~\details
//! \~russian Делает массив плоским.
//! Порядок обхода исходного массива, задаётся с помощью \a ReshapeOrder.
//! \~english Makes the array flat.
//! Еhe order of traversing the source array is set using \a ReshapeOrder.
//! \~\code
//! PIVector<int> v{1, 2, 3, 4, 5, 6};
//! PIVector<PIVector<int>> xv = v.reshape(3,2);
//! piCout << xv; // {{1, 2}, {3, 4}, {5, 6}}
//! piCout << xv.flatten<int>(); // {1, 2, 3, 4, 5, 6}
//! \endcode
//! \~\sa \a map(), \a reduce(), \a reshape()
template<typename C, typename std::enable_if< template<typename C, typename std::enable_if<
std::is_same<T, PIVector<C>>::value std::is_same<T, PIVector<C>>::value
, int>::type = 0> , int>::type = 0>
inline PIVector<C> flat(ReshapeOrder order = ReshapeByRow) const { inline PIVector<C> flatten(ReshapeOrder order = ReshapeByRow) const {
PIVector<C> ret; PIVector<C> ret;
if (isEmpty()) return ret; if (isEmpty()) return ret;
size_t rows = size(); size_t rows = size();
@@ -1996,35 +2017,31 @@ public:
return ret; return ret;
} }
//! \brief
//! \~english Changes the dimension of the two-dimensional array.
//! \~russian Изменяет размерность двухмерного массива.
//! \~\details
//! \~russian
//! \param rows размер внешнего массива
//! \param cols размер внутренних массивов
//! \param order порядок обхода исходного массива, задаётся с помощью \a ReshapeOrder
//! \~english
//! \param rows size external array
//! \param cols size internal arrays
//! \param order the order of traversing the source array is set using \a ReshapeOrder
//! \~\code
//! PIVector<int> v{1, 2, 3, 4, 5, 6};
//! PIVector<PIVector<int>> xv = v.reshape(3,2);
//! piCout << xv; // {{1, 2}, {3, 4}, {5, 6}}
//! piCout << xv.reshape<int>(2,3); // {{1, 2, 3}, {4, 5, 6}}
//! \endcode
//! \~\sa \a map(), \a reduce(), \a reshape()
template<typename C, typename std::enable_if< template<typename C, typename std::enable_if<
std::is_same<T, PIVector<C>>::value std::is_same<T, PIVector<C>>::value
, int>::type = 0> , int>::type = 0>
inline PIVector<PIVector<C>> reshape(size_t rows, size_t cols, ReshapeOrder order = ReshapeByRow) const { inline PIVector<PIVector<C>> reshape(size_t rows, size_t cols, ReshapeOrder order = ReshapeByRow) const {
#ifndef NDEBUG PIVector<C> fl = flatten<C>();
if (rows*cols != piv_size) { return fl.reshape(rows, cols, order);
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: private:

View File

@@ -62,8 +62,10 @@ PICout operator <<(PICout s, const ConstChars & v) {
} }
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
PIVector<int> v{1, 2, 3, 4}; PIVector<int> v{1, 2, 3, 4, 5, 6};
PIVector<PIVector<int>> v2 = v.reshape(2,2, ReshapeByRow); PIVector<PIVector<int>> xv = v.reshape(3,2);
piCout << v2; piCout << xv;
piCout << xv.flatten<int>();
piCout << xv.reshape<int>(2,3);
return 0; return 0;
} }