Salome HOME
Improve performance in the Sketch edition.
authormpv <mpv@opencascade.com>
Fri, 7 Dec 2018 15:01:52 +0000 (18:01 +0300)
committermpv <mpv@opencascade.com>
Fri, 7 Dec 2018 15:02:20 +0000 (18:02 +0300)
src/Model/Model_AttributeRefList.cpp
src/Model/Model_AttributeRefList.h
src/Model/Model_Data.cpp
src/ModelAPI/ModelAPI_AttributeRefList.h
src/ModelAPI/ModelAPI_Result.cpp

index 4d77a91f7f2e68663c1be1a7d18c866ab7d1a0a7..4c152b929046792b437e691049bae46b6e17cb2d 100644 (file)
@@ -44,6 +44,12 @@ void Model_AttributeRefList::append(ObjectPtr theObject)
     myExtDocRef->Append(anEntry);
   } else return; // something is wrong
 
+  if (myHashUsed) {
+    myHashObjects.insert(theObject);
+    myHashIndex[myRef->Extent() - 1] = theObject;
+    myHashIndexNoEmpty[int(myHashIndexNoEmpty.size())] = theObject;
+  }
+
   // do it before the transaction finish to make just created/removed objects know dependencies
   // and reference from composite feature is removed automatically
   ADD_BACK_REF(theObject);
