libFAUDES

Sections

Index

nameset.cpp

Go to the documentation of this file.
00001 /** @file nameset.cpp @brief Classes NameSet */
00002 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005    Copyright (C) 2006  Bernd Opitz
00006    Exclusive copyright is granted to Klaus Schmidt
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Lesser General Public
00010    License as published by the Free Software Foundation; either
00011    version 2.1 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Lesser General Public License for more details.
00017 
00018    You should have received a copy of the GNU Lesser General Public
00019    License along with this library; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00021 
00022 
00023 #include "nameset.h"
00024 
00025 namespace faudes {
00026 
00027 // std faudes type (cannot do New() with macro)
00028 FAUDES_TYPE_IMPLEMENTATION_COPY(NameSet,TBaseSet<Idx>,)
00029 FAUDES_TYPE_IMPLEMENTATION_CAST(NameSet,TBaseSet<Idx>,)
00030 FAUDES_TYPE_IMPLEMENTATION_ASSIGN(NameSet,TBaseSet<Idx>,)
00031 FAUDES_TYPE_IMPLEMENTATION_EQUAL(NameSet,TBaseSet<Idx>,)
00032 
00033 // empty constructor
00034 NameSet::NameSet(void) : TBaseSet<Idx>()
00035 {
00036   mpSymbolTable = SymbolTable::GlobalEventSymbolTablep();
00037   Name("NameSet");
00038   FD_DC("NameSet("<<this<<")::NameSet() with symtab "<< mpSymbolTable); 
00039 }
00040 
00041 // constructor from nameset
00042 NameSet::NameSet(const NameSet& rOtherSet) : TBaseSet<Idx>() {
00043   FD_DC("NameSet(" << this << ")::NameSet(rOtherSet " << &rOtherSet << ")");
00044   mpSymbolTable = rOtherSet.mpSymbolTable;
00045   Assign(rOtherSet);
00046   FD_DC("NameSet(" << this << ")::NameSet(rOtherSet " << &rOtherSet << "): done");
00047 }
00048 
00049 // read file constructor
00050 NameSet::NameSet(const std::string& rFilename, const std::string& rLabel) : TBaseSet<Idx>() {
00051   FD_DC("NameSet(" << this << ")::NameSet(" << rFilename << ")");
00052   mpSymbolTable = SymbolTable::GlobalEventSymbolTablep();
00053   Read(rFilename, rLabel);
00054 }
00055 
00056 // destructor
00057 NameSet::~NameSet(void) {
00058   FD_DC("NameSet("<<this<<")::~NameSet()");
00059 }
00060 
00061 // New()
00062 NameSet* NameSet::New(void) const {
00063   NameSet* res= new NameSet();
00064   res->mpSymbolTable=mpSymbolTable;
00065   return res;
00066 }
00067 
00068 // NewN()
00069 NameSet NameSet::NewN(void) const {
00070   NameSet res;
00071   res.mpSymbolTable=mpSymbolTable;
00072   return res;
00073 }
00074 
00075 // copy (attributes to default)
00076 NameSet& NameSet::DoAssign(const NameSet& rSourceSet) {
00077   FD_DC("NameSet(" << this << ")::DoAssign(..)");
00078   // fix my symboltable
00079   mpSymbolTable=rSourceSet.mpSymbolTable;
00080   // call base 
00081   TBaseSet<Idx>::DoAssign(rSourceSet);
00082   // done
00083   return *this;
00084 } 
00085 
00086 // Compare
00087 bool NameSet::DoEqual(const NameSet& rOtherSet) const {
00088 #ifdef FAUDES_CHECKED
00089   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00090     std::stringstream errstr;
00091     errstr << "symboltable mismazch aka not implemented" << std::endl;
00092     throw Exception("NameSet::Operator==", errstr.str(), 67);
00093   }
00094 #endif
00095   return TBaseSet<Idx>::DoEqual(rOtherSet);
00096 }
00097 
00098 // SymbolTablep()
00099 SymbolTable* NameSet::SymbolTablep(void) const {
00100   return mpSymbolTable;
00101 }
00102 
00103 // SymbolTablep(pSymTab)
00104 void NameSet::SymbolTablep(SymbolTable* pSymTab) {
00105   FD_DC("NameSet(" << this << ")::SymbolTablep(" << pSymTab << ")");
00106   if(!Empty()) {
00107     Clear();
00108   } 
00109   mpSymbolTable=pSymTab;
00110 }
00111 
00112 // DoWrite(tw, rLabel)
00113 void NameSet::DoWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
00114   std::string label=rLabel;
00115   if(label=="") label=Name(); 
00116   FD_DC("NameSet(" << this << ")::DoWrite(..): section " << label << " #" << Size());
00117   tw.WriteBegin(label);
00118   Token token;
00119   Iterator it;
00120   for (it = Begin(); it != End(); ++it) {
00121 #ifdef FAUDES_DEBUG_CODE
00122     if (SymbolicName(*it) == "") {
00123       FD_ERR("NameSet::Write(): "
00124      << "index " << *it << " not in SymbolTable. aborting...");
00125       abort();
00126     }
00127 #endif
00128     token.SetString(SymbolicName(*it));
00129     tw << token;
00130     Attribute(*it).Write(tw,"",pContext);
00131   }
00132   tw.WriteEnd(label);
00133 }
00134 
00135 // DoDWrite()
00136 void NameSet::DoDWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
00137   std::string label=rLabel;
00138   if(label=="") label=Name(); 
00139   tw.WriteBegin(label);
00140   Token token;
00141   Iterator it;
00142   for (it = Begin(); it != End(); ++it) {
00143     tw << Str(*it);
00144     Attribute(*it).Write(tw,"",pContext);
00145   }
00146   tw.WriteEnd(Name());
00147 }
00148 
00149 // DoRead(rTr, rLabel, pContext)
00150 void NameSet::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
00151   Token token;
00152   // figure section
00153   std::string label=rLabel;
00154   if(label=="") {
00155     rTr.Peek(token);
00156     if(token.Type()==Token::Begin) label=token.StringValue();
00157   }
00158   if(label=="") label="NameSet"; 
00159   Name(label);
00160   FD_DC("NameSet(" << this << ")::DoRead(..): section " << label << " with symtab " << mpSymbolTable);
00161   // prepare attribute
00162   AttributeVoid* attrp = Attribute().New();
00163   FD_DC("NameSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
00164   // loop tokens
00165   std::string name;
00166   rTr.SeekBegin(label); 
00167   while(rTr.Peek(token)) {
00168     // break on end
00169     if (token.Type() == Token::End) {
00170       break;
00171     }
00172     // read by name
00173     if (token.Type() == Token::String) {
00174       rTr >> token;
00175       FD_DC("TaNameSet(" << this << ")::DoRead(..): inserting element \"" 
00176       << token.StringValue() << "\"");
00177       name=token.StringValue();
00178       // read attribute
00179       attrp->Read(rTr,"",pContext);
00180       // skip unknown attributes
00181       AttributeVoid::Skip(rTr);
00182       // insert element with attribute
00183       Idx index=Insert(name); 
00184       Attribute(index,*attrp);
00185       continue;
00186     }
00187     // cannot process token
00188     delete attrp;
00189     std::stringstream errstr;
00190     errstr << "Invalid token of type " << token.Type() << " at " << rTr.FileLine();
00191     throw Exception("NameSet::DoRead", errstr.str(), 50);
00192   }
00193   rTr.SeekEnd(label);
00194   delete attrp;
00195   FD_DC("NameSet(" << this << ")::DoRead(tr," << label << ", " << pContext << "): done");
00196 }
00197 
00198 
00199 // Insert(index)
00200 bool NameSet::Insert(const Idx& index) { 
00201 #ifdef FAUDES_CHECKED
00202   if(!mpSymbolTable->Exists(index)) {
00203     std::stringstream errstr;
00204     errstr << "index " << index << " not known to symboltable" << std::endl;
00205     throw Exception("NameSet::Insert", errstr.str(), 65);
00206   }
00207 #endif
00208   return TBaseSet<Idx>::Insert(index);
00209 }
00210 
00211 // Insert(rName)
00212 Idx NameSet::Insert(const std::string& rName) {
00213   FD_DC("NameSet(" << this << ")::Insert(" << rName <<")");
00214   Idx index = mpSymbolTable->InsEntry(rName);
00215   TBaseSet<Idx>::Insert(index);
00216   return index;
00217 }
00218 
00219 // InsertSet(set)
00220 void NameSet::InsertSet(const NameSet& rOtherSet) {
00221   FD_DC("NameSet(" << this << ")::InsertSet(" << rOtherSet.ToString() << ")");
00222 #ifdef FAUDES_CHECKED
00223   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00224     std::stringstream errstr;
00225     errstr << "symboltable mismatch aka not implemented" << std::endl;
00226     throw Exception("NameSet::InsertSet", errstr.str(), 67);
00227   }
00228 #endif
00229   TBaseSet<Idx>::InsertSet(rOtherSet);
00230 }
00231     
00232 // Erase(index)
00233 bool NameSet::Erase(const Idx& index) {
00234   FD_DC("NameSet(" << this << ")::Erase(" << index <<")");
00235   return TBaseSet<Idx>::Erase(index);
00236 }
00237 
00238 // Erase(rName)
00239 bool NameSet::Erase(const std::string& rName) {
00240   FD_DC("NameSet(" << this << ")::Erase(" << rName <<")");
00241   Idx index = mpSymbolTable->Index(rName);
00242 #ifdef FAUDES_CHECKED
00243   if (index == 0) {
00244     std::stringstream errstr;
00245     errstr << "name \"" << rName << "\" not found in NameSet" << std::endl;
00246     throw Exception("NameSet::Erase", errstr.str(), 66);
00247   }
00248 #endif
00249   return TBaseSet<Idx>::Erase(index);
00250 }
00251 
00252 // Erase(pos)
00253 NameSet::Iterator NameSet::Erase(const Iterator& pos) {
00254   return TBaseSet<Idx>::Erase(pos);
00255 }
00256 
00257 // EraseSet(set)
00258 void NameSet::EraseSet(const NameSet& rOtherSet) {
00259 #ifdef FAUDES_CHECKED
00260   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00261     std::stringstream errstr;
00262     errstr << "symboltable mismatch aka not implemented" << std::endl;
00263     throw Exception("NameSet::EraseSet", errstr.str(), 67);
00264   }
00265 #endif
00266   TBaseSet<Idx>::EraseSet(rOtherSet);
00267 }
00268 
00269 // RestrictSet(set)
00270 void NameSet::RestrictSet(const NameSet& rOtherSet) {
00271 #ifdef FAUDES_CHECKED
00272   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00273     std::stringstream errstr;
00274     errstr << "symboltable mismatch aka not implemented" << std::endl;
00275     throw Exception("NameSet::RestrictSet", errstr.str(), 67);
00276   }
00277 #endif
00278   TBaseSet<Idx>::RestrictSet(rOtherSet);
00279 }
00280 
00281 // SymbolicName(index)
00282 std::string NameSet::SymbolicName(Idx index) const {
00283   return mpSymbolTable->Symbol(index);
00284 }
00285 
00286 // SymbolicName(index, name)
00287 void NameSet::SymbolicName(Idx index, const std::string& rName) {
00288   FD_DC("NameSet(" << this << ")::SymbolicName(" << index << ", " << rName <<")");
00289 #ifdef FAUDES_CHECKED
00290   if (! Exists(index)) {
00291     std::stringstream errstr;
00292     errstr << "index " << index << " not in this set" << std::endl;
00293     throw Exception("NameSet::SymbolicName", errstr.str(), 60);
00294   }
00295 #endif
00296   mpSymbolTable->SetEntry(index, rName);
00297 }
00298 
00299 // SymbolicName(name, name)
00300 void NameSet::SymbolicName(const std::string& rName, 
00301           const std::string& rNewName) {
00302   FD_DC("NameSet(" << this << ")::SymbolicName(" << rName << ", " 
00303   << rNewName <<")");
00304 #ifdef FAUDES_CHECKED
00305   if (! Exists(rName)) {
00306     std::stringstream errstr;
00307     errstr << "name \"" << rName << "\" not found in NameSet" << std::endl;
00308     throw Exception("NameSet::Symbolic", errstr.str(), 66);
00309   }
00310 #endif
00311   mpSymbolTable->SetEntry(Index(rName), rNewName);
00312 }
00313 
00314 // Index(rName)
00315 Idx NameSet::Index(const std::string& rName) const {
00316   return mpSymbolTable->Index(rName);
00317 }
00318 
00319 // Exists(index)
00320 bool NameSet::Exists(const Idx& index) const {
00321   return TBaseSet<Idx>::Exists(index);
00322 }
00323 
00324 // Exists(rName)
00325 bool NameSet::Exists(const std::string& rName) const {
00326   return TBaseSet<Idx>::Exists(mpSymbolTable->Index(rName)) ;
00327 }
00328 
00329 // Find(index) const
00330 NameSet::Iterator NameSet::Find(const Idx& index) const {
00331   return TBaseSet<Idx>::Find(index);
00332 }
00333 
00334 // Find(rName) const
00335 NameSet::Iterator NameSet::Find(const std::string& rName) const {
00336   return TBaseSet<Idx>::Find(mpSymbolTable->Index(rName));
00337 }
00338 
00339 
00340 // operator +
00341 NameSet NameSet::operator + (const NameSet& rOtherSet) const {
00342   FD_DC("NameSet(" << this << ")::operator + (" << &rOtherSet << ")");
00343 #ifdef FAUDES_CHECKED
00344   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00345     std::stringstream errstr;
00346     errstr << "symboltable mismazch aka not implemented" << std::endl;
00347     throw Exception("NameSet::Operator+", errstr.str(), 67);
00348   }
00349 #endif
00350   NameSet res= NewN();
00351   // effectively, we want "res.Assign( TBaseSet<Idx>::operator + (rOtherSet) )"
00352   // here: explicit version, to save one constructor --- is it worth?
00353   res.Detach();
00354   std::insert_iterator< std::set<Idx> > insit(*res.pSet, res.pSet->begin());
00355   std::set_union(pSet->begin(), pSet->end(), rOtherSet.pSet->begin(), rOtherSet.pSet->end(), insit);
00356   res.Name(CollapsString(Name() + "+" + rOtherSet.Name()));
00357   return res;
00358 }
00359 
00360 // operator -
00361 NameSet NameSet::operator - (const NameSet& rOtherSet) const {
00362   FD_DC("NameSet(" << this << ")::operator - (" << &rOtherSet << ")");
00363 #ifdef FAUDES_CHECKED
00364   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00365     std::stringstream errstr;
00366     errstr << "symboltable mismazch aka not implemented" << std::endl;
00367     throw Exception("NameSet::Operator-", errstr.str(), 67);
00368   }
00369 #endif
00370   NameSet res= NewN();
00371   // effectively, we want: res.Assign( TBaseSet<Idx>::operator - (rOtherSet) );
00372   // here: explicit version, to save one constructor --- is it worth?
00373   res.Detach();
00374   std::insert_iterator< std::set<Idx> > insit(*res.pSet, res.pSet->begin());
00375   std::set_difference(pSet->begin(), pSet->end(), rOtherSet.pSet->begin(), rOtherSet.pSet->end(), insit);
00376   res.Name(CollapsString(Name() + "-" + rOtherSet.Name()));
00377   return res;
00378 }
00379 
00380 // operator *
00381 NameSet NameSet::operator * (const NameSet& rOtherSet) const {
00382   FD_DC("NameSet(" << this << ")::operator * (" << &rOtherSet << ")");
00383 #ifdef FAUDES_CHECKED
00384   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00385     std::stringstream errstr;
00386     errstr << "symboltable mismazch aka not implemented" << std::endl;
00387     throw Exception("NameSet::Operator*", errstr.str(), 67);
00388   }
00389 #endif
00390   NameSet res= NewN();
00391   // effectively, we want res.Assign( TBaseSet<Idx>::operator * (rOtherSet) );
00392   // here: explicit version, to save one constructor --- is it worth?
00393   res.Detach();
00394   std::insert_iterator< std::set<Idx> > insit(*res.pSet, res.pSet->begin());
00395   std::set_intersection(pSet->begin(), pSet->end(), rOtherSet.pSet->begin(), rOtherSet.pSet->end(), insit);
00396   res.Name(CollapsString(Name() + "*" + rOtherSet.Name()));
00397   return res;
00398 }
00399 
00400 // operator <=
00401 bool NameSet::operator <= (const NameSet& rOtherSet) const {
00402 #ifdef FAUDES_CHECKED
00403   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00404     std::stringstream errstr;
00405     errstr << "symboltable mismazch aka not implemented" << std::endl;
00406     throw Exception("NameSet::Operator<=", errstr.str(), 67);
00407   }
00408 #endif
00409   return TBaseSet<Idx>::operator <= (rOtherSet);
00410 }
00411 
00412 // operator >=
00413 bool NameSet::operator >= (const NameSet& rOtherSet) const {
00414 #ifdef FAUDES_CHECKED
00415   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00416     std::stringstream errstr;
00417     errstr << "symboltable mismazch aka not implemented" << std::endl;
00418     throw Exception("NameSet::Operator>=", errstr.str(), 67);
00419   }
00420 #endif
00421   return TBaseSet<Idx>::operator >= (rOtherSet);
00422 }
00423 
00424 // Str(index)
00425 std::string NameSet::Str(const Idx& index) const {
00426   return mpSymbolTable->Symbol(index)+"["+faudes::ToStringInteger(index)+"]";
00427 }
00428 
00429 
00430 
00431 
00432 } // namespace faudes
00433 
00434 

libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6