24 #ifndef FAUDES_ABASESET_H
25 #define FAUDES_ABASESET_H
46 template<
class T,
class Attr,
class Cmp=std::less<T> >
85 TaBaseSet(
const std::string& rFilename,
const std::string& rLabel =
"BaseSet");
150 virtual void Clear(
void);
251 virtual const Attr&
Attribute(
const T& rElem)
const;
278 virtual void Attribute(
const T& rElem,
const Attr& attr);
337 bool Insert(
const T& rElem);
349 bool Insert(
const T& rElem,
const Attr& attr);
379 bool Erase(
const T& rElem);
438 virtual void Detach(
void)
const;
468 virtual void DoAttributep(
const T& rElem,
const Attr* pAttr);
498 #define THIS TaBaseSet<T,Attr,Cmp>
499 #define TEMP template<class T, class Attr, class Cmp>
500 #define BASE TBaseSet<T,Cmp>
505 mpDefAttribute(new Attr())
507 FD_DC(
"TaBaseSet(" <<
this <<
")::TaBaseSet()");
514 mpDefAttribute(new Attr())
516 FD_DC(
"TaBaseSet(" <<
this <<
")::TaBaseSet(TaBaseSet " << &rOtherSet <<
"): copy construct");
523 mpDefAttribute(new Attr())
525 FD_DC(
"TaBaseSet(" <<
this <<
")::TaBaseSet(TBaseSet " << &rOtherSet <<
"): copy construct");
530 TEMP THIS::TaBaseSet(
const std::string& rFileName,
const std::string& rLabel) :
532 mpDefAttribute(new Attr())
534 FD_DC(
"TaBaseSet(" <<
this <<
")::TaBaseSet()");
541 FD_DC(
"TaBaseSet(" <<
this <<
")::~TaBaseSet()");
543 if(mpDefAttribute)
delete mpDefAttribute;
550 FD_DC(
"TaBaseSet(" <<
this <<
")::DoAssign(TaBaseSet " << &rSourceSet <<
")");
552 if(
this==&rSourceSet)
return;
554 BASE::DoAssign(rSourceSet);
563 DoAssign(rSourceSet);
569 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..)");
570 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): dst type " <<
typeid(*this).name());
571 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): src type " <<
typeid(rSourceSet).name());
573 BASE::DoAssign(rSourceSet);
579 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): dst attribute type " <<
typeid(*mpDefAttribute).name());
580 if(
typeid(*mpDefAttribute) ==
typeid(
AttributeVoid) )
return *
this;
582 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): src attribute type " <<
typeid(*rSourceSet.
Attributep()).name());
585 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): try attribute cast");
586 if(!mpDefAttribute->Cast(rSourceSet.
Attributep()))
return *
this;
588 BASE::mpAttributes=
new std::map<T,AttributeVoid*>();
589 BASE::pAttributes=BASE::mpAttributes;
590 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): mind attributes");
593 Attr* attr=
new Attr;
594 attr->Assign(*ait->second);
595 (*BASE::pAttributes)[ait->first]=attr;
598 FD_DC(
"TaBaseSet(" <<
this <<
")::Assign(..): done");
607 BASE::DoAssign(rSourceSet);
616 FD_DC(
"TaBaseSet::DoEqual()");
618 if(!BASE::DoEqual(rOtherSet))
662 return DoEqual(rOtherSet);
666 TEMP void THIS::Detach(
void)
const {
667 FD_DC(
"TaBaseSet(" <<
this <<
")::Detach(void)");
670 if(BASE::mDetached)
return;
673 THIS* fake_const =
const_cast< THIS*
>(
this);
676 std::map<T,AttributeVoid*>* acopy =
new std::map<T,AttributeVoid*>();
677 for(
aiterator ait=BASE::pAttributes->begin(); ait!=BASE::pAttributes->end(); ++ait) {
678 Attr* attr=
new Attr();
679 attr->Assign(*ait->second);
680 (*acopy)[ait->first]=attr;
687 fake_const->mpAttributes=acopy;
688 fake_const->pAttributes=acopy;
695 FD_DC(
"TaBaseSet(" <<
this <<
")::Clear()");
702 TEMP bool THIS::Insert(
const T& rElem) {
703 FD_DC(
"TvIndexSet(" <<
this <<
")::Insert(elem)");
704 bool ret=BASE::Insert(rElem);
709 TEMP bool THIS::Insert(
const T& rElem,
const Attr& rAttr) {
710 FD_DC(
"TaIndexSet(" <<
this <<
")::Insert(elem,attr)");
712 bool ret=BASE::pSet->insert(rElem).second;
713 DoAttributep(rElem,&rAttr);
719 bool doattr=AttributeTry(rOtherSet.
Attribute());
720 FD_DC(
"TaBaseSet(" <<
this <<
")::InsertSet( [v] " << &rOtherSet <<
"): doattr=" << doattr);
724 while ((it1 != BASE::pSet->end()) && (it2 != rOtherSet.
pSet->end())) {
728 else if (*it1 == *it2) {
734 if(doattr) DoAttributep(*it2,&rOtherSet.
Attribute(*it2));
738 while (it2 != rOtherSet.
pSet->end()) {
740 if(doattr) DoAttributep(*it2,&rOtherSet.
Attribute(*it2));
747 FD_DC(
"TaBaseSet(" <<
this <<
")::InsertSet( [a] " << &rOtherSet <<
")");
751 while ((it1 != BASE::pSet->end()) && (it2 != rOtherSet.
pSet->end())) {
755 else if (*it1 == *it2) {
764 while (it2 != rOtherSet.
pSet->end()) {
772 TEMP bool THIS::Erase(
const T& rElem) {
774 DoAttributep(rElem,(
const Attr*) 0);
775 return (BASE::pSet->erase(rElem)!=0);
797 FD_DC(
"TaBaseSet(" <<
this <<
")::EraseSet(" << rOtherSet.
ToString() <<
")");
803 while ((it != BASE::pSet->end()) && (oit != rOtherSet.
pSet->end())) {
805 it=BASE::pSet->lower_bound(*oit);
807 else if (*it == *oit) {
808 DoAttributep(*it,(
const Attr*) 0);
812 BASE::pSet->erase(tmpit);
815 oit=rOtherSet.
pSet->lower_bound(*it);
823 FD_DC(
"TaIndexSet(" <<
this <<
")::RestrictSet(" << rOtherSet.
ToString() <<
")");
829 while ((it != BASE::pSet->end()) && (oit != rOtherSet.
pSet->end())) {
831 DoAttributep(*it,(
const Attr*) 0);
834 BASE::pSet->erase(tmpit);
836 else if (*it == *oit) {
844 while(it != BASE::pSet->end()) {
845 DoAttributep(*it,(
const Attr*) 0);
848 BASE::pSet->erase(tmpit);
868 TEMP const Attr* THIS::Attributep(
void)
const {
869 return mpDefAttribute;
873 TEMP const Attr& THIS::Attribute(
void)
const {
874 return *mpDefAttribute;
878 TEMP bool THIS::AttributeTry(
const Type& rAttr)
const {
879 return mpDefAttribute->Cast(&rAttr)!=NULL;
884 TEMP Attr* THIS::Attributep(
const T& rElem) {
885 #ifdef FAUDES_CHECKED
886 if(!THIS::Exists(rElem)) {
887 std::stringstream errstr;
888 errstr <<
"element \"" << this->Str(rElem) <<
"\" not member of set " << this->Name() << std::endl;
889 throw Exception(
"TaBaseSet::Attributep(elem)", errstr.str(), 60);
895 Attr* res=DoAttributep(rElem);
901 TEMP const Attr& THIS::Attribute(
const T& rElem)
const {
902 #ifdef FAUDES_CHECKED
903 if(!THIS::Exists(rElem)) {
904 std::stringstream errstr;
905 errstr <<
"element \"" << this->Str(rElem) <<
"\" not member of set " << this->Name() << std::endl;
906 throw Exception(
"TaBaseSet::Attribute(elem)", errstr.str(), 60);
910 const Attr* res=DoAttributep(rElem);
912 if(!res) res=mpDefAttribute;
918 TEMP void THIS::Attribute(
const T& rElem,
const Type& attr) {
919 #ifdef FAUDES_CHECKED
920 if (!THIS::Exists(rElem)) {
921 std::stringstream errstr;
922 errstr <<
"element \"" << this->Str(rElem) <<
"\" not member of set " << this->Name() << std::endl;
923 throw Exception(
"TaBaseSet::Attribute(elem,attr)", errstr.str(), 60);
926 if(!AttributeTry(attr)) {
927 std::stringstream errstr;
928 errstr <<
"cannot cast attribute " << std::endl;
929 throw Exception(
"TaBaseSet::Attribute(elem,attr)", errstr.str(), 63);
932 DoAttributep(rElem, dynamic_cast<const Attr*>(&attr));
936 TEMP void THIS::AttributeTry(
const T& rElem,
const Type& attr) {
937 if(!AttributeTry(attr))
return;
938 #ifdef FAUDES_CHECKED
939 if (!THIS::Exists(rElem)) {
940 std::stringstream errstr;
941 errstr <<
"element \"" << this->Str(rElem) <<
"\" not member of set " << this->Name() << std::endl;
942 throw Exception(
"TaBaseSet::Attribute(elem)", errstr.str(), 60);
946 DoAttributep(rElem, dynamic_cast<const Attr*>(&attr));
950 TEMP void THIS::Attribute(
const T& rElem,
const Attr& attr) {
951 #ifdef FAUDES_CHECKED
952 if (!THIS::Exists(rElem)) {
953 std::stringstream errstr;
954 errstr <<
"element \"" << this->Str(rElem) <<
"\" not member of set " << this->Name() << std::endl;
955 throw Exception(
"TaBaseSet::Attribute(elem)", errstr.str(), 60);
959 DoAttributep(rElem, &attr);
965 if(!AttributeTry(rOtherSet.
Attribute())) {
966 std::stringstream errstr;
967 errstr <<
"cannot cast attribute " << std::endl;
968 throw Exception(
"TaBaseSet::Attribute(elem,attr)", errstr.str(), 63);
973 while ((it1 != BASE::pSet->end()) && (it2 != rOtherSet.
pSet->end())) {
977 else if (*it1 == *it2) {
978 DoAttributep(*it1,&rOtherSet.
Attribute(*it2));
993 while ((it1 != BASE::pSet->end()) && (it2 != rOtherSet.
pSet->end())) {
997 else if (*it1 == *it2) {
998 DoAttributep(*it1,&rOtherSet.
Attribute(*it2));
1009 TEMP void THIS::ClrAttribute(
const T& rElem) {
1011 DoAttributep(rElem,(
const Attr*) 0);
1015 TEMP void THIS::ClearAttributes(
void) {
1017 if(BASE::pAttributes->size()==0)
return;
1021 if(BASE::mpAttributes) {
1022 aiterator ait=BASE::mpAttributes->begin();
1023 for(;ait!=BASE::mpAttributes->end();++ait)
1025 BASE::mpAttributes->clear();
1031 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet)");
1033 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet): 1");
1037 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet): 2");
1038 if(
typeid(*rOtherSet.
Attributep())!=
typeid(*this->Attributep()))
1041 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet): 3");
1043 if(this->AttributesSize()==0)
1046 aiterator ait1 = BASE::pAttributes->begin();
1048 while ((ait1 != BASE::pAttributes->end()) && (ait2 != rOtherSet.
pAttributes->end())) {
1049 if (ait1->first < ait2->first) {
1052 else if (ait1->first == ait2->first) {
1053 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet): cmp " << ait1->second->ToString()
1054 <<
" vs " << ait2->second->ToString());
1055 if( ! ait1->second->Equal(*ait2->second))
return false;
1064 FD_DC(
"TaBaseSet::EqualAttributes(TBaseSet): pass");
1070 FD_DC(
"TaBaseSet::EqualAttributes(TaBaseSet)");
1076 if(this->AttributesSize()==0)
1079 aiterator ait1 = BASE::pAttributes->begin();
1081 while ((ait1 != BASE::pAttributes->end()) && (ait2 != rOtherSet.
pAttributes->end())) {
1082 if (ait1->first < ait2->first) {
1085 else if (ait1->first == ait2->first) {
1086 if( *(static_cast<const Attr*>(ait1->second)) !=
1087 *(static_cast<const Attr*>(ait2->second)) )
return false;
1100 TEMP const Attr* THIS::DoAttributep(
const T& rElem)
const {
1102 ait=BASE::pAttributes->find(rElem);
1103 if(ait==BASE::pAttributes->end())
return NULL;
1104 return static_cast<const Attr*
>(ait->second);
1108 TEMP Attr* THIS::DoAttributep(
const T& rElem) {
1109 FD_DC(
"TaBaseSet::DoAttributep(Elem)");
1110 #ifdef FAUDES_DEBUG_CODE
1111 if(BASE::pAttributes!=BASE::mpAttributes) {
1112 FD_ERR(
"TaBaseSet::DoAttributep(Elem): attributes not detached");
1117 ait=BASE::pAttributes->find(rElem);
1119 if( ait==BASE::pAttributes->end() ) {
1120 Attr* attr =
new Attr();
1121 attr->Assign(*mpDefAttribute);
1122 (*BASE::pAttributes)[rElem]=attr;
1125 return static_cast<Attr*
>(ait->second);
1129 TEMP void THIS::DoAttributep(
const T& rElem,
const Type* pAttr) {
1130 FD_DC(
"TaBaseSet::DoAttributep([v] " << this->Str(rElem) <<
", ...)");
1131 #ifdef FAUDES_DEBUG_CODE
1132 if(BASE::pAttributes!=BASE::mpAttributes) {
1133 FD_ERR(
"TaBaseSet::DoAttributep([v] set): attributes not detached");
1141 ait=BASE::pAttributes->find(rElem);
1142 if(ait!=BASE::pAttributes->end() )
1143 oldattr=ait->second;
1146 FD_DC(
"TaBaseSet::DoAttributep([v] " << this->Str(rElem) <<
", ...): default 1");
1147 if(oldattr==NULL)
return;
1149 BASE::pAttributes->erase(ait);
1154 FD_DC(
"TaBaseSet::DoAttributep([v] " << this->Str(rElem) <<
", ...): default 2");
1155 if(oldattr==NULL)
return;
1157 BASE::pAttributes->erase(ait);
1160 FD_DC(
"TaBaseSet::DoAttributep([v] " << this->Str(rElem) <<
", ...): " << newattr->
ToString());
1163 Attr* attr =
new Attr();
1164 attr->Assign(*newattr);
1165 (*BASE::pAttributes)[rElem]=attr;
1173 TEMP void THIS::DoAttributep(
const T& rElem,
const Attr* pAttr) {
1174 FD_DC(
"TaBaseSet::DoAttributep([a] " << this->Str(rElem) <<
", ...)");
1175 #ifdef FAUDES_DEBUG_CODE
1176 if(BASE::pAttributes!=BASE::mpAttributes) {
1177 FD_ERR(
"TaBaseSet::DoAttributep([a] set): attributes not detached");
1184 ait=BASE::pAttributes->find(rElem);
1185 if(ait!=BASE::pAttributes->end() )
1186 oldattr=ait->second;
1189 if(oldattr==NULL)
return;
1191 BASE::pAttributes->erase(ait);
1195 if(pAttr->IsDefault()) {
1196 if(oldattr==NULL)
return;
1198 BASE::pAttributes->erase(ait);
1203 Attr* attr =
new Attr();
1205 (*BASE::pAttributes)[rElem]=attr;
1209 *
static_cast<Attr*
>(oldattr)=*pAttr;
1214 return BASE::pAttributes->size();