25 #ifndef FAUDES_ABASESET_H
26 #define FAUDES_ABASESET_H
47 template<
class T,
class Attr,
class Cmp=std::less<T> >
86 TaBaseSet(
const std::string& rFilename,
const std::string& rLabel =
"BaseSet");
110 {
return dynamic_cast<const TaBaseSet*
>(pType); };
155 virtual void Clear(
void);
256 virtual const Attr&
Attribute(
const T& rElem)
const;
283 virtual void Attribute(
const T& rElem,
const Attr& attr);
342 bool Insert(
const T& rElem);
354 bool Insert(
const T& rElem,
const Attr& attr);
384 bool Erase(
const T& rElem);
439 virtual void Detach(
void)
const;
469 virtual void DoAttributep(
const T& rElem,
const Attr* pAttr);
499 #define THIS TaBaseSet<T,Attr,Cmp>
500 #define TEMP template<class T, class Attr, class Cmp>
501 #define BASE TBaseSet<T,Cmp>
506 mpDefAttribute(new Attr())
508 FD_DC(
"TaBaseSet(" <<
this <<
")::TaBaseSet()");
515 mpDefAttribute(new Attr())
517 FD_DC(
"TaBaseSet(" <<
this <<
")::TaBaseSet(TaBaseSet " << &rOtherSet <<
"): copy construct");
524 mpDefAttribute(new Attr())
526 FD_DC(
"TaBaseSet(" <<
this <<
")::TaBaseSet(TBaseSet " << &rOtherSet <<
"): copy construct");
531 TEMP THIS::TaBaseSet(
const std::string& rFileName,
const std::string& rLabel) :
533 mpDefAttribute(new Attr())
535 FD_DC(
"TaBaseSet(" <<
this <<
")::TaBaseSet()");
542 FD_DC(
"TaBaseSet(" <<
this <<
")::~TaBaseSet()");
544 if(mpDefAttribute)
delete mpDefAttribute;
551 FD_DC(
"TaBaseSet(" <<
this <<
")::DoAssign(TaBaseSet " << &rSourceSet <<
")");
553 if(
this==&rSourceSet)
return;
555 BASE::DoAssign(rSourceSet);
564 DoAssign(rSourceSet);
570 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..)");
571 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): dst type " <<
typeid(*this).name());
572 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): src type " <<
typeid(rSourceSet).name());
574 BASE::DoAssign(rSourceSet);
580 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): dst attribute type " <<
typeid(*mpDefAttribute).name());
581 if(
typeid(*mpDefAttribute) ==
typeid(
AttributeVoid) )
return *
this;
583 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): src attribute type " <<
typeid(*rSourceSet.
Attributep()).name());
586 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): try attribute cast");
587 if(!mpDefAttribute->Cast(rSourceSet.
Attributep()))
return *
this;
589 BASE::mpAttributes=
new std::map<T,AttributeVoid*>();
590 BASE::pAttributes=BASE::mpAttributes;
591 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): mind attributes");
594 Attr* attr=
new Attr;
595 attr->Assign(*ait->second);
596 (*BASE::pAttributes)[ait->first]=attr;
599 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): done");
608 BASE::DoAssign(rSourceSet);
617 FD_DC(
"TaBaseSet::DoEqual()");
619 if(!BASE::DoEqual(rOtherSet))
663 return DoEqual(rOtherSet);
667 TEMP void THIS::Detach(
void)
const {
668 FD_DC(
"TaBaseSet(" <<
this <<
")::Detach(void)");
671 if(BASE::mDetached)
return;
674 THIS* fake_const =
const_cast< THIS*
>(
this);
677 std::map<T,AttributeVoid*>* acopy =
new std::map<T,AttributeVoid*>();
678 for(
aiterator ait=BASE::pAttributes->begin(); ait!=BASE::pAttributes->end(); ++ait) {
679 Attr* attr=
new Attr();
680 attr->Assign(*ait->second);
681 (*acopy)[ait->first]=attr;
688 fake_const->mpAttributes=acopy;
689 fake_const->pAttributes=acopy;
696 FD_DC(
"TaBaseSet(" <<
this <<
")::Clear()");
703 TEMP bool THIS::Insert(
const T& rElem) {
704 FD_DC(
"TvIndexSet(" <<
this <<
")::Insert(elem)");
705 bool ret=BASE::Insert(rElem);
710 TEMP bool THIS::Insert(
const T& rElem,
const Attr& rAttr) {
711 FD_DC(
"TaIndexSet(" <<
this <<
")::Insert(elem,attr)");
713 bool ret=BASE::pSet->insert(rElem).second;
714 DoAttributep(rElem,&rAttr);
720 bool doattr=AttributeTry(rOtherSet.
Attribute());
721 FD_DC(
"TaBaseSet(" <<
this <<
")::InsertSet( [v] " << &rOtherSet <<
"): doattr=" << doattr);
725 while ((it1 != BASE::pSet->end()) && (it2 != rOtherSet.
pSet->end())) {
729 else if (*it1 == *it2) {
735 if(doattr) DoAttributep(*it2,&rOtherSet.
Attribute(*it2));
739 while (it2 != rOtherSet.
pSet->end()) {
741 if(doattr) DoAttributep(*it2,&rOtherSet.
Attribute(*it2));
748 FD_DC(
"TaBaseSet(" <<
this <<
")::InsertSet( [a] " << &rOtherSet <<
")");
752 while ((it1 != BASE::pSet->end()) && (it2 != rOtherSet.
pSet->end())) {
756 else if (*it1 == *it2) {
765 while (it2 != rOtherSet.
pSet->end()) {
773 TEMP bool THIS::Erase(
const T& rElem) {
775 DoAttributep(rElem,(
const Attr*) 0);
776 return (BASE::pSet->erase(rElem)!=0);
781 TEMP typename faudes::BASE::Iterator THIS::Erase(
const Iterator& pos) {
782 #ifdef FAUDES_CHECKED
783 if(pos == BASE::End()) {
784 std::stringstream errstr;
785 errstr <<
"iterator out of range " << std::endl;
786 throw Exception(
"TaBase::Erase", errstr.str(), 60);
790 return BASE::Erase(pos);
796 FD_DC(
"TaBaseSet(" <<
this <<
")::EraseSet(" << rOtherSet.
ToString() <<
")");
802 while ((it != BASE::pSet->end()) && (oit != rOtherSet.
pSet->end())) {
804 it=BASE::pSet->lower_bound(*oit);
806 else if (*it == *oit) {
807 DoAttributep(*it,(
const Attr*) 0);
811 BASE::pSet->erase(tmpit);
814 oit=rOtherSet.
pSet->lower_bound(*it);
822 FD_DC(
"TaIndexSet(" <<
this <<
")::RestrictSet(" << rOtherSet.
ToString() <<
")");
828 while ((it != BASE::pSet->end()) && (oit != rOtherSet.
pSet->end())) {
830 DoAttributep(*it,(
const Attr*) 0);
833 BASE::pSet->erase(tmpit);
835 else if (*it == *oit) {
843 while(it != BASE::pSet->end()) {
844 DoAttributep(*it,(
const Attr*) 0);
847 BASE::pSet->erase(tmpit);
867 TEMP const Attr* THIS::Attributep(
void)
const {
868 return mpDefAttribute;
872 TEMP const Attr& THIS::Attribute(
void)
const {
873 return *mpDefAttribute;
877 TEMP bool THIS::AttributeTry(
const Type& rAttr)
const {
878 return mpDefAttribute->Cast(&rAttr)!=NULL;
883 TEMP Attr* THIS::Attributep(
const T& rElem) {
884 #ifdef FAUDES_CHECKED
885 if(!THIS::Exists(rElem)) {
886 std::stringstream errstr;
887 errstr <<
"element \"" << this->Str(rElem) <<
"\" not member of set " << this->Name() << std::endl;
888 throw Exception(
"TaBaseSet::Attributep(elem)", errstr.str(), 60);
894 Attr* res=DoAttributep(rElem);
900 TEMP const Attr& THIS::Attribute(
const T& rElem)
const {
901 #ifdef FAUDES_CHECKED
902 if(!THIS::Exists(rElem)) {
903 std::stringstream errstr;
904 errstr <<
"element \"" << this->Str(rElem) <<
"\" not member of set " << this->Name() << std::endl;
905 throw Exception(
"TaBaseSet::Attribute(elem)", errstr.str(), 60);
909 const Attr* res=DoAttributep(rElem);
911 if(!res) res=mpDefAttribute;
917 TEMP void THIS::Attribute(
const T& rElem,
const Type& attr) {
918 #ifdef FAUDES_CHECKED
919 if (!THIS::Exists(rElem)) {
920 std::stringstream errstr;
921 errstr <<
"element \"" << this->Str(rElem) <<
"\" not member of set " << this->Name() << std::endl;
922 throw Exception(
"TaBaseSet::Attribute(elem,attr)", errstr.str(), 60);
925 if(!AttributeTry(attr)) {
926 std::stringstream errstr;
927 errstr <<
"cannot cast attribute " << std::endl;
928 throw Exception(
"TaBaseSet::Attribute(elem,attr)", errstr.str(), 63);
931 DoAttributep(rElem, dynamic_cast<const Attr*>(&attr));
935 TEMP void THIS::AttributeTry(
const T& rElem,
const Type& attr) {
936 if(!AttributeTry(attr))
return;
937 #ifdef FAUDES_CHECKED
938 if (!THIS::Exists(rElem)) {
939 std::stringstream errstr;
940 errstr <<
"element \"" << this->Str(rElem) <<
"\" not member of set " << this->Name() << std::endl;
941 throw Exception(
"TaBaseSet::Attribute(elem)", errstr.str(), 60);
945 DoAttributep(rElem, dynamic_cast<const Attr*>(&attr));
949 TEMP void THIS::Attribute(
const T& rElem,
const Attr& attr) {
950 #ifdef FAUDES_CHECKED
951 if (!THIS::Exists(rElem)) {
952 std::stringstream errstr;
953 errstr <<
"element \"" << this->Str(rElem) <<
"\" not member of set " << this->Name() << std::endl;
954 throw Exception(
"TaBaseSet::Attribute(elem)", errstr.str(), 60);
958 DoAttributep(rElem, &attr);
964 if(!AttributeTry(rOtherSet.
Attribute())) {
965 std::stringstream errstr;
966 errstr <<
"cannot cast attribute " << std::endl;
967 throw Exception(
"TaBaseSet::Attribute(elem,attr)", errstr.str(), 63);
972 while ((it1 != BASE::pSet->end()) && (it2 != rOtherSet.
pSet->end())) {
976 else if (*it1 == *it2) {
977 DoAttributep(*it1,&rOtherSet.
Attribute(*it2));
992 while ((it1 != BASE::pSet->end()) && (it2 != rOtherSet.
pSet->end())) {
996 else if (*it1 == *it2) {
997 DoAttributep(*it1,&rOtherSet.
Attribute(*it2));
1008 TEMP void THIS::ClrAttribute(
const T& rElem) {
1010 DoAttributep(rElem,(
const Attr*) 0);
1014 TEMP void THIS::ClearAttributes(
void) {
1016 if(BASE::pAttributes->size()==0)
return;
1020 if(BASE::mpAttributes) {
1021 aiterator ait=BASE::mpAttributes->begin();
1022 for(;ait!=BASE::mpAttributes->end();++ait)
1024 BASE::mpAttributes->clear();
1030 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet)");
1032 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet): 1");
1036 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet): 2");
1037 if(
typeid(*rOtherSet.
Attributep())!=
typeid(*this->Attributep()))
1040 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet): 3");
1042 if(this->AttributesSize()==0)
1045 aiterator ait1 = BASE::pAttributes->begin();
1047 while ((ait1 != BASE::pAttributes->end()) && (ait2 != rOtherSet.
pAttributes->end())) {
1048 if (ait1->first < ait2->first) {
1051 else if (ait1->first == ait2->first) {
1052 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet): cmp " << ait1->second->ToString()
1053 <<
" vs " << ait2->second->ToString());
1054 if( ! ait1->second->Equal(*ait2->second))
return false;
1063 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet): pass");
1069 FD_DC(
"TaBaseSet::EqualAttributes(TaBaseSet)");
1075 if(this->AttributesSize()==0)
1078 aiterator ait1 = BASE::pAttributes->begin();
1080 while ((ait1 != BASE::pAttributes->end()) && (ait2 != rOtherSet.
pAttributes->end())) {
1081 if (ait1->first < ait2->first) {
1084 else if (ait1->first == ait2->first) {
1085 if( *(static_cast<const Attr*>(ait1->second)) !=
1086 *(static_cast<const Attr*>(ait2->second)) )
return false;
1099 TEMP const Attr* THIS::DoAttributep(
const T& rElem)
const {
1101 ait=BASE::pAttributes->find(rElem);
1102 if(ait==BASE::pAttributes->end())
return NULL;
1103 return static_cast<const Attr*
>(ait->second);
1107 TEMP Attr* THIS::DoAttributep(
const T& rElem) {
1108 FD_DC(
"TaBaseSet::DoAttributep(Elem)");
1109 #ifdef FAUDES_DEBUG_CODE
1110 if(BASE::pAttributes!=BASE::mpAttributes) {
1111 FD_ERR(
"TaBaseSet::DoAttributep(Elem): attributes not detached");
1116 ait=BASE::pAttributes->find(rElem);
1118 if( ait==BASE::pAttributes->end() ) {
1119 Attr* attr =
new Attr();
1120 attr->Assign(*mpDefAttribute);
1121 (*BASE::pAttributes)[rElem]=attr;
1124 return static_cast<Attr*
>(ait->second);
1128 TEMP void THIS::DoAttributep(
const T& rElem,
const Type* pAttr) {
1129 FD_DC(
"TaBaseSet::DoAttributep([v] " << this->Str(rElem) <<
", ...)");
1130 #ifdef FAUDES_DEBUG_CODE
1131 if(BASE::pAttributes!=BASE::mpAttributes) {
1132 FD_ERR(
"TaBaseSet::DoAttributep([v] set): attributes not detached");
1140 ait=BASE::pAttributes->find(rElem);
1141 if(ait!=BASE::pAttributes->end() )
1142 oldattr=ait->second;
1145 FD_DC(
"TaBaseSet::DoAttributep([v] " << this->Str(rElem) <<
", ...): default 1");
1146 if(oldattr==NULL)
return;
1148 BASE::pAttributes->erase(ait);
1153 FD_DC(
"TaBaseSet::DoAttributep([v] " << this->Str(rElem) <<
", ...): default 2");
1154 if(oldattr==NULL)
return;
1156 BASE::pAttributes->erase(ait);
1159 FD_DC(
"TaBaseSet::DoAttributep([v] " << this->Str(rElem) <<
", ...): " << newattr->
ToString());
1162 Attr* attr =
new Attr();
1163 attr->Assign(*newattr);
1164 (*BASE::pAttributes)[rElem]=attr;
1172 TEMP void THIS::DoAttributep(
const T& rElem,
const Attr* pAttr) {
1173 FD_DC(
"TaBaseSet::DoAttributep([a] " << this->Str(rElem) <<
", ...)");
1174 #ifdef FAUDES_DEBUG_CODE
1175 if(BASE::pAttributes!=BASE::mpAttributes) {
1176 FD_ERR(
"TaBaseSet::DoAttributep([a] set): attributes not detached");
1183 ait=BASE::pAttributes->find(rElem);
1184 if(ait!=BASE::pAttributes->end() )
1185 oldattr=ait->second;
1188 if(oldattr==NULL)
return;
1190 BASE::pAttributes->erase(ait);
1194 if(pAttr->IsDefault()) {
1195 if(oldattr==NULL)
return;
1197 BASE::pAttributes->erase(ait);
1202 Attr* attr =
new Attr();
1204 (*BASE::pAttributes)[rElem]=attr;
1208 *
static_cast<Attr*
>(oldattr)=*pAttr;
1213 return BASE::pAttributes->size();