@@ -52,6 +58,7 @@ void Model_AttributeRefList::append(ObjectPtr theObject)
 
 void Model_AttributeRefList::remove(ObjectPtr theObject)
 {
+  eraseHash();
   if (theObject.get() != NULL) {
     if (owner()->document() == theObject->document()) {
       std::shared_ptr<Model_Data> aData;
@@ -79,7 +86,7 @@ void Model_AttributeRefList::remove(ObjectPtr theObject)
           if (anExtIter.Value() == anIdString.str().c_str()) {
             TDataStd_ListIteratorOfListOfExtendedString anExtIter2 = anExtIter;
             anExtIter2.Next();
-            if (anExtIter2.Value() == anEntry) { // fully maches, so, remove(don't copy)
+            if (anExtIter2.Value() == anEntry) { // fully matches, so, remove(don't copy)
               aFound = true;
               continue;
             }
@@ -117,6 +124,7 @@ void Model_AttributeRefList::remove(ObjectPtr theObject)
 
 void Model_AttributeRefList::clear()
 {
+  eraseHash();
   std::list<ObjectPtr> anOldList = list();
   myRef->Clear();
   std::list<ObjectPtr>::iterator anOldIter = anOldList.begin();
@@ -131,6 +139,10 @@ int Model_AttributeRefList::size(const bool theWithEmpty) const
 {
   if (theWithEmpty)
     return myRef->Extent();
+
+  if (myHashUsed)
+    return int(myHashIndexNoEmpty.size());
+
   int aResult = 0;
   const TDF_LabelList& aList = myRef->List();
   for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
@@ -177,15 +189,11 @@ ObjectPtr Model_AttributeRefList::iteratedObject(TDF_ListIteratorOfLabelList& th
 
 std::list<ObjectPtr> Model_AttributeRefList::list()
 {
+  createHash();
   std::list<ObjectPtr> aResult;
-  std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
-      owner()->document());
-  if (aDoc) {
-    const TDF_LabelList& aList = myRef->List();
-    TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
-    for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
-      aResult.push_back(iteratedObject(aLIter, anExtIter, aDoc));
-    }
+  std::map<int, ObjectPtr>::iterator aHashIter = myHashIndex.begin();
+  for(; aHashIter != myHashIndex.end(); aHashIter++) {
+    aResult.push_back(aHashIter->second);
   }
   return aResult;
 }
@@ -195,65 +203,24 @@ bool Model_AttributeRefList::isInList(const ObjectPtr& theObj)
   if(!theObj.get()) {
     return false;
   }
-  if (theObj->document() == owner()->document()) { // this document object
-    std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
-        owner()->document());
-    if (aDoc) {
-      std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
-      if (aData.get() && aData->isValid()) {
-        TDF_Label anObjLab = aData->label().Father();
-        const TDF_LabelList& aList = myRef->List();
-        for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
-          if (aLIter.Value().IsEqual(anObjLab)) {
-            return true;
-          }
-        }
-      }
-    }
-  } else { // external document object
-    // create new lists because for the current moment remove one of the duplicated elements
-    // from the list is buggy
-    std::ostringstream anIdString; // string with document Id
-    anIdString<<theObj->document()->id();
-    std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObj->data());
-    TCollection_AsciiString anEntry;
-    TDF_Tool::Entry(aData->label().Father(), anEntry);
-    bool aFound = false;
-    TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
-    for (; anExtIter.More(); anExtIter.Next()) {
-      if (anExtIter.Value() == anIdString.str().c_str()) {
-        anExtIter.Next();
-        if (anExtIter.Value() == anEntry) { // fully maches
-          return true;
-        }
-      } else {
-        anExtIter.Next();
-      }
-    }
-  }
-  return false;
+  createHash();
+  return myHashObjects.count(theObj) != 0;
 }
 
-ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty) const
+ObjectPtr Model_AttributeRefList::object(const int theIndex, const bool theWithEmpty)
 {
-  std::shared_ptr<Model_Document> aDoc =
-    std::dynamic_pointer_cast<Model_Document>(owner()->document());
-  if (aDoc) {
-    int anIndex = -1;
-    TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
-    for (TDF_ListIteratorOfLabelList aLIter(myRef->List()); aLIter.More(); aLIter.Next()) {
-      if (theWithEmpty || (!aLIter.Value().IsNull() && !aLIter.Value().IsRoot()))
-        anIndex++;
-      if (anIndex == theIndex) {
-        return iteratedObject(aLIter, anExtIter, aDoc);
-      }
-      if (aLIter.Value() == myRef->Label()) {
-        anExtIter.Next();
-        anExtIter.Next();
-      }
-    }
+  createHash();
+  std::map<int, ObjectPtr>::iterator aFind;
+  if (theWithEmpty) {
+    aFind = myHashIndex.find(theIndex);
+    if (aFind == myHashIndex.end())
+      return ObjectPtr();
+  } else {
+    aFind = myHashIndexNoEmpty.find(theIndex);
+    if (aFind == myHashIndexNoEmpty.end())
+      return ObjectPtr();
   }
-  return ObjectPtr();
+  return aFind->second;
 }
 
 void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew)
@@ -273,6 +240,7 @@ void Model_AttributeRefList::substitute(const ObjectPtr& theCurrent, const Objec
         aNewLab = aCurrentLab.Root(); // root means null object
       }
       // do the substitution
+      eraseHash();
       ADD_BACK_REF(theNew);
       if (myRef->InsertAfter(aNewLab, aCurrentLab)) {
         myRef->Remove(aCurrentLab);
@@ -295,6 +263,7 @@ void Model_AttributeRefList::exchange(const ObjectPtr& theObject1, const ObjectP
         std::shared_ptr<Model_Data> aData2 =
           std::dynamic_pointer_cast<Model_Data>(theObject2->data());
         if (aData2.get() && aData2->isValid()) {
+          eraseHash();
           TDF_Label aLab2 = aData2->label().Father();
           // do the substitution: use the temporary label, as usually in exchange
           TDF_Label aTmpLab = aLab1.Root();
@@ -321,6 +290,7 @@ void Model_AttributeRefList::removeLast()
   if (aDoc && !myRef->IsEmpty()) {
     ObjectPtr anObj = aDoc->objects()->object(myRef->Last());
     if (anObj.get()) {
+      eraseHash();
       myRef->Remove(myRef->Last());
       REMOVE_BACK_REF(anObj);
       owner()->data()->sendAttributeUpdated(this);
@@ -333,7 +303,7 @@ void Model_AttributeRefList::remove(const std::set<int>& theIndices)
   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
       owner()->document());
   if (aDoc && !myRef->IsEmpty()) {
-    // collet labels that will be removed
+    // collect labels that will be removed
     TDF_LabelList aLabelsToRemove;
     TDF_ListIteratorOfLabelList aLabIter(myRef->List());
     for(int aCurrent = 0; aLabIter.More(); aLabIter.Next(), aCurrent++) {
@@ -348,6 +318,7 @@ void Model_AttributeRefList::remove(const std::set<int>& theIndices)
         REMOVE_BACK_REF(anObj);
       }
     }
+    eraseHash();
     if (!aLabelsToRemove.IsEmpty()) {
       owner()->data()->sendAttributeUpdated(this);
     }
@@ -369,4 +340,35 @@ void Model_AttributeRefList::reinit()
   if (!myLab.FindAttribute(TDataStd_ExtStringList::GetID(), myExtDocRef)) {
     myExtDocRef = TDataStd_ExtStringList::Set(myLab);
   }
+  eraseHash();
+}
+
+void Model_AttributeRefList::createHash()
+{
+  if (myHashUsed)
+    return;
+  std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
+    owner()->document());
+  if (aDoc) {
+    const TDF_LabelList& aList = myRef->List();
+    TDataStd_ListIteratorOfListOfExtendedString anExtIter(myExtDocRef->List());
+    for (TDF_ListIteratorOfLabelList aLIter(aList); aLIter.More(); aLIter.Next()) {
+      ObjectPtr anObj = iteratedObject(aLIter, anExtIter, aDoc);
+      myHashIndex[int(myHashIndex.size())] = anObj;
+      if (anObj.get()) {
+        myHashIndexNoEmpty[int(myHashIndexNoEmpty.size())] = anObj;
+        myHashObjects.insert(anObj);
+      }
+    }
+    myHashUsed = true;
+  }
+}
+
+
+void Model_AttributeRefList::eraseHash()
+{
+  myHashObjects.clear();
+  myHashIndex.clear();
+  myHashIndexNoEmpty.clear();
+  myHashUsed = false;
 }
index 1964c7d58d26b945bae3dfbd6a80a6a744b202b7..da674ff49f2b4a9d0c76693518ba9241574d2112 100644 (file)
 class Model_AttributeRefList : public ModelAPI_AttributeRefList
 {
   TDF_Label myLab; ///< the main label of this attribute
+
   Handle_TDataStd_ReferenceList myRef;  ///< references to the features labels
   /// pairs of doc ID and entries if reference is to external object, appends some in this list if
   /// something in myRef is empty
   Handle_TDataStd_ExtStringList myExtDocRef;
- public:
+  bool myHashUsed; ///< true if the hash stored is valid
+  std::set<ObjectPtr> myHashObjects; ///< hashed information: objects existing in the list
+  std::map<int, ObjectPtr> myHashIndex; ///< index to object in the list
+  std::map<int, ObjectPtr> myHashIndexNoEmpty; ///< index to not empty object in the list
+public:
   /// Appends the feature to the end of a list
   MODEL_EXPORT virtual void append(ObjectPtr theObject);
 
@@ -64,7 +69,7 @@ class Model_AttributeRefList : public ModelAPI_AttributeRefList
   /// Returns the list of features
   ///\param theIndex zero-based index in the list
   ///\param theWithEmpty if it is false, counts the not-empty referenced objects only
-  MODEL_EXPORT virtual ObjectPtr object(const int theIndex, const bool theWithEmpty = true) const;
+  MODEL_EXPORT virtual ObjectPtr object(const int theIndex, const bool theWithEmpty = true);
 
   /// Substitutes the feature by another one. Does nothing if such object is not found.
   /// Does not support the external documents objects yet.
@@ -94,6 +99,10 @@ class Model_AttributeRefList : public ModelAPI_AttributeRefList
   ObjectPtr iteratedObject(TDF_ListIteratorOfLabelList& theLIter,
     TDataStd_ListIteratorOfListOfExtendedString& theExtIter,
     std::shared_ptr<Model_Document> theDoc) const;
+  /// Creates the hash-objects containers (does nothing if hash is already correct)
+  void createHash();
+  /// Erases the hashed objects caused by complicated modifications in the list
+  void eraseHash();
 
   friend class Model_Data;
 };
index d75a917dec598ef7ea5b903a7d9e95e806e4866b..bfc4630579c80b776fda7551bca57a10c1307e16 100644 (file)
@@ -346,10 +346,10 @@ void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
     }
   } else {
     // trim: need to redisplay
-    if (myObject) {
+    /*if (myObject) {
       static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
       ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
-    }
+    }*/
   }
 }
 
index 78d8ba769dfce1ccaa620fe49605ab0f8b849990..27b2427b968674ba6bb44e4afc86a7575db66205 100644 (file)
@@ -64,7 +64,7 @@ class ModelAPI_AttributeRefList : public ModelAPI_Attribute
   /// Returns the referenced object by the zero-based index
   ///\param theIndex zero-based index in the list
   ///\param theWithEmpty if it is false, counts the not-empty referenced objects only
-  virtual ObjectPtr object(const int theIndex, const bool theWithEmpty = true) const = 0;
+  virtual ObjectPtr object(const int theIndex, const bool theWithEmpty = true) = 0;
 
   /// Substitutes the object by another one. Does nothing if such object is not found.
   virtual void substitute(const ObjectPtr& theCurrent, const ObjectPtr& theNew) = 0;
index 588d138bf17cb447927b3273fed0f6fdfbeb1988..e0f0f8c87af3842860ef28e52175cf52fa594e80 100644 (file)
@@ -35,9 +35,9 @@ void ModelAPI_Result::initAttributes()
 {
   // append the color attribute. It is empty, the attribute will be filled by a request
   DataPtr aData = data();
-  aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
-  aData->addAttribute(DEFLECTION_ID(), ModelAPI_AttributeDouble::typeId());
-  aData->addAttribute(TRANSPARENCY_ID(), ModelAPI_AttributeDouble::typeId());
+  aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId())->setIsArgument(false);
+  aData->addAttribute(DEFLECTION_ID(), ModelAPI_AttributeDouble::typeId())->setIsArgument(false);
+  aData->addAttribute(TRANSPARENCY_ID(), ModelAPI_AttributeDouble::typeId())->setIsArgument(false);
 }
 
 bool ModelAPI_Result::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)