cfl_indexset.cpp

Go to the documentation of this file.
00001 /** @file cfl_indexset.cpp @brief Classes IndexSet */
00002 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005    Copyright (C) 2006  Bernd Opitz
00006    Copyright (C) 2007  Thomas Moor
00007    Exclusive copyright is granted to Klaus Schmidt
00008 
00009    This library is free software; you can redistribute it and/or
00010    modify it under the terms of the GNU Lesser General Public
00011    License as published by the Free Software Foundation; either
00012    version 2.1 of the License, or (at your option) any later version.
00013 
00014    This library is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017    Lesser General Public License for more details.
00018 
00019    You should have received a copy of the GNU Lesser General Public
00020    License along with this library; if not, write to the Free Software
00021    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00022 
00023 #include "cfl_indexset.h"
00024 
00025 
00026 namespace faudes {
00027 
00028 /*
00029 *********************************************************************************
00030 *********************************************************************************
00031 *********************************************************************************
00032 
00033  IndexSet 
00034 
00035 *********************************************************************************
00036 *********************************************************************************
00037 *********************************************************************************
00038 */
00039 
00040 // std faudes type 
00041 FAUDES_TYPE_IMPLEMENTATION(IndexSet,IndexSet,TBaseSet<Idx>)
00042 
00043 /*
00044 FAUDES_TYPE_IMPLEMENTATION_NEW(IndexSet,IndexSet,TBaseSet<Idx>)
00045 FAUDES_TYPE_IMPLEMENTATION_COPY(IndexSet,IndexSet,TBaseSet<Idx>)
00046 FAUDES_TYPE_IMPLEMENTATION_CAST(IndexSet,IndexSet,TBaseSet<Idx>)
00047 FAUDES_TYPE_IMPLEMENTATION_ASSIGN(IndexSet,IndexSet,TBaseSet<Idx>)
00048 FAUDES_TYPE_IMPLEMENTATION_EQUAL(IndexSet,IndexSet,TBaseSet<Idx>)
00049 FAUDES_TYPE_IMPLEMENTATION_REGISTER(IndexSet,IndexSet,TBaseSet<Idx>)
00050 */
00051 
00052 
00053 
00054 // IndexSet()
00055 IndexSet::IndexSet(void) : TBaseSet<Idx>() {
00056   FD_DC("IndexSet(" << this << ")::IndexSet()");
00057   Name("IndexSet");
00058 }
00059 
00060 // IndexSet(rOtherSet)
00061 IndexSet::IndexSet(const IndexSet& rOtherSet) : 
00062   TBaseSet<Idx>(rOtherSet) 
00063 {
00064   FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
00065   // copy my members (none)
00066 }
00067 
00068 // IndexSet(rOtherSet)
00069 IndexSet::IndexSet(const TBaseSet<Idx>& rOtherSet) : 
00070   TBaseSet<Idx>(rOtherSet) 
00071 {
00072   FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
00073   // copy my members (none)
00074 }
00075 
00076 // File constructor
00077 IndexSet::IndexSet(const std::string& rFilename, const std::string& rLabel) :
00078   TBaseSet<Idx>() 
00079 {
00080   FD_DC("IndexSet(" << this << ")::IndexSet(" << rFilename << ")");
00081   Read(rFilename, rLabel);
00082 }
00083 
00084 // assignment (attributes to default)
00085 void IndexSet::DoAssign(const IndexSet& rSourceSet) {
00086   FD_DC("NameSet(" << this << ")::DoAssign(..)");
00087   // call base 
00088   TBaseSet<Idx>::DoAssign(rSourceSet);
00089 } 
00090 
00091 // DoWrite(rTw&)
00092 void IndexSet::DoWrite(TokenWriter& rTw, const std::string& rLabel,const Type* pContext) const {
00093   // figure section
00094   std::string label=rLabel;
00095   if(label=="") label=Name(); 
00096   if(label=="") label="IndexSet"; 
00097   FD_DC("IndexSet(" << this << ")::DoWrite(..): section " << label);
00098   /*
00099   // figure context: default to non
00100   const SymbolTable* symboltable=0;
00101   const std::map<Idx,Idx>* indexmap=0;  
00102   // figure context: can use vgenerator
00103   if(const Generator* vgen=dynamic_cast<const Generator*>(pContext)) {
00104     FD_DC("TaIndexSet(" << this << ")::DoWrite(..): generator context");
00105     symboltable=&vgen->StateSymbolTable();
00106     indexmap=&vgen->MinStateIndexMap();
00107   }
00108   */
00109   rTw.WriteBegin(label);
00110   Iterator it, conit;
00111   // iterate states to write
00112   for (it = Begin(); it != End(); ++it) {
00113     // identify consecutive block
00114     Idx start = *it;
00115     Idx anoncount = 0;
00116     for(conit=it; conit != End(); ++conit) {
00117       if(!Attribute(*conit).IsDefault()) break;
00118       if(*conit != start+anoncount) break;
00119       ++anoncount;
00120     }
00121     // write consecutive block
00122     if (anoncount > FD_CONSECUTIVE) {
00123       rTw.WriteBegin("Consecutive");
00124       rTw << start;
00125       rTw << start+anoncount-1;
00126       rTw.WriteEnd("Consecutive");
00127       it=conit;
00128     } 
00129     // break loop
00130     if(it == End() )
00131       break;
00132     // write individual state
00133     rTw << *it;
00134     // write state attribute
00135     const AttributeVoid& attr=Attribute(*it);
00136     attr.Write(rTw,"",pContext);
00137   }
00138   rTw.WriteEnd(label);
00139 }
00140 
00141 // DoXWrite(rTw&)
00142 void IndexSet::DoXWrite(TokenWriter& rTw, const std::string& rLabel,const Type* pContext) const {
00143   // Set up outer tag
00144   Token btag=XBeginTag(rLabel,"IndexSet");
00145   rTw.Write(btag);
00146   FD_DC("IndexSet(" << this << ")::DoXWrite(..): section " << btag.StringValue() << " #" << Size());
00147 
00148   /*
00149   // figure context: default to non
00150   const SymbolTable* symboltable=0;
00151   const std::map<Idx,Idx>* indexmap=0;  
00152   // figure context: can use vgenerator
00153   if(const Generator* vgen=dynamic_cast<const Generator*>(pContext)) {
00154     FD_DC("TaIndexSet(" << this << ")::DoWrite(..): generator context");
00155     symboltable=&vgen->StateSymbolTable();
00156     indexmap=&vgen->MinStateIndexMap();
00157   }
00158   */
00159   // Loop elements
00160   Iterator it, conit;
00161   std::string etstr=XElementTag();
00162   for(it = Begin(); it != End(); ++it) {
00163     // identify consecutive block
00164     Idx start = *it;
00165     Idx anoncount = 0;
00166     for(conit=it; conit != End(); ++conit) {
00167       if(!Attribute(*conit).IsDefault()) break;
00168       if(*conit != start+anoncount) break;
00169       ++anoncount;
00170     }
00171     // write anonymous block
00172     if(anoncount > FD_CONSECUTIVE) {
00173       Token contag;
00174       contag.SetEmpty("Consecutive");
00175       contag.InsAttributeInteger("from",start);
00176       contag.InsAttributeInteger("to",start+anoncount-1);
00177       rTw.Write(contag);
00178       it=conit;
00179     } 
00180     // break loop
00181     if(it == End()) break;
00182     // prepare token
00183     Token sttag;    
00184     Idx index=*it;
00185     sttag.SetBegin(etstr);
00186     sttag.InsAttributeInteger("id",index);
00187     rTw.Write(sttag);
00188     // attribute
00189     const AttributeVoid& attr=Attribute(index);
00190     if(!attr.IsDefault()) attr.XWrite(rTw);
00191     // done
00192     rTw.WriteEnd(etstr);
00193   }
00194   rTw.WriteEnd(btag.StringValue());
00195 }
00196 
00197 // DoRead(rTr, rLabel)
00198 void IndexSet::DoRead(TokenReader& rTr, const std::string& rLabel,const Type* pContext) {
00199    // set up defaults
00200   std::string label=rLabel;
00201   std::string ftype=TypeName();
00202   std::string etstr=XElementTag();
00203   // figure section from current token
00204   Token token;
00205   if(label=="") {
00206     rTr.Peek(token);
00207     if(token.IsBegin()) label=token.StringValue();
00208   }
00209   if(label=="") label="IndexSet"; 
00210   Name(label);
00211   // read begin
00212   FD_DC("IndexSet(" << this << ")::DoRead(..): section " << label);
00213   rTr.ReadBegin(label,token); 
00214   if(token.ExistsAttributeString("name"))
00215     Name(token.AttributeStringValue("name"));
00216   // prepare attribute
00217   AttributeVoid* attrp = Attribute().New();
00218   FD_DC("IndexSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
00219   // loop tokens
00220   while(!rTr.Eos(label)) {
00221     rTr.Peek(token);
00222     // read by index (faudes format)
00223     if(token.IsInteger()) {
00224       FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting element in faudes format \"" 
00225       << token.IntegerValue() << "\"");
00226       rTr.Get(token);
00227       Idx index=token.IntegerValue();
00228       // read attribute
00229       attrp->Read(rTr,"",pContext);
00230       // skip unknown attributes
00231       AttributeVoid::Skip(rTr);
00232       // insert element with attribute
00233       Insert(index); 
00234       Attribute(index,*attrp);
00235       continue;
00236     }
00237     // read consecutive block of anonymous states (both faudes or XML)
00238     if(token.IsBegin("Consecutive")) {
00239       Token ctag;
00240       rTr.ReadBegin("Consecutive",ctag);
00241       // Read up to two tokens for faudes format
00242       Token token1,token2;
00243       rTr.Peek(token1);
00244       if(!token1.IsEnd()) rTr.Get(token1);
00245       rTr.Peek(token2);
00246       if(!token2.IsEnd()) rTr.Get(token2);
00247       rTr.ReadEnd("Consecutive");
00248       // Try to interpret
00249       int from=-1;
00250       int to=-1;
00251       if(token1.IsInteger()) from=token1.IntegerValue();
00252       if(token2.IsInteger()) to=token2.IntegerValue();
00253       if(ctag.ExistsAttributeInteger("from"))
00254         from= ctag.AttributeIntegerValue("from");
00255       if(ctag.ExistsAttributeInteger("to"))
00256         to= ctag.AttributeIntegerValue("to");
00257       // Insist in valid range
00258       if(from<0 || to <0  || to < from) {
00259   delete attrp;
00260   std::stringstream errstr;
00261   errstr << "Invalid range of consecutive indices"  << rTr.FileLine();
00262   throw Exception("IndexSet::DoRead", errstr.str(), 50);
00263       }
00264       FD_DC("IndexSet(" << this << ")::DoRead(..): inserting range " 
00265       << from << " to " to);
00266       for(Idx index = (Idx) from; index <= (Idx) to; ++index) 
00267   Insert(index);
00268       continue;
00269     }
00270     // read element section (XML format)
00271     if(token.IsBegin(etstr)) {
00272       FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting element in xml format \"" 
00273       << token.StringValue() << "\"");
00274       rTr.ReadBegin(etstr,token);
00275       Idx index=token.AttributeIntegerValue("id");
00276       // read faudes attribute
00277       attrp->Read(rTr,"",pContext);
00278       // skip unknown faudes attributes
00279       rTr.ReadEnd(etstr);
00280       // insert element with attribute
00281       Insert(index); 
00282       Attribute(index,*attrp);
00283       continue;
00284     }
00285     // cannot process token
00286     delete attrp;
00287     std::stringstream errstr;
00288     errstr << "Invalid token type " << token.Type() << " at " << rTr.FileLine();
00289     throw Exception("IndexSet::DoRead", errstr.str(), 50);
00290   }
00291   rTr.ReadEnd(label);
00292   // dispose attribute
00293   delete attrp;
00294 }
00295 
00296 
00297 // MaxIndex()
00298 Idx IndexSet::MaxIndex(void) const {
00299   if(Size()==0) return 0;
00300   Idx res =  *(--(End()));
00301 #ifdef FAUDES_CHECKED
00302   if(res==FAUDES_IDX_MAX) {
00303     std::stringstream errstr;
00304     errstr << "IndexSet(): MaxIndex exceeds platform limitations -- reimplement index allocation: \""
00305      << Name() << "\"";
00306     throw Exception("vGenerator::EventReame(name)", errstr.str(), 61);
00307   }
00308 #endif
00309   return res;
00310 }
00311 
00312 // Insert()
00313 Idx IndexSet::Insert(void) {
00314   FD_DC("IndexSet(" << this << ")::Insert()");
00315   Idx index=MaxIndex()+1;
00316   TBaseSet<Idx>::Insert(index);
00317   return index;
00318 }
00319 
00320 //Insert(idx)
00321 bool IndexSet::Insert(const Idx& rIndex) {
00322   return TBaseSet<Idx>::Insert(rIndex);
00323 }
00324 
00325 
00326 //Valid(idx)
00327 bool  IndexSet::Valid(const Idx& rIndex) const {
00328   return rIndex!=0;
00329 }
00330 
00331 
00332 // Signature()
00333 Idx IndexSet::Signature(void) const {
00334   // prepare result
00335   Idx sig = 0;
00336   // helpers:
00337   Idx i = 1;
00338   Iterator it=Begin() ;
00339   Iterator it_end=End() ;
00340   // algorithm:
00341   for(; it != it_end; ++it) {
00342     sig += *it * i;
00343     ++i;
00344   }
00345   return sig;
00346 }
00347 
00348 
00349 
00350 
00351 } // end name space

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen