Files
pip/src_main/containers/pivector2d.h

126 lines
3.4 KiB
C++

/*! \file pivecto2d.h
* \brief 2D wrapper around PIVector
*
* This file declares PIVector
*/
/*
PIP - Platform Independent Primitives
2D wrapper around PIVector
Copyright (C) 2017 Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIVECTOR2D_H
#define PIVECTOR2D_H
#include "pivector.h"
template <typename T>
class PIVector2D {
public:
inline PIVector2D() {rows_ = cols_ = 0;}
inline PIVector2D(size_t rows, size_t cols, const T & f = T()) {
rows_ = rows;
cols_ = cols;
mat.resize(rows*cols, f);
}
inline PIVector2D(size_t rows, size_t cols, const PIVector<T> & v) {
mat = v;
rows_ = rows;
cols_ = cols;
mat.resize(rows*cols);
}
inline PIVector2D(const PIVector<PIVector<T> > & v) {
rows_ = v.size();
if (rows_) {
cols_ = v[0].size();
for (size_t i = 0; i < rows_; i++) {
mat.append(v[i]);
}
mat.resize(rows_*cols_);
}
if (mat.isEmpty()) rows_ = cols_ = 0;
}
inline size_t rows() const {return rows_;}
inline size_t cols() const {return cols_;}
inline size_t size_all() const {return mat.size();}
inline ssize_t size_s_all() const {return mat.size_s();}
inline bool isEmpty() const {return mat.isEmpty();}
class Row {
friend class PIVector2D<T>;
private:
inline Row(PIVector2D<T> * p, size_t row) : p_(p) {st_ = p_->cols_ * row;}
PIVector2D<T> * p_;
size_t st_;
public:
inline size_t size() const {return p_->cols_;}
inline T & operator [](size_t index) {return p_->mat[st_ + index];}
inline const T & operator [](size_t index) const {return p_->mat[st_ + index];}
};
class RowConst {
friend class PIVector2D<T>;
private:
inline RowConst(const PIVector2D<T> * p, size_t row) : p_(p) {st_ = p_->cols_ * row;}
const PIVector2D<T> * p_;
size_t st_;
public:
inline size_t size() const {return p_->cols_;}
inline const T & operator [](size_t index) const {return p_->mat[st_ + index];}
};
inline Row operator[](size_t index) {return Row(this, index);}
inline RowConst operator[](size_t index) const {return RowConst(this, index);}
PIVector<PIVector<T> > toVectors() const {
PIVector<PIVector<T> > ret;
for(size_t i = 0; i < rows_; ++i)
ret << PIVector<T>(mat.data(i*cols_), cols_);
return ret;
}
PIVector<T> toPlainVector() const {return mat;}
private:
size_t rows_, cols_;
PIVector<T> mat;
};
template<typename T>
inline PICout operator <<(PICout s, const PIVector2D<T> & v) {
s.setControl(0, true);
s << "{";
for (size_t i = 0; i < v.rows(); ++i) {
s << "{ ";
for (size_t j = 0; j < v.cols(); ++j) {
s << v[i][j];
if (j < v.cols() - 1) s << ", ";
}
s << " }";
if (i < v.rows() - 1) s << PICoutManipulators::NewLine ;
}
if (v.isEmpty()) s << "{ }";
s << "}";
s.restoreControl();
return s;
}
#endif // PIVECTOR2D_H