From 789e77d3324692119a130bc5eb6a003fbcf9e9c7 Mon Sep 17 00:00:00 2001 From: Joe Neeman Date: Mon, 24 Jul 2006 11:51:23 +0000 Subject: [PATCH] *** empty log message *** --- flower/include/matrix.hh | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 flower/include/matrix.hh diff --git a/flower/include/matrix.hh b/flower/include/matrix.hh new file mode 100644 index 0000000000..4429ccc46d --- /dev/null +++ b/flower/include/matrix.hh @@ -0,0 +1,66 @@ +/* + matrix.hh -- declare and implement 2d arrays + + source file of the Flower Library + + (c) 2006 Joe Neeman +*/ + +#ifndef MATRIX_HH +#define MATRIX_HH + +#include "std-vector.hh" + +template > +class Matrix +{ +public: + Matrix () + { + rank_ = 0; + } + + Matrix (vsize rows, vsize columns, T const &t) + : data_(rows * columns, t) + { + rank_ = rows; + } + + const T &at (vsize row, vsize col) const + { + assert (row < rank_ && col * rank_ + row < data_.size ()); + + return data_[col * rank_ + row]; + } + + T &at (vsize row, vsize col) + { + assert (row < rank_ && col * rank_ + row < data_.size ()); + + return data_[col * rank_ + row]; + } + + void resize (vsize rows, vsize columns, T const &t) + { + if (rows == rank_) + data_.resize (rows * columns, t); + else + { + vector new_data; + new_data.resize (rows * columns, t); + vsize cur_cols = rank_ ? data_.size () / rank_: 0; + + for (vsize i = 0; i < cur_cols; i++) + for (vsize j = 0; j < rank_; j++) + new_data[i*rows + j] = data_[i*rank_ + j]; + rank_ = rows; + data_ = new_data; + } + } + +private: + vector data_; + vsize rank_; +}; + +#endif /* MATRIX_HH */ -- 2.39.5