libFAUDES

Sections

Index

cfl_nameset.cpp

Go to the documentation of this file.
00001 /** @file cfl_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 "cfl_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::DoEqual()", errstr.str(), 67);
00093   }
00094 #endif
00095   FD_DC("NameSet::DoEqual(): " << TBaseSet<Idx>::DoEqual(rOtherSet));
00096   return TBaseSet<Idx>::DoEqual(rOtherSet);
00097 }
00098 
00099 // SymbolTablep()
00100 SymbolTable* NameSet::SymbolTablep(void) const {
00101   return mpSymbolTable;
00102 }
00103 
00104 // SymbolTablep(pSymTab)
00105 void NameSet::SymbolTablep(SymbolTable* pSymTab) {
00106   FD_DC("NameSet(" << this << ")::SymbolTablep(" << pSymTab << ")");
00107   if(!Empty()) {
00108     Clear();
00109   } 
00110   mpSymbolTable=pSymTab;
00111 }
00112 
00113 // DoWrite(tw, rLabel)
00114 void NameSet::DoWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
00115   std::string label=rLabel;
00116   if(label=="") label=Name(); 
00117   FD_DC("NameSet(" << this << ")::DoWrite(..): section " << label << " #" << Size());
00118   tw.WriteBegin(label);
00119   Token token;
00120   Iterator it;
00121   for (it = Begin(); it != End(); ++it) {
00122 #ifdef FAUDES_DEBUG_CODE
00123     if (SymbolicName(*it) == "") {
00124       FD_ERR("NameSet::Write(): "
00125      << "index " << *it << " not in SymbolTable. aborting...");
00126       abort();
00127     }
00128 #endif
00129     token.SetString(SymbolicName(*it));
00130     tw << token;
00131     Attribute(*it).Write(tw,"",pContext);
00132   }
00133   tw.WriteEnd(label);
00134 }
00135 
00136 // DoDWrite()
00137 void NameSet::DoDWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
00138   std::string label=rLabel;
00139   if(label=="") label=Name(); 
00140   tw.WriteBegin(label);
00141   Token token;
00142   Iterator it;
00143   for (it = Begin(); it != End(); ++it) {
00144     tw << Str(*it);
00145     Attribute(*it).Write(tw,"",pContext);
00146   }
00147   tw.WriteEnd(Name());
00148 }
00149 
00150 // DoRead(rTr, rLabel, pContext)
00151 void NameSet::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
00152   Token token;
00153   // figure section
00154   std::string label=rLabel;
00155   if(label=="") {
00156     rTr.Peek(token);
00157     if(token.Type()==Token::Begin) label=token.StringValue();
00158   }
00159   if(label=="") label="NameSet"; 
00160   Name(label);
00161   FD_DC("NameSet(" << this << ")::DoRead(..): section " << label << " with symtab " << mpSymbolTable);
00162   // prepare attribute
00163   AttributeVoid* attrp = Attribute().New();
00164   FD_DC("NameSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
00165   // loop tokens
00166   std::string name;
00167   rTr.SeekBegin(label); 
00168   while(rTr.Peek(token)) {
00169     // break on end
00170     if (token.Type() == Token::End) {
00171       break;
00172     }
00173     // read by name
00174     if (token.Type() == Token::String) {
00175       rTr >> token;
00176       FD_DC("TaNameSet(" << this << ")::DoRead(..): inserting element \"" 
00177       << token.StringValue() << "\"");
00178       name=token.StringValue();
00179       // read attribute
00180       attrp->Read(rTr,"",pContext);
00181       // skip unknown attributes
00182       AttributeVoid::Skip(rTr);
00183       // insert element with attribute
00184       Idx index=Insert(name); 
00185       Attribute(index,*attrp);
00186       continue;
00187     }
00188     // cannot process token
00189     delete attrp;
00190     std::stringstream errstr;
00191     errstr << "Invalid token of type " << token.Type() << " at " << rTr.FileLine();
00192     throw Exception("NameSet::DoRead", errstr.str(), 50);
00193   }
00194   rTr.SeekEnd(label);
00195   delete attrp;
00196   FD_DC("NameSet(" << this << ")::DoRead(tr," << label << ", " << pContext << "): done");
00197 }
00198 
00199 
00200 // Insert(index)
00201 bool NameSet::Insert(const Idx& index) { 
00202 #ifdef FAUDES_CHECKED
00203   if(!mpSymbolTable->Exists(index)) {
00204     std::stringstream errstr;
00205     errstr << "index " << index << " not known to symboltable" << std::endl;
00206     throw Exception("NameSet::Insert", errstr.str(), 65);
00207   }
00208 #endif
00209   return TBaseSet<Idx>::Insert(index);
00210 }
00211 
00212 // Insert(rName)
00213 Idx NameSet::Insert(const std::string& rName) {
00214   FD_DC("NameSet(" << this << ")::Insert(" << rName <<")");
00215   Idx index = mpSymbolTable->InsEntry(rName);
00216   TBaseSet<Idx>::Insert(index);
00217   return index;
00218 }
00219 
00220 // InsertSet(set)
00221 void NameSet::InsertSet(const NameSet& rOtherSet) {
00222   FD_DC("NameSet(" << this << ")::InsertSet(" << rOtherSet.ToString() << ")");
00223 #ifdef FAUDES_CHECKED
00224   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00225     std::stringstream errstr;
00226     errstr << "symboltable mismatch aka not implemented" << std::endl;
00227     throw Exception("NameSet::InsertSet", errstr.str(), 67);
00228   }
00229 #endif
00230   TBaseSet<Idx>::InsertSet(rOtherSet);
00231 }
00232     
00233 // Erase(index)
00234 bool NameSet::Erase(const Idx& index) {
00235   FD_DC("NameSet(" << this << ")::Erase(" << index <<")");
00236   return TBaseSet<Idx>::Erase(index);
00237 }
00238 
00239 // Erase(rName)
00240 bool NameSet::Erase(const std::string& rName) {
00241   FD_DC("NameSet(" << this << ")::Erase(" << rName <<")");
00242   Idx index = mpSymbolTable->Index(rName);
00243 #ifdef FAUDES_CHECKED
00244   if (index == 0) {
00245     std::stringstream errstr;
00246     errstr << "name \"" << rName << "\" not found in NameSet" << std::endl;
00247     throw Exception("NameSet::Erase", errstr.str(), 66);
00248   }
00249 #endif
00250   return TBaseSet<Idx>::Erase(index);
00251 }
00252 
00253 // Erase(pos)
00254 NameSet::Iterator NameSet::Erase(const Iterator& pos) {
00255   return TBaseSet<Idx>::Erase(pos);
00256 }
00257 
00258 // EraseSet(set)
00259 void NameSet::EraseSet(const NameSet& rOtherSet) {
00260 #ifdef FAUDES_CHECKED
00261   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00262     std::stringstream errstr;
00263     errstr << "symboltable mismatch aka not implemented" << std::endl;
00264     throw Exception("NameSet::EraseSet", errstr.str(), 67);
00265   }
00266 #endif
00267   TBaseSet<Idx>::EraseSet(rOtherSet);
00268 }
00269 
00270 // RestrictSet(set)
00271 void NameSet::RestrictSet(const NameSet& rOtherSet) {
00272 #ifdef FAUDES_CHECKED
00273   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00274     std::stringstream errstr;
00275     errstr << "symboltable mismatch aka not implemented" << std::endl;
00276     throw Exception("NameSet::RestrictSet", errstr.str(), 67);
00277   }
00278 #endif
00279   TBaseSet<Idx>::RestrictSet(rOtherSet);
00280 }
00281 
00282 // SymbolicName(index)
00283 std::string NameSet::SymbolicName(Idx index) const {
00284   return mpSymbolTable->Symbol(index);
00285 }
00286 
00287 // SymbolicName(index, name)
00288 void NameSet::SymbolicName(Idx index, const std::string& rName) {
00289   FD_DC("NameSet(" << this << ")::SymbolicName(" << index << ", " << rName <<")");
00290 #ifdef FAUDES_CHECKED
00291   if (! Exists(index)) {
00292     std::stringstream errstr;
00293     errstr << "index " << index << " not in this set" << std::endl;
00294     throw Exception("NameSet::SymbolicName", errstr.str(), 60);
00295   }
00296 #endif
00297   mpSymbolTable->SetEntry(index, rName);
00298 }
00299 
00300 // SymbolicName(name, name)
00301 void NameSet::SymbolicName(const std::string& rName, 
00302           const std::string& rNewName) {
00303   FD_DC("NameSet(" << this << ")::SymbolicName(" << rName << ", " 
00304   << rNewName <<")");
00305 #ifdef FAUDES_CHECKED
00306   if (! Exists(rName)) {
00307     std::stringstream errstr;
00308     errstr << "name \"" << rName << "\" not found in NameSet" << std::endl;
00309     throw Exception("NameSet::Symbolic", errstr.str(), 66);
00310   }
00311 #endif
00312   mpSymbolTable->SetEntry(Index(rName), rNewName);
00313 }
00314 
00315 // Index(rName)
00316 Idx NameSet::Index(const std::string& rName) const {
00317   return mpSymbolTable->Index(rName);
00318 }
00319 
00320 // Exists(index)
00321 bool NameSet::Exists(const Idx& index) const {
00322   return TBaseSet<Idx>::Exists(index);
00323 }
00324 
00325 // Exists(rName)
00326 bool NameSet::Exists(const std::string& rName) const {
00327   return TBaseSet<Idx>::Exists(mpSymbolTable->Index(rName)) ;
00328 }
00329 
00330 // Find(index) const
00331 NameSet::Iterator NameSet::Find(const Idx& index) const {
00332   return TBaseSet<Idx>::Find(index);
00333 }
00334 
00335 // Find(rName) const
00336 NameSet::Iterator NameSet::Find(const std::string& rName) const {
00337   return TBaseSet<Idx>::Find(mpSymbolTable->Index(rName));
00338 }
00339 
00340 
00341 // operator +
00342 NameSet NameSet::operator + (const NameSet& rOtherSet) const {
00343   FD_DC("NameSet(" << this << ")::operator + (" << &rOtherSet << ")");
00344 #ifdef FAUDES_CHECKED
00345   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00346     std::stringstream errstr;
00347     errstr << "symboltable mismazch aka not implemented" << std::endl;
00348     throw Exception("NameSet::Operator+", errstr.str(), 67);
00349   }
00350 #endif
00351   NameSet res= NewN();
00352   // effectively, we want "res.Assign( TBaseSet<Idx>::operator + (rOtherSet) )"
00353   // here: explicit version, to save one constructor --- is it worth?
00354   res.Detach();
00355   std::insert_iterator< std::set<Idx> > insit(*res.pSet, res.pSet->begin());
00356   std::set_union(pSet->begin(), pSet->end(), rOtherSet.pSet->begin(), rOtherSet.pSet->end(), insit);
00357   res.Name(CollapsString(Name() + "+" + rOtherSet.Name()));
00358   return res;
00359 }
00360 
00361 // operator -
00362 NameSet NameSet::operator - (const NameSet& rOtherSet) const {
00363   FD_DC("NameSet(" << this << ")::operator - (" << &rOtherSet << ")");
00364 #ifdef FAUDES_CHECKED
00365   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00366     std::stringstream errstr;
00367     errstr << "symboltable mismazch aka not implemented" << std::endl;
00368     throw Exception("NameSet::Operator-", errstr.str(), 67);
00369   }
00370 #endif
00371   NameSet res= NewN();
00372   // effectively, we want: res.Assign( TBaseSet<Idx>::operator - (rOtherSet) );
00373   // here: explicit version, to save one constructor --- is it worth?
00374   res.Detach();
00375   std::insert_iterator< std::set<Idx> > insit(*res.pSet, res.pSet->begin());
00376   std::set_difference(pSet->begin(), pSet->end(), rOtherSet.pSet->begin(), rOtherSet.pSet->end(), insit);
00377   res.Name(CollapsString(Name() + "-" + rOtherSet.Name()));
00378   return res;
00379 }
00380 
00381 // operator *
00382 NameSet NameSet::operator * (const NameSet& rOtherSet) const {
00383   FD_DC("NameSet(" << this << ")::operator * (" << &rOtherSet << ")");
00384 #ifdef FAUDES_CHECKED
00385   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00386     std::stringstream errstr;
00387     errstr << "symboltable mismazch aka not implemented" << std::endl;
00388     throw Exception("NameSet::Operator*", errstr.str(), 67);
00389   }
00390 #endif
00391   NameSet res= NewN();
00392   // effectively, we want res.Assign( TBaseSet<Idx>::operator * (rOtherSet) );
00393   // here: explicit version, to save one constructor --- is it worth?
00394   res.Detach();
00395   std::insert_iterator< std::set<Idx> > insit(*res.pSet, res.pSet->begin());
00396   std::set_intersection(pSet->begin(), pSet->end(), rOtherSet.pSet->begin(), rOtherSet.pSet->end(), insit);
00397   res.Name(CollapsString(Name() + "*" + rOtherSet.Name()));
00398   return res;
00399 }
00400 
00401 // operator <=
00402 bool NameSet::operator <= (const NameSet& rOtherSet) const {
00403 #ifdef FAUDES_CHECKED
00404   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00405     std::stringstream errstr;
00406     errstr << "symboltable mismazch aka not implemented" << std::endl;
00407     throw Exception("NameSet::Operator<=", errstr.str(), 67);
00408   }
00409 #endif
00410   return TBaseSet<Idx>::operator <= (rOtherSet);
00411 }
00412 
00413 // operator >=
00414 bool NameSet::operator >= (const NameSet& rOtherSet) const {
00415 #ifdef FAUDES_CHECKED
00416   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00417     std::stringstream errstr;
00418     errstr << "symboltable mismazch aka not implemented" << std::endl;
00419     throw Exception("NameSet::Operator>=", errstr.str(), 67);
00420   }
00421 #endif
00422   return TBaseSet<Idx>::operator >= (rOtherSet);
00423 }
00424 
00425 // Str(index)
00426 std::string NameSet::Str(const Idx& index) const {
00427   return mpSymbolTable->Symbol(index)+"["+faudes::ToStringInteger(index)+"]";
00428 }
00429 
00430 
00431 
00432 
00433 } // namespace faudes
00434 
00435 

libFAUDES 2.16b --- 2010-9-8 --- c++ source docu by doxygen 1.6.3