0028103: Foundation Classes - extend NCollection_Map to check whether two given maps...
authorabk <abk@opencascade.com>
Mon, 14 Nov 2016 15:08:44 +0000 (18:08 +0300)
committerapn <apn@opencascade.com>
Fri, 18 Nov 2016 12:19:40 +0000 (15:19 +0300)
Mehtod HasIntersection added in NCollection_Map to check whether two given maps contain at least one same item.
'Draw' command and test to check the method were created.

src/NCollection/NCollection_Map.hxx
src/QANCollection/QANCollection_Test.cxx
tests/collections/grids.list [new file with mode: 0755]
tests/collections/n/begin [new file with mode: 0644]
tests/collections/n/end [new file with mode: 0644]
tests/collections/n/map [new file with mode: 0644]

index 16dde63..866d0cb 100644 (file)
@@ -383,6 +383,28 @@ class NCollection_Map : public NCollection_BaseMap
     return anOldExtent != Extent();
   }
 
+  //! Returns true if this and theMap have common elements.
+  Standard_Boolean HasIntersection (const NCollection_Map& theMap) const
+  {
+    const NCollection_Map* aMap1 = this;
+    const NCollection_Map* aMap2 = &theMap;
+    if (theMap.Size() < Size())
+    {
+      aMap1 = &theMap;
+      aMap2 = this;
+    }
+
+    for (NCollection_Map::Iterator aIt(*aMap1); aIt.More(); aIt.Next())
+    {
+      if (aMap2->Contains(aIt.Value()))
+      {
+        return Standard_True;
+      }
+    }
+
+    return Standard_False;
+  }
+
   //! Sets this Map to be the result of intersection (aka multiplication, common, boolean AND) operation between two given Maps.
   //! The new Map contains only the values that are contained in both map operands.
   //! All previous content of this Map is cleared.
index 19655dd..f06d11d 100644 (file)
@@ -299,47 +299,71 @@ static void TestSequence (QANCollection_SequenceFunc& theS)
 
 // ===================== Test methods of Map type =============================
 ////////////////////////////////void TestMap  (QANCollection_Map& theM)
-static void TestMap  (QANCollection_MapFunc& theM)
+static void TestMap(QANCollection_MapFunc& theM, Draw_Interpretor& theDI)
 {
-  // Extent
-  Standard_Integer iExt=theM.Extent();
-  Standard_Integer i;
-
-  printf ("Info: testing Map(l=%d)\n", iExt);
-  theM.Statistics(cout);
-  // Resize
-  theM.ReSize(8);
-  theM.Statistics(cout);
-  cout.flush();
-  // Constructor
-  ////////////////////////////////QANCollection_Map aM;
-  QANCollection_MapFunc aM;
-  // Add
-  Key1Type aKey;
-  for (i=0; i<8; i++)
-  {
-    Random (aKey);
-    aM.Add (aKey);
-  }
-  // Contains, Remove
-  if (!aM.Contains(aKey))
   {
-    printf("Error   : map says that it does not contain its key ");
-    PrintItem(aKey);
+    // Extent
+    Standard_Integer iExt=theM.Extent();
+    Standard_Integer i;
+
+    printf ("Info: testing Map(l=%d)\n", iExt);
+    theM.Statistics(cout);
+    // Resize
+    theM.ReSize(8);
+    theM.Statistics(cout);
+    cout.flush();
+    // Constructor
+    ////////////////////////////////QANCollection_Map aM;
+    QANCollection_MapFunc aM;
+    // Add
+    Key1Type aKey;
+    for (i=0; i<8; i++)
+    {
+      Random (aKey);
+      aM.Add (aKey);
+    }
+    // Contains, Remove
+    if (!aM.Contains(aKey))
+    {
+      theDI << "Error: map says that it does not contain its key " << aKey;
+    }
+    else
+    {
+      aM.Remove(aKey);
+      cout << "      successfully removed item, l=%d\n" << aM.Size() << "\n";
+    }
+    // Copy constructor (including operator=)
+    ////////////////////////////////QANCollection_Map aM2 = QANCollection_Map(aM);
+    QANCollection_MapFunc aM2 = QANCollection_MapFunc(aM);
+    // Assign
+    AssignCollection (aM2,theM);
+
+    // Clear
+    aM.Clear();
   }
-  else
+
+  // Check method 'HasIntersection'.
   {
-    aM.Remove(aKey);
-    printf("      successfully removed item, l=%d\n", aM.Size());
-  }
-  // Copy constructor (including operator=)
-  ////////////////////////////////QANCollection_Map aM2 = QANCollection_Map(aM);
-  QANCollection_MapFunc aM2 = QANCollection_MapFunc(aM);
-  // Assign
-  AssignCollection (aM2,theM);
+    QANCollection_MapFunc aM1, aM2, aM3;
 
-  // Clear
-  aM.Clear();
+    aM1.Add(6);
+    aM1.Add(8);
+    aM1.Add(10);
+
+    aM2.Add(4);
+    aM2.Add(8);
+    aM2.Add(16);
+
+    aM3.Add(1);
+    aM3.Add(2);
+    aM3.Add(3);
+
+    if (!aM1.HasIntersection(aM2) || !aM2.HasIntersection(aM1) ||
+         aM1.HasIntersection(aM3) ||  aM3.HasIntersection(aM1))
+    {
+      theDI << "Error: method 'HasIntersection' failed.";
+    }
+  }
 }
 
 // ===================== Test methods of DataMap type =========================
@@ -671,7 +695,7 @@ static Standard_Integer QANColTestMap(Draw_Interpretor& di, Standard_Integer arg
     return 1;
   }
   QANCollection_MapFunc aMap;
-  TestMap(aMap);
+  TestMap(aMap, di);
   return 0;
 }
 
diff --git a/tests/collections/grids.list b/tests/collections/grids.list
new file mode 100755 (executable)
index 0000000..f717d11
--- /dev/null
@@ -0,0 +1 @@
+001 n
diff --git a/tests/collections/n/begin b/tests/collections/n/begin
new file mode 100644 (file)
index 0000000..f012867
--- /dev/null
@@ -0,0 +1 @@
+pload QAcommands
diff --git a/tests/collections/n/end b/tests/collections/n/end
new file mode 100644 (file)
index 0000000..73b3acd
--- /dev/null
@@ -0,0 +1 @@
+puts "TEST COMPLETED"
diff --git a/tests/collections/n/map b/tests/collections/n/map
new file mode 100644 (file)
index 0000000..517a44f
--- /dev/null
@@ -0,0 +1,3 @@
+puts "Check NCollection_Map functionality"
+
+QANColTestMap