#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;
template <class C> class Matrix
{
private:
unsigned h_, w_;
C* data_;
C d_;
Matrix& operator= (const Matrix&); // didn't bother
public:
const unsigned& getH() const {
return h_;
}
const unsigned& getW() const {
return w_;
}
Matrix (const unsigned& h, const unsigned& w, const C& d) :
h_ (h), w_ (w), data_ (new C[w*h]), d_ (d) {
for (unsigned l = 0; l < h_; l++)
for (unsigned c = 0; c < w_; c++)
data_[ (l*w_) + c ] = d_;
}
Matrix (const Matrix& m) :
h_ (m.h_), w_ (m.w_), data_ (new C[m.w_*m.h_]), d_ (m.d_) {
for (unsigned l = 0; l < h_; l++)
for (unsigned c = 0; c < w_; c++)
data_[ (l*w_) + c ] = m.data_[ (l*w_) + c ];
}
~Matrix() {
delete[] data_;
}
C& operator() (const unsigned& l, const unsigned& c) {
assert (l < h_);
assert (c < w_);
return data_[ (l*w_) + c ];
}
};
template <class C> class VarMatrix
{
private:
vector< vector<C> > data_;
C d_;
public:
unsigned getH() const {
return data_.size();
}
unsigned getW() const {
unsigned maxW = 0;
for (unsigned i = 0; i < getH(); i++)
maxW = max (maxW, static_cast<unsigned>(data_[i].size()));
return maxW;
}
VarMatrix (const C& d) :
data_ (vector< vector<C> >()), d_ (d) {}
C& operator() (const unsigned& l, const unsigned& c) {
if (data_.size() <= l)
data_.resize (l + 1, vector<C>());
if (data_[l].size() <= c)
data_[l].resize (c + 1, d_);
return data_[l][c];
}
Matrix<C> toMatrix() {
Matrix<C> r (getH(), getW(), d_);
for (unsigned l = 0; l < getH(); l++)
for (unsigned c = 0; c < data_[l].size(); c++)
r (l, c) = data_[l][c];
return r;
}
};
Subscrever:
Enviar feedback (Atom)
Sem comentários:
Enviar um comentário