libFAUDES

Sections

Index

indexset.cpp

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

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