#ifndef CT_SPARSEATTRIBUTEMANAGER_H #define CT_SPARSEATTRIBUTEMANAGER_H #include #include "ct_global/ct_cloudscontext.h" #include "ct_global/ct_repositorymanager.h" #include "ct_attributes/managers/abstract/ct_abstractxattributemanager.h" #include "ct_attributes/setters/ct_sparseattributesetter.h" #include template class CT_SparseAttributeManager : public CT_AbstractXAttributeManager { using SuperClass = CT_AbstractXAttributeManager; public: using PEF = typename TCIR::element_type::element_type; using Setter = CT_SparseAttributeSetter; using SetterPtr = std::unique_ptr; Setter createAttributesSetter(CT_CIR cir); SetterPtr createAttributesSetterPtr(CT_CIR cir); CT_SparseAttributeManager(); bool hasBeenSet(const size_t& globalIndex) const final; bool visitValues(typename CT_AbstractXAttributeManager::Visitor v) const final; bool visitAllIndexesSet(typename CT_AbstractXAttributeManager::IVisitor v) const final; size_t numberOfSetValues() const final; bool isEmpty() const final; const T& tAt(const size_t& globalIndex, bool* hasBeenSet = nullptr) const final; const T& tAtLocalIndex(const size_t& localIndex) const final; void createCollectionsIfNotCreated(); bool copyAndModifyAttributesOfSForD(CT_CIR source, CT_CIR destination, typename SuperClass::AttributeModificator modificator = nullptr) final; protected: using SetterPtrAttributesCollection = typename Setter::PtrParseAttributesCollectionType; using SetterAttributesCollection = typename Setter::template ParseAttributesCollection; SetterPtrAttributesCollection mAttributes; T mDefaultValue; }; template CT_SparseAttributeManager::CT_SparseAttributeManager() : mDefaultValue(T()) { } template typename CT_SparseAttributeManager::Setter CT_SparseAttributeManager::createAttributesSetter(CT_CIR cir) { this->createCollectionsIfNotCreated(); return CT_SparseAttributeSetter(cir, mAttributes); } template typename CT_SparseAttributeManager::SetterPtr CT_SparseAttributeManager::createAttributesSetterPtr(CT_CIR cir) { this->createCollectionsIfNotCreated(); return std::make_unique::SetterPtr>(cir, mAttributes); } template bool CT_SparseAttributeManager::hasBeenSet(const size_t& globalIndex) const { if(mAttributes.isNull()) return false; return mAttributes->abstractModifiableCloudIndex()->contains(globalIndex); } template bool CT_SparseAttributeManager::visitValues(typename CT_AbstractXAttributeManager::Visitor v) const { if(mAttributes.isNull()) return true; const SetterAttributesCollection* collection = dynamic_cast(mAttributes->abstractModifiableCloudIndex()); auto it = collection->cbegin(); auto end = collection->cend(); while(it != end) { if(!v((*it).first, (*it).second)) return false; ++it; } return true; } template bool CT_SparseAttributeManager::visitAllIndexesSet(typename CT_AbstractXAttributeManager::IVisitor v) const { if(mAttributes.isNull()) return true; const SetterAttributesCollection* collection = dynamic_cast(mAttributes->abstractModifiableCloudIndex()); auto it = collection->cbegin(); auto end = collection->cend(); while(it != end) { if(!v((*it).first)) return false; ++it; } return true; } template size_t CT_SparseAttributeManager::numberOfSetValues() const { if(mAttributes.isNull()) return 0; return mAttributes->abstractModifiableCloudIndex()->size(); } template bool CT_SparseAttributeManager::isEmpty() const { if(mAttributes.isNull()) return true; return (mAttributes->abstractModifiableCloudIndex()->size() == 0); } template const T& CT_SparseAttributeManager::tAt(const size_t& globalIndex, bool* hasBeenSet) const { SetterAttributesCollection* c = dynamic_cast(mAttributes->abstractModifiableCloudIndex()); const auto it = c->findAtGlobalIndex(globalIndex); if(it != c->cend()) { if(hasBeenSet != nullptr) (*hasBeenSet) = true; return it->second; } if(hasBeenSet != nullptr) (*hasBeenSet) = false; return mDefaultValue; } template const T& CT_SparseAttributeManager::tAtLocalIndex(const size_t& localIndex) const { SetterAttributesCollection* c = dynamic_cast(mAttributes->abstractModifiableCloudIndex()); const auto it = c->findAtLocalIndex(localIndex); if(it != c->cend()) return it->second; return mDefaultValue; } template void CT_SparseAttributeManager::createCollectionsIfNotCreated() { if(!mAttributes.isNull()) return; mAttributes = PS_REPOSITORY->createNewIndexCloudTSyncWithPEF::ParseAttributesCollection>(); } template bool CT_SparseAttributeManager::copyAndModifyAttributesOfSForD(CT_CIR source, CT_CIR destination, typename SuperClass::AttributeModificator modificator) { if(mAttributes.isNull() // || source.dynamicCast().isNull() // correction AP 09/11/2022 || source.isNull() || destination.isNull() || destination.dynamicCast().isNull()) return false; auto sourceCI = source->abstractCloudIndex(); auto destCI = destination->abstractCloudIndex(); const auto sourceSize = sourceCI->size(); const auto destSize = destCI->size(); if(destSize == 0) return true; if(hasBeenSet(destCI->indexAt(0))) return false; auto setter = createAttributesSetter(destination.dynamicCast()); if(modificator == nullptr) { for(auto i=0; iindexAt(i), tAt(sourceCI->indexAt(i))); } } else { T destValue; for(auto i=0; iindexAt(i); const T& sourceValue = tAt(sourceCI->indexAt(i)); const auto destinationGlobalIndex = destCI->indexAt(i); const int ret = modificator(sourceGlobalIndex, sourceValue, destinationGlobalIndex, destValue); if(ret > 0) setter.setValueWithGlobalIndex(destinationGlobalIndex, destValue); else if(ret < 0) return true; } } return true; } #endif // CT_SPARSEATTRIBUTEMANAGER_H