pelib
2.0.0
|
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/AmplOutputData.hpp> 00022 #include <pelib/Matrix.hpp> 00023 #include <pelib/CastException.hpp> 00024 00025 #ifndef PELIB_AMPLOUTPUTMATRIX 00026 #define PELIB_AMPLOUTPUTMATRIX 00027 00028 #ifdef debug 00029 #undef debug 00030 #endif 00031 00032 #if 0 00033 #include <iostream> 00034 #define debug(var) std::cout << "[" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << "] " << #var << " = \"" << var << "\"" << std::endl; 00035 #else 00036 #define debug(var) 00037 #endif 00038 00039 namespace pelib 00040 { 00042 template <class Col, class Row, class Value> 00043 class AmplOutputMatrix: public AmplOutputData 00044 { 00045 typedef std::map<Col, Value> RowType; 00046 typedef std::map<Row, RowType> MatrixType; 00047 00048 public: 00052 AmplOutputMatrix(bool strict = true) 00053 { 00054 this->strict = strict; 00055 } 00056 00058 virtual 00059 AmplOutputMatrix* 00060 clone() const 00061 { 00062 return new AmplOutputMatrix(); 00063 } 00064 00066 virtual 00067 std::string 00068 getDetailedPattern() 00069 { 00070 return "(\\w[\\w\\d_]*)\\s*\\[\\*,\\*\\]\\s*:\\s*(.+?)\\s*:=\\s*(.+?)\\s*"; 00071 } 00072 00074 virtual 00075 std::string 00076 getGlobalPattern() 00077 { 00078 return "\\w[\\w\\d_]*\\s*\\[\\*,\\*\\]\\s*:\\s*(?:[-\\w\\d\\.+]+\\s*)+\\s*:=.+"; 00079 } 00080 00082 virtual 00083 AlgebraData* 00084 parse(std::istream &in) 00085 { 00086 MatrixType values; 00087 00088 std::string str((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); 00089 boost::cmatch match; // = AlgebraDataParser::match(getDetailedPattern(), str); 00090 if(!boost::regex_match(str.c_str(), match, boost::regex(getDetailedPattern()))) 00091 { 00092 throw ParseException(std::string("String \"").append(str).append("\" doesn't match regex \"").append(getDetailedPattern()).append("\". ")); 00093 } 00094 00095 std::string indexes = match[2]; 00096 std::string remain = match[3]; 00097 00098 // Extract all indexes 00099 boost::regex isolated_value("\\s*([^\\s]+)\\s*"); 00100 boost::sregex_token_iterator iter = make_regex_token_iterator(indexes, isolated_value, 1, boost::regex_constants::match_default); 00101 boost::sregex_token_iterator end; 00102 00103 // Put them in a vector 00104 std::vector<Col> cols; 00105 for(; iter != end; ++iter) 00106 { 00107 try 00108 { 00109 cols.push_back(AlgebraDataParser::convert<Col>(*iter, strict)); 00110 } catch(NoDecimalFloatException &e) 00111 { 00112 std::ostringstream ss; 00113 ss << e.getValue(); 00114 throw ParseException(std::string("Asked a decimal conversion, but \"").append(ss.str()).append("\" is integer.")); 00115 } 00116 } 00117 int s = cols.size(); 00118 00119 // Parse the rest of the matrix; catch the row index every s reads 00120 iter = make_regex_token_iterator(remain, isolated_value, 1, boost::regex_constants::match_default); 00121 00122 Row row; 00123 Col col; 00124 Value val; 00125 00126 int integer_values = 0, total_values = 0; 00127 while(iter != end) 00128 { 00129 std::map<Col, Value> vector; 00130 00131 try 00132 { 00133 row = AlgebraDataParser::convert<Row>(*iter, strict); 00134 } catch(NoDecimalFloatException &e) 00135 { 00136 std::ostringstream ss; 00137 ss << e.getValue(); 00138 throw ParseException(std::string("Asked a decimal conversion, but \"").append(ss.str()).append("\" is integer.")); 00139 } 00140 iter++; 00141 for(int i = 0; i < s; i++) 00142 { 00143 try 00144 { 00145 val = AlgebraDataParser::convert<Value>(*iter, strict); 00146 } catch(NoDecimalFloatException &e) 00147 { 00148 float float_value = e.getValue(); 00149 std::stringstream ss; 00150 ss << float_value; 00151 ss >> val; 00152 00153 integer_values++; 00154 } 00155 col = cols[i]; 00156 vector.insert(std::pair<Col, Value>(col, val)); 00157 00158 iter++; 00159 total_values++; 00160 } 00161 values.insert(std::pair<Row, RowType>(row, vector)); 00162 } 00163 00164 // If all values could have been parsed as integer, then this is obviously an integer vector rather to a float one 00165 if(integer_values == total_values) 00166 { 00167 //throw NoDecimalFloatException(std::string("Matrix only composed of integer-parsable values."), 0); 00168 throw ParseException(std::string("Matrix only composed of integer-parsable values.")); 00169 } 00170 00171 return new Matrix<Col, Row, Value>(match[1], values); 00172 } 00173 00175 virtual 00176 void 00177 dump(std::ostream &o, const AlgebraData *data) const 00178 { 00179 const Matrix<Col, Row, Value> *matrix = dynamic_cast<const Matrix<Col, Row, Value>*>(data); 00180 if(matrix == NULL) throw CastException("parameter \"data\" was not of type \"Matrix<Col, Row, Value>\"."); 00181 00182 o << matrix->getName() << " [*,*]" << std::endl << ":\t"; 00183 typename std::map<Row, std::map<Col, Value> >::const_iterator row_iter = matrix->getValues().begin(); 00184 if(row_iter != matrix->getValues().end()) 00185 { 00186 for(typename std::map<Col, Value>::const_iterator col_iter = row_iter->second.begin(); col_iter != row_iter->second.end(); col_iter++) 00187 { 00188 o << col_iter->first << "\t"; 00189 } 00190 o << ":=" << std::endl; 00191 00192 for(; row_iter != matrix->getValues().end(); row_iter++) 00193 { 00194 o << row_iter->first << "\t"; 00195 for(typename std::map<Col, Value>::const_iterator col_iter = row_iter->second.begin(); col_iter != row_iter->second.end(); col_iter++) 00196 { 00197 o << col_iter->second << "\t"; 00198 } 00199 o << std::endl; 00200 } 00201 } 00202 00203 o << ";" << std::endl; 00204 } 00205 00206 protected: 00208 bool strict; 00209 private: 00210 }; 00211 } 00212 00213 #undef debug 00214 #endif