About
User Reference
C++ API
luafaudes
Developer
Links
libFAUDES online
libFAUDES

Sections

Index

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 // DoRead(rTr, rLabel)
00142 void IndexSet::DoRead(TokenReader& rTr, const std::string& rLabel,const Type* pContext) {
00143   Token token;
00144   // figure section
00145   std::string label=rLabel;
00146   if(label=="") {
00147     rTr.Peek(token);
00148     if(token.Type()==Token::Begin) label=token.StringValue();
00149   }
00150   if(label=="") label="IndexSet"; 
00151   Name(label);
00152   FD_DC("IndexSet(" << this << ")::DoRead(..): section " << label);
00153   // prepare attribute
00154   AttributeVoid* attrp = Attribute().New();
00155   FD_DC("IndexSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
00156   // loop tokens
00157   rTr.ReadBegin(label); 
00158   while(rTr.Peek(token)) {
00159     // break on end
00160     if (token.Type() == Token::End) {
00161       break;
00162     }
00163     // read individual index with attribute
00164     if (token.Type() == Token::Integer) {
00165       rTr.Get(token);
00166       FD_DC("IndexSet(" << this << ")::DoRead(..): inserting index \"" 
00167       << token.IntegerValue() << "\"");
00168       Idx index=token.IntegerValue();
00169       // read attribute
00170       attrp->Read(rTr,"",pContext);
00171       // skip unknown attributes
00172       AttributeVoid::Skip(rTr);
00173       // insert element with attribute
00174       Insert(index); 
00175       Attribute(index,*attrp);
00176       continue;
00177     }
00178     // read consecutive block of anonymous states
00179     if (token.Type() == Token::Begin && token.StringValue() == "Consecutive") {
00180       rTr.ReadBegin("Consecutive");
00181       Token token1,token2;
00182       rTr.Get(token1);
00183       rTr.Get(token2);
00184       if((token1.Type() != Token::Integer) || (token2.Type() != Token::Integer)) {
00185   delete attrp;
00186   std::stringstream errstr;
00187   errstr << "Invalid range of consecutive indices"  << rTr.FileLine();
00188   throw Exception("IndexSet::DoRead", errstr.str(), 50);
00189       }
00190       FD_DC("IndexSet(" << this << ")::DoRead(..): inserting range " 
00191       << token1.IntegerValue() << " to " << token2.IntegerValue() );
00192       for(Idx index = (Idx) token1.IntegerValue(); index <= (Idx) token2.IntegerValue(); ++index) 
00193   Insert(index);
00194       rTr.ReadEnd("Consecutive");
00195       continue;
00196     }
00197     // cannot process token
00198     delete attrp;
00199     std::stringstream errstr;
00200     errstr << "Invalid token type " << token.Type() << " at " << rTr.FileLine();
00201     throw Exception("IndexSet::DoRead", errstr.str(), 50);
00202   }
00203   rTr.ReadEnd(label);
00204   // dispose attribute
00205   delete attrp;
00206 }
00207 
00208 
00209 // MaxIndex()
00210 Idx IndexSet::MaxIndex(void) const {
00211   if(Size()==0) return 0;
00212   Iterator it= End();
00213   it--;
00214   return *it;
00215 }
00216 
00217 // Insert()
00218 Idx IndexSet::Insert(void) {
00219   FD_DC("IndexSet(" << this << ")::Insert()");
00220   Idx index=MaxIndex()+1;
00221   TBaseSet<Idx>::Insert(index);
00222   return index;
00223 }
00224 
00225 //Insert(idx)
00226 bool IndexSet::Insert(const Idx& rIndex) {
00227   return TBaseSet<Idx>::Insert(rIndex);
00228 }
00229 
00230 
00231 //Valid(idx)
00232 bool  IndexSet::Valid(const Idx& rIndex) const {
00233   return rIndex!=0;
00234 }
00235 
00236 
00237 // Signature()
00238 Idx IndexSet::Signature(void) const {
00239   // prepare result
00240   Idx sig = 0;
00241   // helpers:
00242   Idx i = 1;
00243   Iterator it;
00244   // algorithm:
00245   for (it = Begin(); it != End(); ++it) {
00246     sig += *it * i;
00247     ++i;
00248   }
00249   return sig;
00250 }
00251 
00252 
00253 
00254 
00255 } // end name space

libFAUDES 2.20d --- 2011.04.26 --- c++ source docu by doxygen