ReshapeOrder for reshape() methods
Last PIVector features ported to PIDeque
This commit is contained in:
@@ -53,6 +53,10 @@ public:
|
||||
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
|
||||
resize(piv_size, f);
|
||||
}
|
||||
inline PIVector(size_t piv_size, std::function<T(size_t)> f): piv_data(0), piv_size(0), piv_rsize(0) {
|
||||
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
|
||||
resize(piv_size, f);
|
||||
}
|
||||
inline PIVector(PIVector<T> && other): piv_data(other.piv_data), piv_size(other.piv_size), piv_rsize(other.piv_rsize) {
|
||||
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
|
||||
other._reset();
|
||||
@@ -81,6 +85,11 @@ public:
|
||||
|
||||
typedef T value_type;
|
||||
|
||||
enum ReshapeOrder {
|
||||
byRow,
|
||||
byColumn
|
||||
};
|
||||
|
||||
class iterator {
|
||||
friend class PIVector<T>;
|
||||
private:
|
||||
@@ -470,12 +479,21 @@ public:
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols) const {
|
||||
assert(rows*cols == piv_size);
|
||||
inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols, int order = byRow) const {
|
||||
PIVector<PIVector<T>> ret;
|
||||
if (isEmpty()) return ret;
|
||||
assert(rows*cols == piv_size);
|
||||
ret.resize(rows);
|
||||
for (size_t i = 0; i < rows; i++) {
|
||||
ret[i] = PIVector<T>(&(piv_data[i*cols]), cols);
|
||||
if (order == byRow) {
|
||||
for (size_t r = 0; r < rows; r++)
|
||||
ret[r] = PIVector<T>(&(piv_data[r*cols]), cols);
|
||||
}
|
||||
if (order == byColumn) {
|
||||
for (size_t r = 0; r < rows; r++) {
|
||||
ret[r].resize(cols);
|
||||
for (size_t c = 0; c < cols; c++)
|
||||
ret[r][c] = piv_data[c*rows + r];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -483,17 +501,22 @@ public:
|
||||
template<typename C, typename std::enable_if<
|
||||
std::is_same<T, PIVector<C>>::value
|
||||
, int>::type = 0>
|
||||
inline PIVector<C> reshape() const {
|
||||
inline PIVector<C> reshape(int order = byRow) const {
|
||||
PIVector<C> ret;
|
||||
if (isEmpty()) return ret;
|
||||
size_t rows = size();
|
||||
if (rows) {
|
||||
size_t cols = at(0).size();
|
||||
ret.reserve(rows * cols);
|
||||
for (size_t i = 0; i < rows; i++) {
|
||||
ret.append(at(i));
|
||||
}
|
||||
ret.resize(rows * cols);
|
||||
size_t cols = at(0).size();
|
||||
ret.reserve(rows * cols);
|
||||
if (order == byRow) {
|
||||
for (size_t r = 0; r < rows; r++)
|
||||
ret.append(at(r));
|
||||
}
|
||||
if (order == byColumn) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user