- Main Page
- Classes
- Files

- File List

# OptFrame/Util/Matrix.hpp

    00001 // TODO
    00002 // Implement ITERATORS - Usage: to sort matrix based on a line or a column.
    00003 // Implement sortMatrix method based on 'sort' of STL.
    00004
    00005 // Ver este link
    00006 //http://www.parashift.com/c++-faq-lite/operator-overloading.html
    00007
    00008 // boas dicas em 13.9
    00009
    00010 // Analisar preferencia de m(1,2) do que m[1][2]
    00011
    00012 // http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.11
    00013
    00014 // (...)
    00015 // Put it this way: the operator() approach is never worse than, and sometimes
    00016 // better than, the [][] approach.
    00017
    00018
    00019 #ifndef MATRIX_H_
    00020 #define MATRIX_H_
    00021
    00022 #include <iostream>
    00023 #include <stdlib.h>
    00024 #include <vector>
    00025
    00026 using namespace std;
    00027
    00028 class BadIndex
    00029 {
    00030 private:
    00031    string message;
    00032 public:
    00033    BadIndex(string m)
    00034    {
    00035       message = m;
    00036    }
    00037 };
    00038
    00039 template<class T>
    00040 class Matrix
    00041 {
    00042 public:
    00043    Matrix(unsigned _quadratic)
    00044    {
    00045       rows = cols = _quadratic;
    00046
    00047       if (rows == 0 || cols == 0)
    00048          throw BadIndex("Matrix constructor has 0 size");
    00049
    00050       data = new T[rows * cols];
    00051    }
    00052
    00053    Matrix(unsigned _rows, unsigned _cols)
    00054    {
    00055       rows = _rows;
    00056       cols = _cols;
    00057
    00058       if (rows == 0 || cols == 0)
    00059          throw BadIndex("Matrix constructor has 0 size");
    00060
    00061       data = new T[rows * cols];
    00062    }
    00063
    00064    T& operator()(unsigned row, unsigned col) // subscript operators often come in pairs
    00065    {
    00066       if (row >= rows || col >= cols)
    00067       {
    00068          cout << "[set Matrix] Pedido de (" << row << "," << col << ")" << " de (" << rows << ","
    00069                << cols << ")" << endl;
    00070
    00071          int jkl;
    00072          cin >> jkl;
    00073
    00074          throw BadIndex("Matrix subscript out of bounds");
    00075       }
    00076
    00077       return data[cols * row + col];
    00078    }
    00079
    00080    T operator()(unsigned row, unsigned col) const // subscript operators often come in pairs
    00081    {
    00082       if (row >= rows || col >= cols)
    00083       {
    00084          cout << "[get Matrix] Pedido de (" << row << "," << col << ")" << " de (" << rows << ","
    00085                << cols << ")" << endl;
    00086
    00087          int jkl;
    00088          cin >> jkl;
    00089
    00090          throw BadIndex("const Matrix subscript out of bounds");
    00091       }
    00092
    00093       return data[cols * row + col];
    00094    }
    00095
    00096    virtual ~Matrix() //Destructor
    00097    {
    00098       delete[] data;
    00099    }
    00100
    00101    // Retorna true se a matriz for quadrada
    00102    bool square() const
    00103    {
    00104       return (rows == cols);
    00105    }
    00106
    00107    // Metodos util! fill() preenche a matriz com o valor desejado
    00108    void fill(T v)
    00109    {
    00110       unsigned int total = rows * cols;
    00111
    00112       for (unsigned int i = 0; i < (total); i++)
    00113          data[i] = v;
    00114    }
    00115
    00116    Matrix(const Matrix& m) // Copy constructor
    00117    {
    00118       rows = m.rows;
    00119       cols = m.cols;
    00120
    00121       unsigned int total = rows * cols;
    00122       data = new T[total];
    00123
    00124       for (unsigned int i = 0; i < (total); i++)
    00125          data[i] = m.data[i];
    00126    }
    00127
    00128    Matrix& operator=(const Matrix& m) // Assignment operator
    00129    {
    00130       //Verificando auto-referencia (Importante!)
    00131       if (&m == this)
    00132          return *this;
    00133
    00134       delete[] data;
    00135
    00136       rows = m.rows;
    00137       cols = m.cols;
    00138
    00139       int total = rows * cols;
    00140
    00141       if (total < 0)
    00142       {
    00143          cerr << "Valor maior do que o suportado pela Matrix! (" << total << ")" << endl;
    00144          exit(1);
    00145       }
    00146
    00147       data = new T[total];
    00148
    00149       for (int i = 0; i < (total); i++)
    00150          data[i] = m.data[i];
    00151
    00152       return *this;
    00153    }
    00154
    00155    unsigned getRows() const
    00156    {
    00157       return rows;
    00158    }
    00159
    00160    unsigned getCols() const
    00161    {
    00162       return cols;
    00163    }
    00164
    00165    vector<T> getRow(int _row) const
    00166    {
    00167       vector<T> row(cols);
    00168
    00169       for (int i = 0; i < cols; i++)
    00170       {
    00171          row[i] = operator()(_row, i);
    00172       }
    00173
    00174       return row;
    00175    }
    00176
    00177    void setRow(int p, vector<T>& _row)
    00178    {
    00179       int numCol = (_row.size() < cols) ? _row.size() : cols;
    00180
    00181       for (int i = 0; i < numCol; i++)
    00182       {
    00183          operator()(p, i) = _row[i];
    00184       }
    00185    }
    00186
    00187    vector<T> getCol(int _col) const
    00188    {
    00189       vector<T> col(rows);
    00190
    00191       for (int i = 0; i < rows; i++)
    00192       {
    00193          col[i] = operator()(i, _col);
    00194       }
    00195
    00196       return col;
    00197    }
    00198
    00199    void setCol(int p, vector<T>& _col)
    00200    {
    00201       int numRow = (_col.size() < rows) ? _col.size() : rows;
    00202
    00203       for (int i = 0; i < numRow; i++)
    00204       {
    00205          operator()(i, p) = _col[i];
    00206       }
    00207    }
    00208
    00209 private:
    00210    unsigned rows, cols;
    00211    T* data;
    00212 };
    00213
    00214 template<class T>
    00215 ostream& operator<<(ostream &os, const Matrix<T> &obj)
    00216 {
    00217    //os << "Matrix(" << obj.getRows() << "," << obj.getCols() << ")" << endl;
    00218
    00219    os << endl;
    00220
    00221    for (unsigned int i = 0; i < obj.getRows(); i++)
    00222    {
    00223       for (unsigned int j = 0; j < obj.getCols(); j++)
    00224          os << obj(i, j) << " ";
    00225       os << endl;
    00226    }
    00227    return os;
    00228 }
    00229
    00230 ostream& operator<<(ostream &os, const Matrix<string> &obj)
    00231 {
    00232    os << "Matrix(" << obj.getRows() << "," << obj.getCols() << ")" << endl;
    00233
    00234    for (unsigned int i = 0; i < obj.getRows(); i++)
    00235    {
    00236       for (unsigned int j = 0; j < obj.getCols(); j++)
    00237          os << "\"" << obj(i, j) << "\"" << "\t";
    00238       os << endl;
    00239    }
    00240    return os;
    00241 }
    00242
    00243 #endif /*MATRIX_H_*/

All Classes Functions