PIVector2D and PIMathMatrix

completed and optimized

git-svn-id: svn://db.shs.com.ru/pip@657 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
2018-11-13 13:14:09 +00:00
parent e35313dc1a
commit 95983ca9a4
3 changed files with 119 additions and 94 deletions

View File

@@ -73,63 +73,110 @@ public:
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_;
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 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 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>(p_->cols_, other.p_->cols_);
p_->copyRow(st_, other.data(), sz);
size_t sz = piMin<size_t>(sz_, other.sz_);
copyRow(st_, other.data(), sz, p_);
return *this;
}
inline Row & operator =(const PIVector<T> & other) {
size_t sz = piMin<size_t>(p_->cols_, other.size());
p_->copyRow(st_, other.data(), sz);
size_t sz = piMin<size_t>(sz, other.size());
copyRow(st_, other.data(), sz, p_);
return *this;
}
inline PIVector<T> toVector() const {return PIVector<T>(p_->mat.data(st_), p_->cols_);}
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_;}
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) {
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];
return *this;
}
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];
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_];
return ret;
}
};
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_;
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 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 PIVector<T> toVector() const {return PIVector<T>(p_->mat.data(st_), p_->cols_);}
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_;}
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 PIVector<T> toVector() const {
PIVector<T> ret;
ret.reserve(sz_);
for (int i=0; i<rows_; 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 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 PIVector<T> col(size_t index) {
PIVector<T> ret;
ret.reserve(rows_);
for (int i=0; i<rows_; i++) ret << mat[i*cols_+index];
return ret;
}
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.p_->cols_);
copyRow(cols_ * row, other.data(), sz);
copyRow(cols_ * row, other.data(), sz, &mat);
return *this;
}
inline PIVector2D<T> & setRow(size_t row, const PIVector<T> & other) {
size_t sz = piMin<size_t>(cols_, other.size());
copyRow(cols_ * row, other.data(), sz);
copyRow(cols_ * row, other.data(), sz, &mat);
return *this;
}
@@ -156,12 +203,11 @@ public:
}
protected:
inline void copyRow(size_t start, const T * data, size_t size) {
inline void copyRow(size_t start, const T * data, size_t size, PIVector<T> * dst) {
for (size_t i = 0; i < size; i++)
mat[start + i] = data[i];
(*dst)[start + i] = data[i];
}
//private:
size_t rows_, cols_;
PIVector<T> mat;
};
@@ -187,7 +233,7 @@ inline PICout operator <<(PICout s, const PIVector2D<T> & v) {
}
#define __PIVECTOR2D_SIMPLE_TYPE__(T) \
template<> inline void PIVector2D<T>::copyRow(size_t start, const T * data, size_t size) {memcpy(mat.data(start), data, size * sizeof(T));} \
template<> inline void PIVector2D<T>::copyRow(size_t start, const T * data, size_t size, PIVector<T> * dst) {memcpy(dst->data(start), data, size * sizeof(T));} \
template<> inline PIVector2D<T> & PIVector2D<T>::_resizeRaw(size_t r, size_t c) {rows_ = r; cols_ = c; mat._resizeRaw(r*c); return *this;}
__PIVECTOR2D_SIMPLE_TYPE__(bool)