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/PelibException.hpp> 00030 #include <pelib/AlgebraData.hpp> 00031 00032 #ifndef PELIB_VECTOR 00033 #define PELIB_VECTOR 00034 00035 namespace pelib 00036 { 00038 template <class Key, class Value> 00039 class Vector: public AlgebraData 00040 { 00041 typedef std::map<Key, Value> VectorType; 00042 00043 public: 00048 Vector(const std::string name, const VectorType values) : AlgebraData(name), values(values) 00049 { 00050 // Do nothing 00051 } 00052 00054 Vector(const Vector<Key, Value>* vector): AlgebraData(vector->getName()), values(vector->getValues()) 00055 { 00056 // Do nothing 00057 } 00058 00060 virtual 00061 Vector* 00062 clone() const 00063 { 00064 return new Vector<Key, Value>(name, values); 00065 } 00066 00068 virtual 00069 const std::map<Key, Value>& 00070 getValues() const 00071 { 00072 return values; 00073 } 00074 00076 virtual 00077 const Value& 00078 find(Key key) const 00079 { 00080 return values.find(key)->second; 00081 } 00082 00084 virtual 00085 size_t 00086 getSize() const 00087 { 00088 return values.size(); 00089 } 00090 00091 void 00092 merge(const AlgebraData *ptr) 00093 { 00094 // Only allow merging if both this and ptr are of same name and type 00095 if(ptr->getName().compare(this->getName()) == 0 && std::string(typeid(*ptr).name()).compare(typeid(Vector<Key, Value>).name()) == 0) 00096 { 00097 Vector<Key, Value> *vector = (Vector<Key, Value>*)ptr; 00098 for(typename VectorType::iterator i = vector->values.begin(); i != vector->values.end(); i++) 00099 { 00100 if(this->getValues().find(i->first) != this->getValues().end()) 00101 { 00102 this->values.erase(this->values.find(i->first)); 00103 } 00104 // Add the new row in this matrix 00105 this->values.insert(std::pair<Key, Value>(i->first, i->second)); 00106 } 00107 } 00108 else 00109 { 00110 throw PelibException(std::string("Cannot merge data \"") + ptr->getName() + "\" with " + typeid(Value).name() + " vector of name \"" + this->getName() + "\"."); 00111 } 00112 } 00113 protected: 00114 VectorType values; 00115 private: 00116 }; 00117 } 00118 00119 #endif