00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef INDEXSET_H
00027
00028 #include "definitions.h"
00029 #include "attributes.h"
00030 #include "baseset.h"
00031 #include <set>
00032 #include <map>
00033 #include <algorithm>
00034
00035 namespace faudes {
00036
00040
00041 template<class Attr> class TaIndexSet;
00042
00072 class IndexSet : public TBaseSet<Idx> {
00073
00078 template<class Attr> friend class TaIndexSet;
00079
00080 public:
00081
00085 IndexSet(void);
00086
00090 IndexSet(const TBaseSet<Idx>& rOtherSet);
00091
00105 IndexSet(const std::string& rFilename, const std::string& rLabel = "IndexSet");
00106
00110 virtual ~IndexSet(void) {};
00111
00118 Idx MaxIndex(void) const;
00119
00127 bool Valid(Idx index) const;
00128
00132 typedef TBaseSet<Idx>::Iterator Iterator;
00133
00140 Idx Insert(void);
00141
00150 bool Insert(Idx idx);
00151
00160 Idx Signature(void) const;
00161
00171 std::string Str(Idx index) const {return ToStringInteger(index); };
00172
00173 protected:
00174
00188 void DoWrite(TokenWriter& tw, const std::string& rLabel="") const;
00189
00204 void DoRead(TokenReader& rTr, const std::string& rLabel = "IndexSet");
00205
00206
00207 };
00208
00209
00210 typedef IndexSet StateSet;
00211
00212
00213
00248 template<class Attr>
00249 class TaIndexSet : public virtual IndexSet {
00250
00255 template<class A> friend class TaIndexSet;
00256
00257 public:
00261 TaIndexSet(void);
00262
00266 TaIndexSet(const TaIndexSet& rOtherSet);
00267
00271 TaIndexSet(const IndexSet& rOtherSet);
00272
00286 TaIndexSet(const std::string& rFilename, const std::string& rLabel = "IndexSet");
00287
00291 virtual ~TaIndexSet(void) {};
00292
00300 template<class OtherAttr>
00301 void CopyWithoutAttributes(TaIndexSet<OtherAttr>& rSet) const;
00302
00309 Idx AttributesSize(void) const;
00310
00321 Attr* Attributep(Idx idx);
00322
00331 const Attr& Attribute(Idx idx) const;
00332
00341 void Attribute(Idx idx, const Attr& attr);
00342
00349 void ClrAttribute(Idx idx);
00350
00355 void ClearAttributes(void);
00356
00357
00366 bool Erase(Idx idx);
00367
00368
00377 Iterator Erase(const Iterator& pos);
00378
00379
00388 void EraseSet(const IndexSet& rOtherSet);
00389
00390
00397 Idx Insert(void);
00398
00407 Idx Insert(const Attr& attr);
00408
00418 bool Insert(Idx idx);
00419
00430 bool Insert(Idx idx, const Attr& attr);
00431
00440 void InsertSet(const IndexSet& rOtherSet);
00441
00449 void InsertSet(const TaIndexSet& rOtherSet);
00450
00454 void Clear(void);
00455
00463 void SetUnion(const TaIndexSet& rOtherSet);
00464
00472 void SetUnion(const IndexSet& rOtherSet);
00473
00481 void SetIntersection(const TaIndexSet& rOtherSet);
00482
00490 void SetIntersection(const IndexSet& rOtherSet);
00491
00492
00493 protected:
00495 std::map<Idx,Attr> mAttributeMap;
00496
00498 static Attr mDefAttribute;
00499
00526 void DoWrite(TokenWriter& rTw, const std::string& rLabel="") const;
00527
00543 void DoRead(TokenReader& rTr, const std::string& rLabel = "IndexSet");
00544
00545
00546 };
00547
00549 template<class Attr> Attr TaIndexSet<Attr>::mDefAttribute;
00550
00552 #define TaStateSet TaIndexSet
00553
00554
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567 template<class Attr>
00568 TaIndexSet<Attr>::TaIndexSet(void) {
00569 FD_DC("TaIndexSet(" << this << ")::TaIndexSet()");
00570 IndexSet::Name("IndexSet");
00571 }
00572
00573
00574 template<class Attr>
00575 TaIndexSet<Attr>::TaIndexSet(const TaIndexSet& rOtherSet) :
00576 IndexSet(rOtherSet)
00577 {
00578 FD_DC("TaIndexSet(" << this << ")::TaIndexSet(rOtherSet " << &rOtherSet << ")");
00579 mAttributeMap=rOtherSet.mAttributeMap;
00580 }
00581
00582
00583 template<class Attr>
00584 TaIndexSet<Attr>::TaIndexSet(const IndexSet& rOtherSet) :
00585 IndexSet(rOtherSet)
00586 {
00587 FD_DC("TaIndexSet(" << this << ")::TaIndexSet(rOtherSet " << &rOtherSet << ")");
00588 }
00589
00590
00591
00592 template<class Attr>
00593 TaIndexSet<Attr>::TaIndexSet(const std::string& rFilename, const std::string& rLabel) {
00594 FD_DC("TaIndexSet(" << this << ")::TaIndexSet(" << rFilename << ")");
00595 Read(rFilename, rLabel);
00596 }
00597
00598
00599
00600 template<class Attr>
00601 template<class OtherAttr>
00602 void TaIndexSet<Attr>::CopyWithoutAttributes(TaIndexSet<OtherAttr>& rSet) const {
00603 rSet.Clear();
00604 rSet.Assign(*this);
00605 }
00606
00607
00608
00609 template<class Attr>
00610 void TaIndexSet<Attr>::DoWrite(TokenWriter& rTw, const std::string& rLabel) const {
00611 std::string label=rLabel;
00612 if(label=="") label=Name();
00613 rTw.WriteBegin(label);
00614 Iterator it, conit;
00615
00616 for (it = Begin(); it != End(); ++it) {
00617
00618 Idx start = *it;
00619 Idx anoncount = 0;
00620 for(conit=it; conit != End(); ++conit) {
00621 if(!Attribute(*conit).IsDefault()) break;
00622 if(*conit != start+anoncount) break;
00623 ++anoncount;
00624 }
00625
00626 if (anoncount > FD_CONSECUTIVE) {
00627 rTw.WriteBegin("Consecutive");
00628 rTw << start;
00629 rTw << start+anoncount-1;
00630 rTw.WriteEnd("Consecutive");
00631 it=conit;
00632 }
00633
00634 if(it == End() )
00635 break;
00636
00637 rTw << *it;
00638
00639 Attr attr=Attribute(*it);
00640 attr.Write(rTw);
00641 }
00642 rTw.WriteEnd(label);
00643 }
00644
00645
00646
00647 template<class Attr>
00648 void TaIndexSet<Attr>::DoRead(TokenReader& rTr, const std::string& rLabel) {
00649 Name(rLabel);
00650 Clear();
00651 rTr.SeekBegin(rLabel);
00652 Token token;
00653 Attr attribute;
00654 while(rTr.Peek(token)) {
00655
00656 if (token.Type() == Token::End) {
00657 break;
00658 }
00659
00660 if (token.Type() == Token::Integer) {
00661 rTr.Get(token);
00662 FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting index \""
00663 << token.IntegerValue() << "\"");
00664 Idx index=token.IntegerValue();
00665
00666 Attr attr;
00667 attr.Read(rTr);
00668
00669 AttributeVoid vattr;
00670 vattr.Read(rTr);
00671
00672 Insert(index,attr);
00673 continue;
00674 }
00675
00676 if (token.Type() == Token::Begin && token.StringValue() == "Consecutive") {
00677 rTr.ReadBegin("Consecutive");
00678 Token token1,token2;
00679 rTr.Get(token1);
00680 rTr.Get(token2);
00681 if ((token1.Type() != Token::Integer) || (token2.Type() != Token::Integer)) {
00682 std::stringstream errstr;
00683 errstr << "Invalid range of consecutive indices" << rTr.FileLine();
00684 throw Exception("TaIndexSet::DoRead", errstr.str(), 50);
00685 }
00686 FD_DC("TaIndexSet(" << this << ")::DoRead(..): inserting range "
00687 << token1.IntegerValue() << " to " << token2.IntegerValue() );
00688 for(Idx index = (Idx) token1.IntegerValue(); index <= (Idx) token2.IntegerValue(); ++index)
00689 Insert(index);
00690 rTr.ReadEnd("Consecutive");
00691 continue;
00692 }
00693
00694 std::stringstream errstr;
00695 errstr << "Invalid token" << rTr.FileLine();
00696 throw Exception("TaIndexSetet::DoRead", errstr.str(), 50);
00697 }
00698 rTr.SeekEnd(rLabel);
00699 }
00700
00701
00702
00703 template<class Attr>
00704 Idx TaIndexSet<Attr>::AttributesSize(void) const {
00705 return (Idx)mAttributeMap.size();
00706 }
00707
00708
00709 template<class Attr>
00710 Attr* TaIndexSet<Attr>::Attributep(Idx idx) {
00711 #ifdef FAUDES_CHECKED
00712 if (!Exists(idx)) {
00713 std::stringstream errstr;
00714 errstr << "index " << idx << " not member of set" << std::endl;
00715 throw Exception("TaIndexSet::Attributep(idx)", errstr.str(), 60);
00716 }
00717 #endif
00718 typename std::map<Idx,Attr>::iterator ait;
00719 ait=mAttributeMap.find(idx);
00720 if( ait!=mAttributeMap.end() )
00721 return & (ait->second);
00722
00723 typename std::map<Idx,Attr>::value_type entry(idx,mDefAttribute);
00724 ait=mAttributeMap.insert(entry).first;
00725 return & (ait->second);
00726 }
00727
00728
00729 template<class Attr>
00730 const Attr& TaIndexSet<Attr>::Attribute(Idx idx) const{
00731 #ifdef FAUDES_CHECKED
00732 if (!Exists(idx)) {
00733 std::stringstream errstr;
00734 errstr << "index " << idx << " not member of set" << std::endl;
00735 throw Exception("TaIndexSet::Attribute(idx)", errstr.str(), 60);
00736 }
00737 #endif
00738 typename std::map<Idx,Attr>::const_iterator ait;
00739 ait=mAttributeMap.find(idx);
00740 if( ait!=mAttributeMap.end() ) {
00741 return ait->second;
00742 }
00743 return mDefAttribute;
00744 }
00745
00746
00747 template<class Attr>
00748 void TaIndexSet<Attr>::Attribute(Idx idx, const Attr& attr) {
00749 #ifdef FAUDES_CHECKED
00750 if (!Exists(idx)) {
00751 std::stringstream errstr;
00752 errstr << "index " << idx << " not member of set" << std::endl;
00753 throw Exception("TaIndexSet::Attribute(idx,attr)", errstr.str(), 60);
00754 }
00755 #endif
00756 if(attr.IsDefault()) {
00757 mAttributeMap.erase(idx);
00758 } else {
00759 mAttributeMap[idx]=attr;
00760 }
00761 }
00762
00763
00764 template<class Attr>
00765 void TaIndexSet<Attr>::ClearAttributes(void) {
00766 mAttributeMap.clear();
00767 }
00768
00769
00770 template<class Attr>
00771 void TaIndexSet<Attr>::ClrAttribute(Idx idx) {
00772 mAttributeMap.erase(idx);
00773 }
00774
00775
00776 template<class Attr>
00777 void TaIndexSet<Attr>::Clear(void) {
00778 IndexSet::Clear();
00779 mAttributeMap.clear();
00780 }
00781
00782
00783 template<class Attr>
00784 Idx TaIndexSet<Attr>::Insert(void) {
00785 FD_DC("TaIndexSet(" << this << ")::Insert()");
00786 Idx index=MaxIndex()+1;
00787 Insert(index);
00788 return index;
00789 }
00790
00791
00792 template<class Attr>
00793 bool TaIndexSet<Attr>::Insert(Idx idx) {
00794 FD_DC("TaIndexSet(" << this << ")::Insert("<< idx <<")");
00795 bool ret=TBaseSet<Idx>::Insert(idx);
00796
00797 return ret;
00798 }
00799
00800
00801 template<class Attr>
00802 Idx TaIndexSet<Attr>::Insert(const Attr& attr) {
00803 FD_DC("TaIndexSet(" << this << ")::Insert(" << attr.ToString() << ")");
00804 Idx index = Insert();
00805 if(!attr.IsDefault()) {
00806 mAttributeMap[index]=attr;
00807 }
00808 return index;
00809 }
00810
00811
00812 template<class Attr>
00813 bool TaIndexSet<Attr>::Insert(Idx idx, const Attr& attr) {
00814 FD_DC("TaIndexSet(" << this << ")::Insert("<< idx <<",attr)");
00815 bool ret=TBaseSet<Idx>::Insert(idx);
00816 if(attr.IsDefault())
00817 mAttributeMap.erase(idx);
00818 else
00819 mAttributeMap[idx]=attr;
00820 return ret;
00821 }
00822
00823
00824
00825 template<class Attr>
00826 void TaIndexSet<Attr>::InsertSet(const TaIndexSet& rOtherSet) {
00827 Detach();
00828 iterator it1 = pSet->begin();
00829 iterator it2 = rOtherSet.pSet->begin();
00830 while ((it1 != pSet->end()) && (it2 != rOtherSet.pSet->end())) {
00831 if (*it1 < *it2) {
00832 ++it1;
00833 }
00834 else if (*it1 == *it2) {
00835 ++it1;
00836 ++it2;
00837 }
00838 else {
00839 Insert(*it2,rOtherSet.Attribute(*it2));
00840 ++it2;
00841 }
00842 }
00843 while (it2 != rOtherSet.pSet->end()) {
00844 Insert(*it2,rOtherSet.Attribute(*it2));
00845 ++it2;
00846 }
00847 }
00848
00849
00850 template<class Attr>
00851 void TaIndexSet<Attr>::InsertSet(const IndexSet& rOtherSet) {
00852 IndexSet::InsertSet(rOtherSet);
00853 }
00854
00855
00856 template<class Attr>
00857 bool TaIndexSet<Attr>::Erase(Idx idx) {
00858 bool ret=TBaseSet<Idx>::Erase(idx);
00859 mAttributeMap.erase(idx);
00860 return ret;
00861 }
00862
00863
00864
00865 template<class Attr>
00866 typename TaIndexSet<Attr>::Iterator TaIndexSet<Attr>::Erase(const Iterator& pos) {
00867 #ifdef FAUDES_CHECKED
00868 if (pos == End()) {
00869 std::stringstream errstr;
00870 errstr << "iterator out of range " << std::endl;
00871 throw Exception("TaIndexSet::Erase", errstr.str(), 60);
00872 }
00873 #endif
00874 mAttributeMap.erase(*pos);
00875 return TBaseSet<Idx>::Erase(pos);
00876 }
00877
00878
00879
00880 template<class Attr>
00881 void TaIndexSet<Attr>::EraseSet(const IndexSet& rOtherSet) {
00882 FD_DC("TaIndexSet(" << this << ")::EraseSet(" << rOtherSet.ToString() << ")");
00883 Detach();
00884
00885 iterator tmpit;
00886 iterator it = pSet->begin();
00887 iterator oit = rOtherSet.pSet->begin();
00888 while ((it != pSet->end()) && (oit != rOtherSet.pSet->end())) {
00889 if (*it < *oit) {
00890 it=pSet->lower_bound(*oit);
00891 }
00892 else if (*it == *oit) {
00893 mAttributeMap.erase(*it);
00894 tmpit=it;
00895 ++it;
00896 ++oit;
00897 pSet->erase(tmpit);
00898 }
00899 else {
00900 oit=rOtherSet.pSet->lower_bound(*it);
00901 }
00902 }
00903 }
00904
00905
00906
00907 template<class Attr>
00908 void TaIndexSet<Attr>::SetUnion(const IndexSet& rOtherSet) {
00909 IndexSet::SetUnion(rOtherSet);
00910 mAttributeMap.clear();
00911 }
00912
00913
00914 template<class Attr>
00915 void TaIndexSet<Attr>::SetUnion(const TaIndexSet& rOtherSet) {
00916 IndexSet::SetUnion(rOtherSet);
00917 mAttributeMap.clear();
00918 }
00919
00920
00921 template<class Attr>
00922 void TaIndexSet<Attr>::SetIntersection(const IndexSet& rOtherSet) {
00923 IndexSet::SetIntersection(rOtherSet);
00924 mAttributeMap.clear();
00925 }
00926
00927
00928 template<class Attr>
00929 void TaIndexSet<Attr>::SetIntersection(const TaIndexSet& rOtherSet) {
00930 IndexSet::SetIntersection(rOtherSet);
00931 mAttributeMap.clear();
00932 }
00933
00934
00935
00936 }
00937
00938 #define INDEXSET_H
00939 #endif