PIVector reshape and etc...

This commit is contained in:
2021-07-16 14:11:55 +03:00
parent 040eb3b279
commit 856a9b80ea
5 changed files with 76 additions and 236 deletions

View File

@@ -233,6 +233,13 @@ public:
elementNew(piv_data + i, f);
return *this;
}
inline PIVector<T> & fill(std::function<T(size_t)> f) {
deleteT(piv_data, piv_size);
PIINTROSPECTION_CONTAINER_USED(T, piv_size)
for (size_t i = 0; i < piv_size; ++i)
elementNew(piv_data + i, f(i));
return *this;
}
inline PIVector<T> & assign(const T & f = T()) {return fill(f);}
template<typename T1 = T, typename std::enable_if<
!std::is_trivially_copyable<T1>::value
@@ -264,6 +271,21 @@ public:
}
return *this;
}
inline PIVector<T> & resize(size_t new_size, std::function<T(size_t)> f) {
if (new_size < piv_size) {
T * de = &(piv_data[new_size]);
deleteT(de, piv_size - new_size);
piv_size = new_size;
}
if (new_size > piv_size) {
size_t os = piv_size;
alloc(new_size);
PIINTROSPECTION_CONTAINER_USED(T, (new_size-os))
for (size_t i = os; i < new_size; ++i)
elementNew(piv_data + i, f(i));
}
return *this;
}
template<typename T1 = T, typename std::enable_if<
std::is_trivially_copyable<T1>::value
, int>::type = 0>
@@ -448,6 +470,33 @@ public:
return ret;
}
inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols) const {
assert(rows*cols == piv_size);
PIVector<PIVector<T>> ret;
ret.resize(rows);
for (size_t i = 0; i < rows; i++) {
ret[i] = PIVector<T>(&(piv_data[i*cols]), cols);
}
return ret;
}
template<typename C, typename std::enable_if<
std::is_same<T, PIVector<C>>::value
, int>::type = 0>
inline PIVector<C> reshape() const {
PIVector<C> 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);
}
return ret;
}
private:
inline void _reset() {piv_size = piv_rsize = 0; piv_data = 0;}
inline size_t asize(size_t s) {

View File

@@ -50,10 +50,11 @@ public:
inline PIVector2D(size_t rows, size_t cols, PIVector<T> && v) : rows_(rows), cols_(cols), mat(std::move(v)) {
mat.resize(rows*cols);
}
inline PIVector2D(const PIVector<PIVector<T> > & v) {
inline PIVector2D(const PIVector<PIVector<T>> & v) {
rows_ = v.size();
if (rows_) {
cols_ = v[0].size();
mat.reserve(rows_*cols_);
for (size_t i = 0; i < rows_; i++) {
mat.append(v[i]);
}