indexset.h

Go to the documentation of this file.
00001 
00004 /* FAU Discrete Event Systems Library (libfaudes)
00005 
00006    Copyright (C) 2006  Bernd Opitz
00007    Copyright (C) 2007  Thomas Moor
00008    Exclusive copyright is granted to Klaus Schmidt
00009 
00010    This library is free software; you can redistribute it and/or
00011    modify it under the terms of the GNU Lesser General Public
00012    License as published by the Free Software Foundation; either
00013    version 2.1 of the License, or (at your option) any later version.
00014 
00015    This library is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018    Lesser General Public License for more details.
00019 
00020    You should have received a copy of the GNU Lesser General Public
00021    License along with this library; if not, write to the Free Software
00022    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00023 
00024 
00025 
00026 #ifndef FAUDES_INDEXSET_H
00027 #define FAUDES_INDEXSET_H
00028 
00029 #include "definitions.h"
00030 #include "attributes.h"
00031 #include "baseset.h"
00032 #include "symboltable.h"
00033 #include <set>
00034 #include <map>
00035 #include <algorithm>
00036 
00037 
00038 namespace faudes {
00039 
00040 
00041 
00045 // Forward declaration for the attributed version of indexset
00046 template<class Attr> class TaIndexSet;
00047 
00078 class IndexSet : public TBaseSet<Idx> {
00079 
00084   template<class Attr> friend class TaIndexSet;
00085 
00086 public:
00087 
00091   IndexSet(void);
00092 
00096   IndexSet(const TBaseSet<Idx>& rOtherSet);
00097 
00111   IndexSet(const std::string& rFilename, const std::string& rLabel = "IndexSet");
00112 
00116   virtual ~IndexSet(void) {};
00117 
00124   Idx MaxIndex(void) const;
00125 
00133    bool Valid(Idx index) const;
00134    
00138   typedef TBaseSet<Idx>::Iterator Iterator;
00139 
00146   Idx Insert(void);
00147 
00156   bool Insert(Idx idx);
00157 
00166   Idx Signature(void) const;
00167 
00177   std::string Str(Idx index) const {return ToStringInteger(index); };
00178 
00179  protected:
00180 
00196   virtual void DoWrite(TokenWriter& tw, const std::string& rLabel="", const Type* pContext=0) const;
00197 
00214   virtual void DoRead(TokenReader& rTr, const std::string& rLabel="", const Type* pContext=0);
00215 
00216 
00217 };
00218 
00219 /* convenience typedef for stateset */
00220 typedef IndexSet StateSet;
00221 
00222 
00223 
00258 template<class Attr>
00259 class TaIndexSet : public virtual IndexSet {
00260 
00265   friend class IndexSet;
00266 
00267  public:
00271   TaIndexSet(void);
00272 
00276   TaIndexSet(const TaIndexSet& rOtherSet);
00277 
00281   TaIndexSet(const IndexSet& rOtherSet);
00282 
00296   TaIndexSet(const std::string& rFilename, const std::string& rLabel = "IndexSet");
00297 
00301   virtual ~TaIndexSet(void) {};
00302 
00310    template<class OtherAttr>
00311    void CopyWithoutAttributes(TaIndexSet<OtherAttr>& rSet) const;
00312 
00319   Idx AttributesSize(void) const;
00320 
00331   Attr* Attributep(Idx idx);
00332 
00341   const Attr& Attribute(Idx idx) const;
00342 
00351   void Attribute(Idx idx, const Attr& attr);
00352 
00359   void ClrAttribute(Idx idx);
00360 
00365   void ClearAttributes(void);
00366 
00367 
00376   bool Erase(Idx idx);
00377 
00378 
00387   Iterator Erase(const Iterator& pos); 
00388 
00389 
00398    void EraseSet(const IndexSet& rOtherSet);
00399 
00400 
00407   Idx Insert(void);
00408 
00417   Idx Insert(const Attr& attr);
00418 
00428   bool Insert(Idx idx);
00429 
00440   bool Insert(Idx idx, const Attr& attr);
00441 
00450    void InsertSet(const IndexSet& rOtherSet);
00451 
00459    void InsertSet(const TaIndexSet& rOtherSet);
00460 
00464   virtual void Clear(void);
00465       
00473   void SetUnion(const TaIndexSet& rOtherSet);
00474 
00482   void SetUnion(const IndexSet& rOtherSet);
00483 
00491   void SetIntersection(const TaIndexSet& rOtherSet);
00492 
00500   void SetIntersection(const IndexSet& rOtherSet);
00501 
00502 
00503  protected:
00505   std::map<Idx,Attr> mAttributeMap;
00506 
00508   static Attr mDefAttribute; 
00509 
00538   virtual void DoWrite(TokenWriter& rTw,  const std::string& rLabel="", const Type* pContext=0) const;
00539 
00557   virtual void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0);
00558 
00559 
00560 };
00561 
00563 template<class Attr> Attr TaIndexSet<Attr>::mDefAttribute;
00564 
00566 //template<class Attr> typedef TaStateSet<class Attr> TaIndexSet<class Attr>
00567 #define TaStateSet TaIndexSet
00568 
00569 
00573 /*
00574 *************************************************************************************
00575 *************************************************************************************
00576  Implementation
00577 *************************************************************************************
00578 *************************************************************************************
00579 */
00580 
00581 // more includes
00582 //#include "vgenerator.h"
00583 
00584 // TaIndexSet()
00585 template<class Attr>
00586 TaIndexSet<Attr>::TaIndexSet(void) {
00587   FD_DC("TaIndexSet(" << this << ")::TaIndexSet()");
00588   IndexSet::Name("IndexSet");
00589 }
00590 
00591 // TaIndexSet(rOtherSet)
00592 template<class Attr>
00593 TaIndexSet<Attr>::TaIndexSet(const TaIndexSet& rOtherSet) : 
00594   IndexSet(rOtherSet) 
00595 {
00596   FD_DC("TaIndexSet(" << this << ")::TaIndexSet(rOtherSet " << &rOtherSet << ")");
00597   mAttributeMap=rOtherSet.mAttributeMap;
00598 }
00599 
00600 // TaIndexSet(rOtherSet)
00601 template<class Attr>
00602 TaIndexSet<Attr>::TaIndexSet(const IndexSet& rOtherSet) :
00603   IndexSet(rOtherSet) 
00604 {
00605   FD_DC("TaIndexSet(" << this << ")::TaIndexSet(rOtherSet " << &rOtherSet << ")");
00606 }
00607 
00608 
00609 // File constructor
00610 template<class Attr>
00611 TaIndexSet<Attr>::TaIndexSet(const std::string& rFilename, const std::string& rLabel) {
00612   FD_DC("TaIndexSet(" << this << ")::TaIndexSet(" << rFilename << ")");
00613   Read(rFilename, rLabel);
00614 }
00615 
00616 
00617 // CopyWithoutAttributes(rSet)
00618 template<class Attr>
00619 template<class OtherAttr>
00620 void TaIndexSet<Attr>::CopyWithoutAttributes(TaIndexSet<OtherAttr>& rSet) const {
00621   rSet.Clear();
00622   rSet.Assign(*this);
00623 }  
00624 
00625 
00626 // DoWrite(rTw&)
00627 template<class Attr>
00628 void TaIndexSet<Attr>::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
00629   // figure section
00630   std::string label=rLabel;
00631   if(label=="") label=Name(); 
00632   if(label=="") label="IndexSet"; 
00633   FD_DC("TaIndexSet(" << this << ")::DoWrite(..): section " << label);
00634   /*
00635   // figure context: default to non
00636   const SymbolTable* symboltable=0;
00637   const std::map<Idx,Idx>* indexmap=0;  
00638   // figure context: can use vgenerator
00639   if(const vGenerator* vgen=dynamic_cast<const vGenerator*>(pContext)) {
00640     FD_DC("TaIndexSet(" << this << ")::DoWrite(..): generator context");
00641     symboltable=&vgen->StateSymbolTable();
00642     indexmap=&vgen->MinStateIndexMap();
00643   }
00644   */
00645   rTw.WriteBegin(label);
00646   Iterator it, conit;
00647   // iterate states to write
00648   for (it = Begin(); it != End(); ++it) {
00649     // identify consecutive block
00650     Idx start = *it;
00651     Idx anoncount = 0;
00652     for(conit=it; conit != End(); ++conit) {
00653       if(!Attribute(*conit).IsDefault()) break;
00654       if(*conit != start+anoncount) break;
00655       ++anoncount;
00656     }
00657     // write consecutive block
00658     if (anoncount > FD_CONSECUTIVE) {
00659       rTw.WriteBegin("Consecutive");
00660       rTw << start;
00661       rTw << start+anoncount-1;
00662       rTw.WriteEnd("Consecutive");
00663       it=conit;
00664     } 
00665     // break loop
00666     if(it == End() )
00667       break;
00668     // write individual state
00669     rTw << *it;
00670     // write state attribute
00671     Attr attr=Attribute(*it);
00672     attr.Write(rTw,"",pContext);
00673   }
00674   rTw.WriteEnd(label);
00675 }
00676 
00677 
00678 // DoRead(rTr, rLabel, pContext)
00679 template<class Attr>
00680 void TaIndexSet<Attr>::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
00681   std::string label=rLabel;
00682   if(label=="") label="IndexSet"; 
00683   FD_DC("TaIndexSet(" << this << ")::DoRead(..): section " << label);
00684   Name(label);
00685   rTr.SeekBegin(label); 
00686   Token token;
00687   Attr attribute;
00688   while(rTr.Peek(token)) {
00689     // break on end
00690     if (token.Type() == Token::End) {
00691       break;
00692     }
00693     // read individual index with attribute
00694     if (token.Type() == Token::Integer) {
00695       rTr.Get(token);
00696       FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting index \"" 
00697       << token.IntegerValue() << "\"");
00698       Idx index=token.IntegerValue();
00699       // read attribute
00700       Attr attr;
00701       attr.Read(rTr,"",pContext);
00702       // insert element
00703       Insert(index,attr);
00704       continue;
00705     }
00706     // read consecutive block of anonymous states
00707     if (token.Type() == Token::Begin && token.StringValue() == "Consecutive") {
00708       rTr.ReadBegin("Consecutive");
00709       Token token1,token2;
00710       rTr.Get(token1);
00711       rTr.Get(token2);
00712       if ((token1.Type() != Token::Integer) || (token2.Type() != Token::Integer)) {
00713   std::stringstream errstr;
00714   errstr << "Invalid range of consecutive indices"  << rTr.FileLine();
00715   throw Exception("TaIndexSet::DoRead", errstr.str(), 50);
00716       }
00717       FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting range " 
00718       << token1.IntegerValue() << " to " << token2.IntegerValue() );
00719       for(Idx index = (Idx) token1.IntegerValue(); index <= (Idx) token2.IntegerValue(); ++index) 
00720   Insert(index);
00721       rTr.ReadEnd("Consecutive");
00722       continue;
00723     }
00724     // cannot process token
00725     std::stringstream errstr;
00726     errstr << "Invalid token type " << token.Type() << " at " << rTr.FileLine();
00727     throw Exception("TaIndexSet::DoRead", errstr.str(), 50);
00728   }
00729   rTr.SeekEnd(label);
00730 }
00731 
00732 
00733 // AttributesSize()
00734 template<class Attr>
00735 Idx TaIndexSet<Attr>::AttributesSize(void) const {
00736   return (Idx)mAttributeMap.size();
00737 }
00738 
00739 // Attributep(idx)
00740 template<class Attr>
00741 Attr* TaIndexSet<Attr>::Attributep(Idx idx) {
00742 #ifdef FAUDES_CHECKED
00743   if(!Exists(idx)) {
00744     std::stringstream errstr;
00745     errstr << "index " << idx << " not member of set" << std::endl;
00746     throw Exception("TaIndexSet::Attributep(idx)", errstr.str(), 60);
00747   }
00748 #endif
00749   typename std::map<Idx,Attr>::iterator ait;
00750   ait=mAttributeMap.find(idx);
00751   if( ait!=mAttributeMap.end() ) 
00752     return & (ait->second);
00753   // insert explicit default attribute
00754   typename std::map<Idx,Attr>::value_type entry(idx,mDefAttribute);
00755   ait=mAttributeMap.insert(entry).first;
00756   return & (ait->second);
00757 }
00758 
00759 // Attribute(idx)
00760 template<class Attr>
00761 const Attr& TaIndexSet<Attr>::Attribute(Idx idx) const{
00762 #ifdef FAUDES_CHECKED
00763   if (!Exists(idx)) {
00764     std::stringstream errstr;
00765     errstr << "index " << idx << " not member of set" << std::endl;
00766     throw Exception("TaIndexSet::Attribute(idx)", errstr.str(), 60);
00767   }
00768 #endif
00769   typename std::map<Idx,Attr>::const_iterator ait;
00770   ait=mAttributeMap.find(idx);
00771   if( ait!=mAttributeMap.end() ) {
00772     return ait->second;
00773   } 
00774   return mDefAttribute;
00775 }
00776 
00777 // Attribute(idx,attr)
00778 template<class Attr>
00779 void TaIndexSet<Attr>::Attribute(Idx idx, const Attr& attr) {
00780 #ifdef FAUDES_CHECKED
00781   if (!Exists(idx)) {
00782     std::stringstream errstr;
00783     errstr << "index " << idx << " not member of set" << std::endl;
00784     throw Exception("TaIndexSet::Attribute(idx,attr)", errstr.str(), 60);
00785   }
00786 #endif
00787   if(attr.IsDefault()) {
00788     mAttributeMap.erase(idx);
00789   } else {
00790     mAttributeMap[idx]=attr;
00791   }
00792 }
00793 
00794 //ClearAttributes()
00795 template<class Attr>
00796 void TaIndexSet<Attr>::ClearAttributes(void) {
00797   mAttributeMap.clear();
00798 }
00799 
00800 //ClrAttribute(idx)
00801 template<class Attr>
00802 void TaIndexSet<Attr>::ClrAttribute(Idx idx) {
00803   mAttributeMap.erase(idx);
00804 }
00805 
00806 //Clear
00807 template<class Attr>
00808 void TaIndexSet<Attr>::Clear(void) {
00809   FD_DC("TaIndexSet(" << this << ")::Clear()");
00810   IndexSet::Clear();
00811   mAttributeMap.clear();
00812 }  
00813 
00814 // Insert()
00815 template<class Attr>
00816 Idx TaIndexSet<Attr>::Insert(void) {
00817   FD_DC("TaIndexSet(" << this << ")::Insert()");
00818   Idx index=MaxIndex()+1;
00819   Insert(index);
00820   return index;
00821 }
00822 
00823 //Insert(idx)
00824 template<class Attr>
00825 bool TaIndexSet<Attr>::Insert(Idx idx) {
00826   FD_DC("TaIndexSet(" << this << ")::Insert("<< idx <<")");
00827   bool ret=TBaseSet<Idx>::Insert(idx);
00828   //mAttributeMap.erase(idx);
00829   return ret;
00830 }
00831 
00832 // Insert(attr)
00833 template<class Attr>
00834 Idx TaIndexSet<Attr>::Insert(const Attr& attr) {
00835   FD_DC("TaIndexSet(" << this << ")::Insert(" << attr.ToString() << ")");
00836   Idx index = Insert();
00837   if(!attr.IsDefault()) {
00838     mAttributeMap[index]=attr;
00839   }
00840   return index;
00841 }
00842 
00843 //Insert(idx,attr)
00844 template<class Attr>
00845 bool TaIndexSet<Attr>::Insert(Idx idx, const Attr& attr) {
00846   FD_DC("TaIndexSet(" << this << ")::Insert("<< idx <<",attr)");
00847   bool ret=TBaseSet<Idx>::Insert(idx);
00848   if(attr.IsDefault())
00849     mAttributeMap.erase(idx);
00850   else
00851     mAttributeMap[idx]=attr;
00852   return ret;
00853 }
00854 
00855 
00856 // InsertSet(set)
00857 template<class Attr>
00858 void TaIndexSet<Attr>::InsertSet(const TaIndexSet& rOtherSet) {
00859   Detach();
00860   iterator it1 = pSet->begin();
00861   iterator it2 = rOtherSet.pSet->begin();
00862   while ((it1 != pSet->end()) && (it2 != rOtherSet.pSet->end())) {
00863     if (*it1 < *it2) {
00864       ++it1;
00865     }
00866     else if (*it1 == *it2) {
00867       ++it1;
00868       ++it2;
00869     }
00870     else { // (*it1 > *it2)
00871       Insert(*it2,rOtherSet.Attribute(*it2)); // todo: optimize 
00872       ++it2;
00873     }
00874   }
00875   while (it2 != rOtherSet.pSet->end()) {
00876     Insert(*it2,rOtherSet.Attribute(*it2)); // todo: optimize 
00877     ++it2;
00878   }
00879 }
00880 
00881 // InsertSet(set)
00882 template<class Attr>
00883 void TaIndexSet<Attr>::InsertSet(const IndexSet& rOtherSet) {
00884   IndexSet::InsertSet(rOtherSet);
00885 }
00886 
00887 //Erase(idx)
00888 template<class Attr>
00889 bool TaIndexSet<Attr>::Erase(Idx idx) {
00890   bool ret=TBaseSet<Idx>::Erase(idx);
00891   mAttributeMap.erase(idx);
00892   return ret;
00893 }
00894 
00895 
00896 //Erase(pos)
00897 template<class Attr>
00898 typename TaIndexSet<Attr>::Iterator TaIndexSet<Attr>::Erase(const Iterator& pos) { 
00899 #ifdef FAUDES_CHECKED
00900   if (pos == End()) {
00901     std::stringstream errstr;
00902     errstr << "iterator out of range " << std::endl;
00903     throw Exception("TaIndexSet::Erase", errstr.str(), 60);
00904   }
00905 #endif
00906   mAttributeMap.erase(*pos);
00907   return TBaseSet<Idx>::Erase(pos); 
00908 }
00909 
00910 
00911 //EraseSet(set)
00912 template<class Attr>
00913 void TaIndexSet<Attr>::EraseSet(const IndexSet& rOtherSet) {
00914   FD_DC("TaIndexSet(" << this << ")::EraseSet(" << rOtherSet.ToString() << ")");
00915   Detach();
00916   // TODO: test and optimize 
00917   iterator tmpit;
00918   iterator it = pSet->begin();
00919   iterator oit = rOtherSet.pSet->begin();
00920   while ((it != pSet->end()) && (oit != rOtherSet.pSet->end())) {
00921     if (*it < *oit) {
00922       it=pSet->lower_bound(*oit); // alt: ++it;
00923     }
00924     else if (*it == *oit) { 
00925       mAttributeMap.erase(*it);
00926       tmpit=it;
00927       ++it;
00928       ++oit;
00929       pSet->erase(tmpit);
00930     }
00931     else { // (*it > *oit)
00932       oit=rOtherSet.pSet->lower_bound(*it); // ++it2;
00933     }
00934   }
00935 }
00936 
00937 
00938 // SetUnion(set)
00939 template<class Attr>
00940 void TaIndexSet<Attr>::SetUnion(const IndexSet& rOtherSet) {
00941   IndexSet::SetUnion(rOtherSet);
00942   mAttributeMap.clear();
00943 }
00944 
00945 // SetUnion(set)
00946 template<class Attr>
00947 void TaIndexSet<Attr>::SetUnion(const TaIndexSet& rOtherSet) {
00948   IndexSet::SetUnion(rOtherSet);
00949   mAttributeMap.clear();
00950 }
00951 
00952 // SetIntersection(set)
00953 template<class Attr>
00954 void TaIndexSet<Attr>::SetIntersection(const IndexSet& rOtherSet) {
00955   IndexSet::SetIntersection(rOtherSet);
00956   mAttributeMap.clear();
00957 }
00958 
00959 // SetIntersection(set)
00960 template<class Attr>
00961 void TaIndexSet<Attr>::SetIntersection(const TaIndexSet& rOtherSet) {
00962   IndexSet::SetIntersection(rOtherSet);
00963   mAttributeMap.clear();
00964 }
00965 
00966 
00967 
00968 } // namespace faudes
00969 
00970 #endif 

Generated on Mon Nov 10 08:13:14 2008 for libFAUDES 2.11v by  doxygen 1.4.4