/*! \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 .
*/
#ifndef PIVECTOR2D_H
#define PIVECTOR2D_H
#include "pivector.h"
/*! \brief 2D array,
* \details This class used to store 2D array of any type elements as plain vector.
* You can read/write any element via operators [][], first dimension - row, second - column.
* The first dimension is Row, and you can operate with Row as PIVector: modify any element, assign to another Row and etc.
* You can't add values to array, but you can modify any elements or create another PIVector2D.
* PIVector2D has constructors from PIVector and PIVector >
*/
template
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 & v) {
mat = v;
rows_ = rows;
cols_ = cols;
mat.resize(rows*cols);
}
inline PIVector2D(const PIVector > & 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() const {return mat.size();}
inline ssize_t size_s() const {return mat.size_s();}
inline size_t length() const {return mat.length();}
inline size_t capacity() const {return mat.capacity();}
inline bool isEmpty() const {return mat.isEmpty();}
class Row {
friend class PIVector2D;
private:
inline Row(PIVector2D * p, size_t row) : p_(p) {st_ = p_->cols_ * row;}
PIVector2D * 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];}
inline T * data(size_t index = 0) {return p_->mat.data(st_ + index);}
inline const T * data(size_t index = 0) const {return p_->mat.data(st_ + index);}
inline Row & operator =(const Row & other) {
if (p_ == other.p_ && st_ == other.st_) return *this;
size_t sz = piMin(p_->cols_, other.p_->cols_);
p_->copyRow(st_, other.data(), sz);
return *this;
}
inline Row & operator =(const PIVector & other) {
size_t sz = piMin(p_->cols_, other.size());
p_->copyRow(st_, other.data(), sz);
return *this;
}
};
class RowConst {
friend class PIVector2D;
private:
inline RowConst(const PIVector2D * p, size_t row) : p_(p) {st_ = p_->cols_ * row;}
const PIVector2D * 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 const T * data(size_t index = 0) const {return p_->mat.data(st_ + index);}
};
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 PIVector2D & setRow(size_t row, const Row & other) {
size_t sz = piMin(cols_, other.p_->cols_);
copyRow(cols_ * row, other.data(), sz);
return *this;
}
inline PIVector2D & setRow(size_t row, const PIVector & other) {
size_t sz = piMin(cols_, other.size());
copyRow(cols_ * row, other.data(), sz);
return *this;
}
PIVector > toVectors() const {
PIVector > ret;
for(size_t i = 0; i < rows_; ++i)
ret << PIVector(mat.data(i*cols_), cols_);
return ret;
}
PIVector toPlainVector() const {return mat;}
PIVector & plainVector() {return mat;}
const PIVector & plainVector() const {return mat;}
inline PIVector2D & _resizeRaw(size_t r, size_t c) {
piCout << "Error, \"resizeRaw()\" only allowed for simple type declared with __PIVECTOR_SIMPLE_TYPE__ macro!";
assert(0);
return *this;
}
private:
inline void copyRow(size_t start, const T * data, size_t size) {
for (size_t i = 0; i < size; i++)
mat[start + i] = data[i];
}
size_t rows_, cols_;
PIVector mat;
};
template
inline PICout operator <<(PICout s, const PIVector2D & 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;
}
#define __PIVECTOR2D_SIMPLE_TYPE__(T) \
template<> inline void PIVector2D::copyRow(size_t start, const T * data, size_t size) {memcpy(mat.data(start), data, size * sizeof(T));} \
template<> inline PIVector2D & PIVector2D::_resizeRaw(size_t r, size_t c) {rows_ = r; cols_ = c; mat._resizeRaw(r*c); return *this;}
__PIVECTOR2D_SIMPLE_TYPE__(bool)
__PIVECTOR2D_SIMPLE_TYPE__(char)
__PIVECTOR2D_SIMPLE_TYPE__(uchar)
__PIVECTOR2D_SIMPLE_TYPE__(short)
__PIVECTOR2D_SIMPLE_TYPE__(ushort)
__PIVECTOR2D_SIMPLE_TYPE__(int)
__PIVECTOR2D_SIMPLE_TYPE__(uint)
__PIVECTOR2D_SIMPLE_TYPE__(long)
__PIVECTOR2D_SIMPLE_TYPE__(ulong)
__PIVECTOR2D_SIMPLE_TYPE__(llong)
__PIVECTOR2D_SIMPLE_TYPE__(ullong)
__PIVECTOR2D_SIMPLE_TYPE__(float)
__PIVECTOR2D_SIMPLE_TYPE__(double)
__PIVECTOR2D_SIMPLE_TYPE__(ldouble)
#endif // PIVECTOR2D_H