code format
This commit is contained in:
@@ -2,24 +2,24 @@
|
||||
* \brief 2D wrapper around PIVector
|
||||
*
|
||||
* This file declares PIVector
|
||||
*/
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
2D wrapper around PIVector
|
||||
Andrey Bychkov work.a.b@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
2D wrapper around PIVector
|
||||
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 Lesser 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 free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
|
||||
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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PIVECTOR2D_H
|
||||
@@ -35,154 +35,176 @@
|
||||
* PIVector2D has constructors from PIVector<T> and PIVector<PIVector<T> >
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
template<typename T>
|
||||
class PIVector2D {
|
||||
public:
|
||||
inline PIVector2D() {rows_ = cols_ = 0;}
|
||||
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);
|
||||
mat.resize(rows * cols, f);
|
||||
}
|
||||
|
||||
inline PIVector2D(size_t rows, size_t cols, const PIVector<T> & v) : rows_(rows), cols_(cols), mat(v) {
|
||||
mat.resize(rows*cols);
|
||||
}
|
||||
inline PIVector2D(size_t rows, size_t cols, const PIVector<T> & v): rows_(rows), cols_(cols), mat(v) { mat.resize(rows * cols); }
|
||||
|
||||
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(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) {
|
||||
rows_ = v.size();
|
||||
if (rows_) {
|
||||
cols_ = v[0].size();
|
||||
mat.reserve(rows_*cols_);
|
||||
mat.reserve(rows_ * cols_);
|
||||
for (size_t i = 0; i < rows_; i++) {
|
||||
mat.append(v[i]);
|
||||
}
|
||||
mat.resize(rows_*cols_);
|
||||
mat.resize(rows_ * cols_);
|
||||
}
|
||||
if (mat.isEmpty()) rows_ = cols_ = 0;
|
||||
}
|
||||
|
||||
inline size_t rows() const {return rows_;}
|
||||
inline size_t rows() const { return rows_; }
|
||||
|
||||
inline size_t cols() const {return cols_;}
|
||||
inline size_t cols() const { return cols_; }
|
||||
|
||||
inline size_t size() const {return mat.size();}
|
||||
inline size_t size() const { return mat.size(); }
|
||||
|
||||
inline ssize_t size_s() const {return mat.size_s();}
|
||||
inline ssize_t size_s() const { return mat.size_s(); }
|
||||
|
||||
inline size_t length() const {return mat.length();}
|
||||
inline size_t length() const { return mat.length(); }
|
||||
|
||||
inline size_t capacity() const {return mat.capacity();}
|
||||
inline size_t capacity() const { return mat.capacity(); }
|
||||
|
||||
inline bool isEmpty() const {return mat.isEmpty();}
|
||||
inline bool isEmpty() const { return mat.isEmpty(); }
|
||||
|
||||
inline bool isNotEmpty() const {return mat.isNotEmpty();}
|
||||
inline bool isNotEmpty() const { return mat.isNotEmpty(); }
|
||||
|
||||
class Row {
|
||||
friend class PIVector2D<T>;
|
||||
|
||||
private:
|
||||
inline Row(PIVector2D<T> * p, size_t row) : p_(&(p->mat)) {st_ = p->cols_ * row; sz_ = p->cols_;}
|
||||
inline Row(PIVector2D<T> * p, size_t row): p_(&(p->mat)) {
|
||||
st_ = p->cols_ * row;
|
||||
sz_ = p->cols_;
|
||||
}
|
||||
PIVector<T> * p_;
|
||||
size_t st_, sz_;
|
||||
|
||||
public:
|
||||
inline size_t size() const {return sz_;}
|
||||
inline T & operator [](size_t index) {return (*p_)[st_ + index];}
|
||||
inline const T & operator [](size_t index) const {return (*p_)[st_ + index];}
|
||||
inline T * data(size_t index = 0) {return p_->data(st_ + index);}
|
||||
inline const T * data(size_t index = 0) const {return p_->data(st_ + index);}
|
||||
inline Row & operator =(const Row & other) {
|
||||
inline size_t size() const { return sz_; }
|
||||
inline T & operator[](size_t index) { return (*p_)[st_ + index]; }
|
||||
inline const T & operator[](size_t index) const { return (*p_)[st_ + index]; }
|
||||
inline T * data(size_t index = 0) { return p_->data(st_ + index); }
|
||||
inline const T * data(size_t index = 0) const { return p_->data(st_ + index); }
|
||||
inline Row & operator=(const Row & other) {
|
||||
if (p_ == other.p_ && st_ == other.st_) return *this;
|
||||
size_t sz = piMin<size_t>(sz_, other.sz_);
|
||||
p_->_copyRaw(p_->data(st_), other.data(), sz);
|
||||
return *this;
|
||||
}
|
||||
inline Row & operator =(const PIVector<T> & other) {
|
||||
inline Row & operator=(const PIVector<T> & other) {
|
||||
size_t sz = piMin<size_t>(sz, other.size());
|
||||
p_->_copyRaw(p_->data(st_), other.data(), sz);
|
||||
return *this;
|
||||
}
|
||||
inline PIVector<T> toVector() const {return PIVector<T>(p_->data(st_), sz_);}
|
||||
inline PIVector<T> toVector() const { return PIVector<T>(p_->data(st_), sz_); }
|
||||
};
|
||||
|
||||
class Col {
|
||||
friend class PIVector2D<T>;
|
||||
|
||||
private:
|
||||
inline Col(PIVector2D<T> * p, size_t row) : p_(&(p->mat)) {step_ = p->cols_; row_ = row; sz_ = p->rows_;}
|
||||
inline Col(PIVector2D<T> * p, size_t row): p_(&(p->mat)) {
|
||||
step_ = p->cols_;
|
||||
row_ = row;
|
||||
sz_ = p->rows_;
|
||||
}
|
||||
PIVector<T> * p_;
|
||||
size_t step_, row_, sz_;
|
||||
|
||||
public:
|
||||
inline size_t size() const {return sz_;}
|
||||
inline T & operator [](size_t index) {return (*p_)[index * step_ + row_];}
|
||||
inline const T & operator [](size_t index) const {return (*p_)[index * step_ + row_];}
|
||||
inline T * data(size_t index = 0) {return p_->data(index * step_ + row_);}
|
||||
inline const T * data(size_t index = 0) const {return p_->data(index * step_ + row_);}
|
||||
inline Col & operator =(const Col & other) {
|
||||
inline size_t size() const { return sz_; }
|
||||
inline T & operator[](size_t index) { return (*p_)[index * step_ + row_]; }
|
||||
inline const T & operator[](size_t index) const { return (*p_)[index * step_ + row_]; }
|
||||
inline T * data(size_t index = 0) { return p_->data(index * step_ + row_); }
|
||||
inline const T * data(size_t index = 0) const { return p_->data(index * step_ + row_); }
|
||||
inline Col & operator=(const Col & other) {
|
||||
if (p_ == other.p_ && row_ == other.row_) return *this;
|
||||
size_t sz = piMin<size_t>(sz_, other.sz_);
|
||||
for (int i=0; i<sz; ++i) (*p_)[i * step_ + row_] = other[i];
|
||||
for (int i = 0; i < sz; ++i)
|
||||
(*p_)[i * step_ + row_] = other[i];
|
||||
return *this;
|
||||
}
|
||||
inline Row & operator =(const PIVector<T> & other) {
|
||||
inline Row & operator=(const PIVector<T> & other) {
|
||||
size_t sz = piMin<size_t>(sz_, other.size());
|
||||
for (int i=0; i<sz; ++i) (*p_)[i * step_ + row_] = other[i];
|
||||
for (int i = 0; i < sz; ++i)
|
||||
(*p_)[i * step_ + row_] = other[i];
|
||||
return *this;
|
||||
}
|
||||
inline PIVector<T> toVector() const {
|
||||
PIVector<T> ret;
|
||||
ret.reserve(sz_);
|
||||
for (size_t i=0; i<sz_; i++) ret << (*p_)[i * step_ + row_];
|
||||
for (size_t i = 0; i < sz_; i++)
|
||||
ret << (*p_)[i * step_ + row_];
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
class RowConst {
|
||||
friend class PIVector2D<T>;
|
||||
|
||||
private:
|
||||
inline RowConst(const PIVector2D<T> * p, size_t row) : p_(&(p->mat)) {st_ = p->cols_ * row; sz_ = p->cols_;}
|
||||
inline RowConst(const PIVector2D<T> * p, size_t row): p_(&(p->mat)) {
|
||||
st_ = p->cols_ * row;
|
||||
sz_ = p->cols_;
|
||||
}
|
||||
const PIVector<T> * p_;
|
||||
size_t st_, sz_;
|
||||
|
||||
public:
|
||||
inline size_t size() const {return sz_;}
|
||||
inline const T & operator [](size_t index) const {return (*p_)[st_ + index];}
|
||||
inline const T * data(size_t index = 0) const {return p_->data(st_ + index);}
|
||||
inline PIVector<T> toVector() const {return PIVector<T>(p_->data(st_), sz_);}
|
||||
inline size_t size() const { return sz_; }
|
||||
inline const T & operator[](size_t index) const { return (*p_)[st_ + index]; }
|
||||
inline const T * data(size_t index = 0) const { return p_->data(st_ + index); }
|
||||
inline PIVector<T> toVector() const { return PIVector<T>(p_->data(st_), sz_); }
|
||||
};
|
||||
|
||||
class ColConst {
|
||||
friend class PIVector2D<T>;
|
||||
|
||||
private:
|
||||
inline ColConst(const PIVector2D<T> * p, size_t row) : p_(&(p->mat)) {step_ = p->cols_; row_ = row; sz_ = p->rows_;}
|
||||
inline ColConst(const PIVector2D<T> * p, size_t row): p_(&(p->mat)) {
|
||||
step_ = p->cols_;
|
||||
row_ = row;
|
||||
sz_ = p->rows_;
|
||||
}
|
||||
const PIVector<T> * p_;
|
||||
size_t step_, row_, sz_;
|
||||
|
||||
public:
|
||||
inline size_t size() const {return p_->rows_;}
|
||||
inline const T & operator [](size_t index) const {return (*p_)[index * step_ + row_];}
|
||||
inline const T * data(size_t index = 0) const {return p_->data(index * step_ + row_);}
|
||||
inline size_t size() const { return p_->rows_; }
|
||||
inline const T & operator[](size_t index) const { return (*p_)[index * step_ + row_]; }
|
||||
inline const T * data(size_t index = 0) const { return p_->data(index * step_ + row_); }
|
||||
inline PIVector<T> toVector() const {
|
||||
PIVector<T> ret;
|
||||
ret.reserve(sz_);
|
||||
for (int i=0; i<size(); i++) ret << (*p_)[i * step_ + row_];
|
||||
for (int i = 0; i < size(); i++)
|
||||
ret << (*p_)[i * step_ + row_];
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
inline T & element(size_t row, size_t col) {return mat[row * cols_ + col];}
|
||||
inline const T & element(size_t row, size_t col) const {return mat[row * cols_ + col];}
|
||||
inline const T & at(size_t row, size_t col) const {return mat[row * cols_ + col];}
|
||||
inline Row operator[](size_t index) {return Row(this, index);}
|
||||
inline RowConst operator[](size_t index) const {return RowConst(this, index);}
|
||||
inline T * data(size_t index = 0) {return mat.data(index);}
|
||||
inline const T * data(size_t index = 0) const {return mat.data(index);}
|
||||
inline T & element(size_t row, size_t col) { return mat[row * cols_ + col]; }
|
||||
inline const T & element(size_t row, size_t col) const { return mat[row * cols_ + col]; }
|
||||
inline const T & at(size_t row, size_t col) const { return mat[row * cols_ + col]; }
|
||||
inline Row operator[](size_t index) { return Row(this, index); }
|
||||
inline RowConst operator[](size_t index) const { return RowConst(this, index); }
|
||||
inline T * data(size_t index = 0) { return mat.data(index); }
|
||||
inline const T * data(size_t index = 0) const { return mat.data(index); }
|
||||
|
||||
inline Row row(size_t index) {return Row(this, index);}
|
||||
inline RowConst row(size_t index) const {return RowConst(this, index);}
|
||||
inline Col col(size_t index) {return Col(this, index);}
|
||||
inline ColConst col(size_t index) const {return ColConst(this, index);}
|
||||
inline Row row(size_t index) { return Row(this, index); }
|
||||
inline RowConst row(size_t index) const { return RowConst(this, index); }
|
||||
inline Col col(size_t index) { return Col(this, index); }
|
||||
inline ColConst col(size_t index) const { return ColConst(this, index); }
|
||||
inline PIVector2D<T> & setRow(size_t row, const Row & other) {
|
||||
size_t sz = piMin<size_t>(cols_, other.sz_);
|
||||
mat._copyRaw(mat.data(cols_ * row), other.data(), sz);
|
||||
@@ -227,20 +249,20 @@ public:
|
||||
}
|
||||
|
||||
inline PIVector2D<T> & resize(size_t rows, size_t cols, const T & f = T()) {
|
||||
mat.resize(rows*cols_, f);
|
||||
rows_ = rows;
|
||||
mat.resize(rows * cols_, f);
|
||||
rows_ = rows;
|
||||
int cs = (cols - cols_);
|
||||
if (cs < 0) {
|
||||
for (size_t r=0; r<rows; ++r) {
|
||||
mat.remove(r*cols + cols, -cs);
|
||||
for (size_t r = 0; r < rows; ++r) {
|
||||
mat.remove(r * cols + cols, -cs);
|
||||
}
|
||||
}
|
||||
mat.resize(rows*cols, f);
|
||||
mat.resize(rows * cols, f);
|
||||
if (!mat.isEmpty()) {
|
||||
if (cs > 0) {
|
||||
for (size_t r=0; r<rows_; ++r) {
|
||||
for (int i=0; i<cs; ++i)
|
||||
mat.insert(r*cols + cols_, mat.take_back());
|
||||
for (size_t r = 0; r < rows_; ++r) {
|
||||
for (int i = 0; i < cs; ++i)
|
||||
mat.insert(r * cols + cols_, mat.take_back());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -248,26 +270,25 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline bool operator ==(const PIVector2D<T> & t) const {
|
||||
if (cols_ != t.cols_ || rows_ != t.rows_)
|
||||
return false;
|
||||
inline bool operator==(const PIVector2D<T> & t) const {
|
||||
if (cols_ != t.cols_ || rows_ != t.rows_) return false;
|
||||
return mat == t.mat;
|
||||
}
|
||||
inline bool operator !=(const PIVector2D<T> & t) const {return !(*this == t);}
|
||||
inline bool operator!=(const PIVector2D<T> & t) const { return !(*this == t); }
|
||||
|
||||
inline PIVector<PIVector<T> > toVectors() const {
|
||||
PIVector<PIVector<T> > ret;
|
||||
inline PIVector<PIVector<T>> toVectors() const {
|
||||
PIVector<PIVector<T>> ret;
|
||||
ret.reserve(rows_);
|
||||
for(size_t i = 0; i < rows_; ++i)
|
||||
ret << PIVector<T>(mat.data(i*cols_), cols_);
|
||||
for (size_t i = 0; i < rows_; ++i)
|
||||
ret << PIVector<T>(mat.data(i * cols_), cols_);
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline PIVector<T> toPlainVector() const {return mat;}
|
||||
inline PIVector<T> toPlainVector() const { return mat; }
|
||||
|
||||
inline PIVector<T> & plainVector() {return mat;}
|
||||
inline PIVector<T> & plainVector() { return mat; }
|
||||
|
||||
inline const PIVector<T> & plainVector() const {return mat;}
|
||||
inline const PIVector<T> & plainVector() const { return mat; }
|
||||
|
||||
inline void swap(PIVector2D<T> & other) {
|
||||
mat.swap(other.mat);
|
||||
@@ -275,13 +296,11 @@ public:
|
||||
piSwap<size_t>(cols_, other.cols_);
|
||||
}
|
||||
|
||||
template<typename T1 = T, typename std::enable_if<
|
||||
std::is_trivially_copyable<T1>::value
|
||||
, int>::type = 0>
|
||||
template<typename T1 = T, typename std::enable_if<std::is_trivially_copyable<T1>::value, int>::type = 0>
|
||||
inline PIVector2D<T> & _resizeRaw(size_t r, size_t c) {
|
||||
rows_ = r;
|
||||
cols_ = c;
|
||||
mat._resizeRaw(r*c);
|
||||
mat._resizeRaw(r * c);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -290,14 +309,12 @@ public:
|
||||
mat.clear();
|
||||
}
|
||||
|
||||
template <typename ST>
|
||||
template<typename ST>
|
||||
inline PIVector2D<ST> map(std::function<ST(const T & e)> f) const {
|
||||
return PIVector2D<ST>(rows_, cols_, mat.map(f));
|
||||
}
|
||||
|
||||
inline void forEach(std::function<void(const T &)> f) const {
|
||||
mat.forEach(f);
|
||||
}
|
||||
inline void forEach(std::function<void(const T &)> f) const { mat.forEach(f); }
|
||||
|
||||
inline PIVector2D<T> & forEach(std::function<void(T &)> f) {
|
||||
mat.forEach(f);
|
||||
@@ -311,7 +328,7 @@ protected:
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline PICout operator <<(PICout s, const PIVector2D<T> & v) {
|
||||
inline PICout operator<<(PICout s, const PIVector2D<T> & v) {
|
||||
s.saveAndSetControls(0);
|
||||
s << "{";
|
||||
for (size_t i = 0; i < v.rows(); ++i) {
|
||||
@@ -321,7 +338,7 @@ inline PICout operator <<(PICout s, const PIVector2D<T> & v) {
|
||||
if (j < v.cols() - 1) s << ", ";
|
||||
}
|
||||
s << " }";
|
||||
if (i < v.rows() - 1) s << PICoutManipulators::NewLine ;
|
||||
if (i < v.rows() - 1) s << PICoutManipulators::NewLine;
|
||||
}
|
||||
if (v.isEmpty()) s << "{ }";
|
||||
s << "}";
|
||||
|
||||
Reference in New Issue
Block a user