From 34b2e9bd326e93c1d1834357cf807314ee62bb06 Mon Sep 17 00:00:00 2001 From: mpv Date: Thu, 28 Apr 2016 13:46:50 +0300 Subject: [PATCH] Issue #1037 : implementation of cashing of values for method isInList --- .../ExchangePlugin_Validators.cpp | 2 +- src/Model/Model_AttributeSelectionList.cpp | 37 +++++++++++++++++-- src/Model/Model_AttributeSelectionList.h | 11 +++++- .../ModelAPI_AttributeSelectionList.h | 5 +++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/ExchangePlugin/ExchangePlugin_Validators.cpp b/src/ExchangePlugin/ExchangePlugin_Validators.cpp index e05794640..e08b9854a 100644 --- a/src/ExchangePlugin/ExchangePlugin_Validators.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Validators.cpp @@ -24,7 +24,7 @@ bool ExchangePlugin_FormatValidator::parseFormats(const std::list& bool result = true; for (; it != theArguments.end(); ++it) { std::string anArg = *it; - int aSepPos = anArg.find(":"); + size_t aSepPos = anArg.find(":"); if (aSepPos == std::string::npos) { result = false; continue; diff --git a/src/Model/Model_AttributeSelectionList.cpp b/src/Model/Model_AttributeSelectionList.cpp index 56f82cc6a..86581b348 100644 --- a/src/Model/Model_AttributeSelectionList.cpp +++ b/src/Model/Model_AttributeSelectionList.cpp @@ -14,11 +14,12 @@ #include #include #include - #include #include #include #include +#include +#include using namespace std; @@ -85,9 +86,6 @@ void Model_AttributeSelectionList::removeLast() } } -#include -#include - // copies named shapes: we need the implementation of this static void CopyNS(const Handle(TNaming_NamedShape)& theFrom, const Handle(TDF_Attribute)& theTo) @@ -189,6 +187,25 @@ bool Model_AttributeSelectionList::isInList(const ResultPtr& theContext, const std::shared_ptr& theSubShape, const bool theTemporarily) { + if (myCash.size()) { // the cashing is active + std::map > >::iterator aContext = + myCash.find(theContext); + if (aContext != myCash.end()) { + // iterate shapes because "isEqual" method must be called for each shape + std::list >::iterator aShapes = aContext->second.begin(); + for(; aShapes != aContext->second.end(); aShapes++) { + if (!theSubShape.get()) { + if (!aShapes->get()) + return true; + } else { + if (theSubShape->isEqual(*aShapes)) + return true; + } + } + } + return false; + } + // no-cash method for(int anIndex = size() - 1; anIndex >= 0; anIndex--) { AttributeSelectionPtr anAttr = value(anIndex); if (anAttr.get()) { @@ -275,3 +292,15 @@ Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel) theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType); } } + +void Model_AttributeSelectionList::cashValues(const bool theEnabled) { + myCash.clear(); // empty list as indicator that cash is not used + if (theEnabled) { + for(int anIndex = size() - 1; anIndex >= 0; anIndex--) { + AttributeSelectionPtr anAttr = value(anIndex); + if (anAttr.get()) { + myCash[anAttr->context()].push_back(anAttr->value()); + } + } + } +} diff --git a/src/Model/Model_AttributeSelectionList.h b/src/Model/Model_AttributeSelectionList.h index d28798dda..2df86e31d 100644 --- a/src/Model/Model_AttributeSelectionList.h +++ b/src/Model/Model_AttributeSelectionList.h @@ -13,6 +13,7 @@ #include #include #include +#include /**\class Model_AttributeSelectionList * \ingroup DataModel @@ -23,8 +24,11 @@ class Model_AttributeSelectionList : public ModelAPI_AttributeSelectionList { Handle(TDataStd_Integer) mySize; ///< Contains size of this list - Handle(TDataStd_Comment) mySelectionType; ///< Contains current type name (same as selection attribute) + /// Contains current type name (same as selection attribute) + Handle(TDataStd_Comment) mySelectionType; std::shared_ptr myTmpAttr; ///< temporary attribute (the last one) + /// the cashed shapes to optimize isInList method: from context to set of shapes in this context + std::map > > myCash; public: /// Adds the new reference to the end of the list /// \param theContext object where the sub-shape was selected @@ -74,6 +78,11 @@ public: /// Returns true if attribute was initialized by some value MODEL_EXPORT virtual bool isInitialized(); + /// Starts or stops cashing of the values in the attribute (the cash may become invalid + /// on modification of the attribute or sub-elements, so the cash must be enabled only + /// during non-modification operations with this attribute) + MODEL_EXPORT virtual void cashValues(const bool theEnabled); + protected: /// Objects are created for features automatically MODEL_EXPORT Model_AttributeSelectionList(TDF_Label& theLabel); diff --git a/src/ModelAPI/ModelAPI_AttributeSelectionList.h b/src/ModelAPI/ModelAPI_AttributeSelectionList.h index b5d1ff466..9f0b0a69a 100644 --- a/src/ModelAPI/ModelAPI_AttributeSelectionList.h +++ b/src/ModelAPI/ModelAPI_AttributeSelectionList.h @@ -64,6 +64,11 @@ class ModelAPI_AttributeSelectionList : public ModelAPI_Attribute /// Returns all attributes virtual void clear() = 0; + /// Starts or stops cashing of the values in the attribute (the cash may become invalid + /// on modification of the attribute or sub-elements, so the cash must be enabled only + /// during non-modification operations with this attribute) + virtual void cashValues(const bool theEnabled) = 0; + /// Returns the type of this class of attributes static std::string typeId() { -- 2.39.2