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 // std faudes type 
00029 FAUDES_TYPE_IMPLEMENTATION(IndexSet,TBaseSet<Idx>)
00030 
00031 // IndexSet()
00032 IndexSet::IndexSet(void) : 
00033   TBaseSet<Idx>() 
00034 {
00035   FD_DC("IndexSet(" << this << ")::IndexSet()");
00036   Name("IndexSet");
00037 }
00038 
00039 // IndexSet(rOtherSet)
00040 IndexSet::IndexSet(const IndexSet& rOtherSet) : 
00041   TBaseSet<Idx>(rOtherSet) 
00042 {
00043   FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
00044   // copy my members (none)
00045 }
00046 
00047 // IndexSet(rOtherSet)
00048 IndexSet::IndexSet(const TBaseSet<Idx>& rOtherSet) : 
00049   TBaseSet<Idx>(rOtherSet) 
00050 {
00051   FD_DC("IndexSet(" << this << ")::IndexSet(rOtherSet " << &rOtherSet << ")");
00052   // copy my members (none)
00053 }
00054 
00055 // File constructor
00056 IndexSet::IndexSet(const std::string& rFilename, const std::string& rLabel) :
00057   TBaseSet<Idx>() 
00058 {
00059   FD_DC("IndexSet(" << this << ")::IndexSet(" << rFilename << ")");
00060   Read(rFilename, rLabel);
00061 }
00062 
00063 // assignment (attributes to default)
00064 IndexSet& IndexSet::DoAssign(const IndexSet& rSourceSet) {
00065   FD_DC("NameSet(" << this << ")::DoAssign(..)");
00066   // call base 
00067   TBaseSet<Idx>::DoAssign(rSourceSet);
00068   // done
00069   return *this;
00070 } 
00071 
00072 // DoWrite(rTw&)
00073 void IndexSet::DoWrite(TokenWriter& rTw, const std::string& rLabel,const Type* pContext) const {
00074   // figure section
00075   std::string label=rLabel;
00076   if(label=="") label=Name(); 
00077   if(label=="") label="IndexSet"; 
00078   FD_DC("IndexSet(" << this << ")::DoWrite(..): section " << label);
00079   /*
00080   // figure context: default to non
00081   const SymbolTable* symboltable=0;
00082   const std::map<Idx,Idx>* indexmap=0;  
00083   // figure context: can use vgenerator
00084   if(const vGenerator* vgen=dynamic_cast<const vGenerator*>(pContext)) {
00085     FD_DC("TaIndexSet(" << this << ")::DoWrite(..): generator context");
00086     symboltable=&vgen->StateSymbolTable();
00087     indexmap=&vgen->MinStateIndexMap();
00088   }
00089   */
00090   rTw.WriteBegin(label);
00091   Iterator it, conit;
00092   // iterate states to write
00093   for (it = Begin(); it != End(); ++it) {
00094     // identify consecutive block
00095     Idx start = *it;
00096     Idx anoncount = 0;
00097     for(conit=it; conit != End(); ++conit) {
00098       if(!Attribute(*conit).IsDefault()) break;
00099       if(*conit != start+anoncount) break;
00100       ++anoncount;
00101     }
00102     // write consecutive block
00103     if (anoncount > FD_CONSECUTIVE) {
00104       rTw.WriteBegin("Consecutive");
00105       rTw << start;
00106       rTw << start+anoncount-1;
00107       rTw.WriteEnd("Consecutive");
00108       it=conit;
00109     } 
00110     // break loop
00111     if(it == End() )
00112       break;
00113     // write individual state
00114     rTw << *it;
00115     // write state attribute
00116     const AttributeVoid& attr=Attribute(*it);
00117     attr.Write(rTw,"",pContext);
00118   }
00119   rTw.WriteEnd(label);
00120 }
00121 
00122 // DoRead(rTr, rLabel)
00123 void IndexSet::DoRead(TokenReader& rTr, const std::string& rLabel,const Type* pContext) {
00124   Token token;
00125   // figure section
00126   std::string label=rLabel;
00127   if(label=="") {
00128     rTr.Peek(token);
00129     if(token.Type()==Token::Begin) label=token.StringValue();
00130   }
00131   if(label=="") label="IndexSet"; 
00132   Name(label);
00133   FD_DC("IndexSet(" << this << ")::DoRead(..): section " << label);
00134   // prepare attribute
00135   AttributeVoid* attrp = Attribute().New();
00136   FD_DC("IndexSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
00137   // loop tokens
00138   rTr.SeekBegin(label); 
00139   while(rTr.Peek(token)) {
00140     // break on end
00141     if (token.Type() == Token::End) {
00142       break;
00143     }
00144     // read individual index with attribute
00145     if (token.Type() == Token::Integer) {
00146       rTr.Get(token);
00147       FD_DC("IndexSet(" << this << ")::DoRead(..): inserting index \"" 
00148       << token.IntegerValue() << "\"");
00149       Idx index=token.IntegerValue();
00150       // read attribute
00151       attrp->Read(rTr,"",pContext);
00152       // skip unknown attributes
00153       AttributeVoid::Skip(rTr);
00154       // insert element with attribute
00155       Insert(index); 
00156       Attribute(index,*attrp);
00157       continue;
00158     }
00159     // read consecutive block of anonymous states
00160     if (token.Type() == Token::Begin && token.StringValue() == "Consecutive") {
00161       rTr.ReadBegin("Consecutive");
00162       Token token1,token2;
00163       rTr.Get(token1);
00164       rTr.Get(token2);
00165       if((token1.Type() != Token::Integer) || (token2.Type() != Token::Integer)) {
00166   delete attrp;
00167   std::stringstream errstr;
00168   errstr << "Invalid range of consecutive indices"  << rTr.FileLine();
00169   throw Exception("IndexSet::DoRead", errstr.str(), 50);
00170       }
00171       FD_DC("IndexSet(" << this << ")::DoRead(..): inserting range " 
00172       << token1.IntegerValue() << " to " << token2.IntegerValue() );
00173       for(Idx index = (Idx) token1.IntegerValue(); index <= (Idx) token2.IntegerValue(); ++index) 
00174   Insert(index);
00175       rTr.ReadEnd("Consecutive");
00176       continue;
00177     }
00178     // cannot process token
00179     delete attrp;
00180     std::stringstream errstr;
00181     errstr << "Invalid token type " << token.Type() << " at " << rTr.FileLine();
00182     throw Exception("IndexSet::DoRead", errstr.str(), 50);
00183   }
00184   rTr.SeekEnd(label);
00185   // dispose attribute
00186   delete attrp;
00187 }
00188 
00189 
00190 // MaxIndex()
00191 Idx IndexSet::MaxIndex(void) const {
00192   if(Size()==0) return 0;
00193   Iterator it= End();
00194   it--;
00195   return *it;
00196 }
00197 
00198 // Insert()
00199 Idx IndexSet::Insert(void) {
00200   FD_DC("IndexSet(" << this << ")::Insert()");
00201   Idx index=MaxIndex()+1;
00202   TBaseSet<Idx>::Insert(index);
00203   return index;
00204 }
00205 
00206 //Insert(idx)
00207 bool IndexSet::Insert(const Idx& rIndex) {
00208   return TBaseSet<Idx>::Insert(rIndex);
00209 }
00210 
00211 
00212 //Valid(idx)
00213 bool  IndexSet::Valid(const Idx& rIndex) const {
00214   return rIndex!=0;
00215 }
00216 
00217 
00218 // Signature()
00219 Idx IndexSet::Signature(void) const {
00220   // prepare result
00221   Idx sig = 0;
00222   // helpers:
00223   Idx i = 1;
00224   Iterator it;
00225   // algorithm:
00226   for (it = Begin(); it != End(); ++it) {
00227     sig += *it * i;
00228     ++i;
00229   }
00230   return sig;
00231 }
00232 
00233 
00234 
00235 
00236 } // end name space

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