pelib  2.0.0
include/pelib/CppMatrix.hpp
Go to the documentation of this file.
00001 /*
00002  Copyright 2015 Nicolas Melot
00003 
00004  This file is part of Pelib.
00005 
00006  Pelib is free software: you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation, either version 3 of the License, or
00009  (at your option) any later version.
00010 
00011  Pelib is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with Pelib. If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 #include <pelib/CppData.hpp>
00022 #include <pelib/Matrix.hpp>
00023 #include <pelib/CastException.hpp>
00024 
00025 #ifndef PELIB_CPPMATRIX
00026 #define PELIB_CPPMATRIX
00027 
00028 namespace pelib
00029 {
00031         template <class Col, class Row, class Value>            
00032         class CppMatrix: public CppData
00033         {
00034                 typedef std::map<Col, Value> RowType;
00035                 typedef std::map<Row, RowType> MatrixType;
00036                 
00037                 public:
00039                         CppMatrix() : CppData()
00040                         {
00041                                 // Do nothing
00042                         }
00043                         
00045                         virtual
00046                         CppMatrix*
00047                         clone() const
00048                         {
00049                                 return new CppMatrix();
00050                         }
00051 
00056                         virtual
00057                         void
00058                         dump(std::ostream &o, const AlgebraData *data) const
00059                         {                               
00060                                 const Matrix<Col, Row, Value> *matrix = dynamic_cast<const Matrix<Col, Row, Value>*>(data);
00061                                 if(matrix == NULL) throw CastException("parameter \"data\" was not of type \"Matrix<Col, Row, Value>\".");
00062 
00063                                 // Dummy key and value and string for their type
00064                                 Row row;
00065                                 Col col;
00066                                 Value value;
00067                                 std::string row_str = type_name(typeid(row)).c_str();
00068                                 std::string col_str = type_name(typeid(col)).c_str();
00069                                 std::string value_str = type_name(typeid(value)).c_str();
00070 
00071                                 // Declare a map to contain the whole matrix
00072                                 o << "std::map<" << row_str.c_str() << ", std::map<" << col_str.c_str() << ", " << value_str.c_str() << "> > map_" << matrix->getName() << ";" << std::endl;
00073 
00074                                 // Declare a map to contain each row
00075                                 o << "std::map<" << col_str.c_str() << ", " << value_str.c_str() << "> map_row;" << std::endl;
00076                                 for(typename std::map<Row, std::map<Col, Value> >::const_iterator row_iter = matrix->getValues().begin();
00077                                     row_iter != matrix->getValues().end(); row_iter++)
00078                                 {
00079                                         row = row_iter->first;
00080                                         for(typename std::map<Col, Value>::const_iterator col_iter = row_iter->second.begin(); col_iter != row_iter->second.end(); col_iter++)
00081                                         {
00082                                                 col = col_iter->first;
00083                                                 value = col_iter->second;
00084 
00085                                                 // Add a value to row
00086                                                 o << "map_row.insert(std::pair<" << col_str.c_str() << ", " << value_str.c_str() << ">(" << col << ", " << value << "));" << std::endl;
00087                                         }
00088 
00089                                         // Add the row to matrix
00090                                         o << "map_" << matrix->getName() << ".insert(std::pair<" << row_str.c_str()
00091                                                 << ", std::map<" << col_str.c_str() << ", " << value_str.c_str()
00092                                                 << "> >(" << row << ", map_row));" << std::endl;
00093 
00094                                         // Reinitialize row
00095                                         o << "map_row = std::map<" << col_str.c_str() << ", " << value_str.c_str() << ">();" << std::endl;
00096                                 }
00097 
00098                                 // Add matrix to data structure
00099                                 o << "pelib::Matrix<" << row_str.c_str() << ", " << col_str.c_str() << ", " << value_str.c_str() << "> " << matrix->getName() << "(\"" << matrix->getName() << "\", map_e);" << std::endl;  
00100                         }
00101         
00102                 protected:
00103                 private:                
00104         };
00105 }
00106 
00107 #endif