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 <iostream> 00022 #include <cstdlib> 00023 #include <fstream> 00024 #include <sstream> 00025 #include <string> 00026 #include <boost/regex.hpp> 00027 #include <iomanip> 00028 00029 #include <pelib/Algebra.hpp> 00030 #include <pelib/AmplDataParser.hpp> 00031 #include <pelib/AlgebraData.hpp> 00032 00033 #define debug(var) std::cout << "[" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << "] " << #var << " = \"" << (var) << "\"" << std::endl; 00034 00035 using namespace std; 00036 00037 namespace pelib 00038 { 00039 Algebra::Algebra() { /* Do nothing */ } 00040 00041 Algebra::Algebra(const map<string, const AlgebraData* const> &records) 00042 { 00043 this->records = records; 00044 } 00045 00046 Algebra::~Algebra() 00047 { 00048 deleteRecords(); 00049 } 00050 00051 Algebra::Algebra(const Algebra &rhs) 00052 { 00053 *this = rhs; 00054 } 00055 00056 Algebra::Algebra(const Algebra *rhs) 00057 { 00058 *this = *rhs; 00059 } 00060 00061 Algebra* 00062 Algebra::clone() const 00063 { 00064 return new Algebra(this); 00065 } 00066 00067 Algebra 00068 Algebra::merge(const Algebra& record) const 00069 { 00070 // Copy all AlgebraData 00071 map<string, const AlgebraData * const> rec; 00072 00073 // Copy all records stored in this instance 00074 for(map<string, const AlgebraData * const>::const_iterator iter = records.begin(); iter != records.end(); iter++) 00075 { 00076 rec.insert(pair<string, pelib::AlgebraData*>(iter->second->getName(), iter->second->clone())); 00077 } 00078 00079 // Copy all records from the foreign instance 00080 for(map<string, const AlgebraData * const>::const_iterator iter = record.getAllRecords().begin(); iter != record.getAllRecords().end(); iter++) 00081 { 00082 rec.insert(pair<string, pelib::AlgebraData*>(iter->second->getName(), iter->second->clone())); 00083 } 00084 00085 // Build a new algebra with all records 00086 return Algebra(rec); 00087 } 00088 00089 const map<string, const AlgebraData * const>& 00090 Algebra::getAllRecords() const 00091 { 00092 return records; 00093 } 00094 00095 void 00096 Algebra::insert(const pelib::AlgebraData *data) 00097 { 00098 map<string, const AlgebraData * const>::iterator iter = records.find(data->getName()); 00099 const AlgebraData *new_elem; 00100 if(iter != records.end()) 00101 { 00102 const AlgebraData *ptr = iter->second; 00103 AlgebraData *dat = ptr->clone(); 00104 delete ptr; 00105 dat->merge(data); 00106 new_elem = dat; 00107 records.erase(iter); 00108 } 00109 else 00110 { 00111 new_elem = data->clone(); 00112 } 00113 records.insert(pair<string, const AlgebraData*>(data->getName(), new_elem)); 00114 } 00115 00116 void 00117 Algebra::remove(const string name) 00118 { 00119 map<string, const AlgebraData * const>::iterator ptr = records.find(name); 00120 00121 if(ptr != records.end()) 00122 { 00123 delete ptr->second; 00124 records.erase(name); 00125 } 00126 } 00127 00128 Algebra& 00129 Algebra::operator=(const Algebra &rhs) 00130 { 00131 if(this != &rhs) 00132 { 00133 // Free all records 00134 deleteRecords(); 00135 00136 // Clone every objects of the source's collection 00137 map<string, const AlgebraData * const> rhrec = rhs.records; 00138 for (map<string, const AlgebraData * const>::iterator i = rhrec.begin(); i != rhrec.end(); i++) 00139 { 00140 AlgebraData *copy = i->second->clone(); 00141 this->records.insert(pair<string, const AlgebraData * const>(i->first, copy)); 00142 } 00143 } 00144 00145 return *this; 00146 } 00147 00148 void 00149 Algebra::deleteRecords() 00150 { 00151 for (map<string, const AlgebraData * const>::iterator i = records.begin(); i != records.end();) 00152 { 00153 delete i->second; 00154 map<string, const AlgebraData * const>::iterator erase = i; 00155 i++; 00156 records.erase(erase); 00157 } 00158 } 00159 00160 Algebra 00161 Algebra::filter(const set<string> &list) const 00162 { 00163 Algebra copy; 00164 for(std::map<std::string, const AlgebraData * const>::const_iterator i = this->records.begin(); i != this->records.end(); i++) 00165 { 00166 if(list.find(i->first) != list.end()) 00167 { 00168 copy.insert(i->second->clone()); 00169 } 00170 } 00171 00172 return copy; 00173 } 00174 }