changeset 5379:b31c73bc7cb6

Toolbox : more set functions
author Alain Mazy <am@osimis.io>
date Thu, 31 Aug 2023 15:20:41 +0200
parents d857c6210c50
children 97004471a5c5
files OrthancFramework/Sources/SerializationToolbox.cpp OrthancFramework/Sources/SerializationToolbox.h OrthancFramework/Sources/Toolbox.h OrthancFramework/UnitTestsSources/ToolboxTests.cpp
diffstat 4 files changed, 105 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/SerializationToolbox.cpp	Thu Aug 24 16:35:55 2023 +0200
+++ b/OrthancFramework/Sources/SerializationToolbox.cpp	Thu Aug 31 15:20:41 2023 +0200
@@ -178,23 +178,44 @@
         value[field.c_str()].type() != Json::arrayValue)
     {
       throw OrthancException(ErrorCode_BadFileFormat,
-                             "List of strings expected in field: " + field);
+                            "List of strings expected in field: " + field);
     }
 
     const Json::Value& arr = value[field.c_str()];
 
-    target.resize(arr.size());
+    try
+    {
+      ReadArrayOfStrings(target, arr);
+    }
+    catch (OrthancException& ex)
+    {  // more detailed error
+      throw OrthancException(ErrorCode_BadFileFormat,
+                              "List of strings expected in field: " + field);
+    }
+  }
+
 
-    for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++)
+  void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target,
+                                                const Json::Value& array)
+  {
+    if (array.type() != Json::arrayValue)
     {
-      if (arr[i].type() != Json::stringValue)
+      throw OrthancException(ErrorCode_BadFileFormat,
+                             "List of strings expected");
+    }
+
+    target.resize(array.size());
+
+    for (Json::Value::ArrayIndex i = 0; i < array.size(); i++)
+    {
+      if (array[i].type() != Json::stringValue)
       {
         throw OrthancException(ErrorCode_BadFileFormat,
-                               "List of strings expected in field: " + field);
+                               "List of strings expected");
       }
       else
       {
-        target[i] = arr[i].asString();
+        target[i] = array[i].asString();
       }
     }
   }
@@ -230,6 +251,20 @@
   }
 
 
+  void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target,
+                                              const Json::Value& value)
+  {
+    std::vector<std::string> tmp;
+    ReadArrayOfStrings(tmp, value);
+
+    target.clear();
+    for (size_t i = 0; i < tmp.size(); i++)
+    {
+      target.insert(tmp[i]);
+    }
+  }
+
+
   void SerializationToolbox::ReadSetOfTags(std::set<DicomTag>& target,
                                            const Json::Value& value,
                                            const std::string& field)
--- a/OrthancFramework/Sources/SerializationToolbox.h	Thu Aug 24 16:35:55 2023 +0200
+++ b/OrthancFramework/Sources/SerializationToolbox.h	Thu Aug 31 15:20:41 2023 +0200
@@ -63,6 +63,9 @@
                                    const Json::Value& value,
                                    const std::string& field);
 
+    static void ReadArrayOfStrings(std::vector<std::string>& target,
+                                   const Json::Value& value);
+
     static void ReadListOfStrings(std::list<std::string>& target,
                                   const Json::Value& value,
                                   const std::string& field);
@@ -71,6 +74,9 @@
                                  const Json::Value& value,
                                  const std::string& field);
 
+    static void ReadSetOfStrings(std::set<std::string>& target,
+                                 const Json::Value& value);
+
     static void ReadSetOfTags(std::set<DicomTag>& target,
                               const Json::Value& value,
                               const std::string& field);
--- a/OrthancFramework/Sources/Toolbox.h	Thu Aug 24 16:35:55 2023 +0200
+++ b/OrthancFramework/Sources/Toolbox.h	Thu Aug 31 15:20:41 2023 +0200
@@ -259,6 +259,22 @@
       }
     }
 
+    // returns true if all element of 'needles' are found in 'haystack'
+    template <typename T> static void GetIntersection(std::set<T>& target, const std::set<T>& a, const std::set<T>& b)
+    {
+      target.clear();
+
+      for (typename std::set<T>::const_iterator it = a.begin();
+            it != a.end(); ++it)
+      {
+        if (b.count(*it) > 0)
+        {
+          target.insert(*it);
+        }
+      }
+    }
+
+
 #if ORTHANC_ENABLE_PUGIXML == 1
     static void JsonToXml(std::string& target,
                           const Json::Value& source,
--- a/OrthancFramework/UnitTestsSources/ToolboxTests.cpp	Thu Aug 24 16:35:55 2023 +0200
+++ b/OrthancFramework/UnitTestsSources/ToolboxTests.cpp	Thu Aug 31 15:20:41 2023 +0200
@@ -280,6 +280,48 @@
   }
 }
 
+TEST(Toolbox, GetSetIntersection)
+{
+  {
+    std::set<int> target;
+    std::set<int> a;
+    std::set<int> b;
+
+    Toolbox::GetIntersection(target, a, b);
+    ASSERT_EQ(0u, target.size());
+  }
+
+  {
+    std::set<int> target;
+    std::set<int> a;
+    std::set<int> b;
+
+    a.insert(1);
+    b.insert(1);
+
+    Toolbox::GetIntersection(target, a, b);
+    ASSERT_EQ(1u, target.size());
+    ASSERT_EQ(1u, target.count(1));
+  }
+
+  {
+    std::set<int> target;
+    std::set<int> a;
+    std::set<int> b;
+
+    a.insert(1);
+    a.insert(2);
+    b.insert(2);
+
+    Toolbox::GetIntersection(target, a, b);
+    ASSERT_EQ(1u, target.size());
+    ASSERT_EQ(0u, target.count(1));
+    ASSERT_EQ(1u, target.count(2));
+  }
+
+}
+
+
 TEST(Toolbox, JoinStrings)
 {
   {