# HG changeset patch # User Sebastien Jodogne # Date 1625497426 -7200 # Node ID e17fdc43ef6ce18d99913e97ffdf88f077c3ef1c # Parent b51c08bd5c381f87337004222794b93f137e64f6 optimized version of DicomPath::IsMatch() diff -r b51c08bd5c38 -r e17fdc43ef6c OrthancFramework/Sources/DicomFormat/DicomPath.cpp --- a/OrthancFramework/Sources/DicomFormat/DicomPath.cpp Mon Jul 05 16:12:10 2021 +0200 +++ b/OrthancFramework/Sources/DicomFormat/DicomPath.cpp Mon Jul 05 17:03:46 2021 +0200 @@ -372,4 +372,42 @@ } } } + + + bool DicomPath::IsMatch(const DicomPath& pattern, + const std::vector& prefixTags, + const std::vector& prefixIndexes, + const DicomTag& finalTag) + { + if (prefixTags.size() != prefixIndexes.size()) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + + if (prefixTags.size() < pattern.GetPrefixLength()) + { + return false; + } + else + { + for (size_t i = 0; i < pattern.GetPrefixLength(); i++) + { + if (prefixTags[i] != pattern.GetPrefixTag(i) || + (!pattern.IsPrefixUniversal(i) && + prefixIndexes[i] != pattern.GetPrefixIndex(i))) + { + return false; + } + } + + if (prefixTags.size() == pattern.GetPrefixLength()) + { + return (finalTag == pattern.GetFinalTag()); + } + else + { + return (prefixTags[pattern.GetPrefixLength()] == pattern.GetFinalTag()); + } + } + } } diff -r b51c08bd5c38 -r e17fdc43ef6c OrthancFramework/Sources/DicomFormat/DicomPath.h --- a/OrthancFramework/Sources/DicomFormat/DicomPath.h Mon Jul 05 16:12:10 2021 +0200 +++ b/OrthancFramework/Sources/DicomFormat/DicomPath.h Mon Jul 05 17:03:46 2021 +0200 @@ -130,5 +130,10 @@ static bool IsMatch(const DicomPath& pattern, const DicomPath& path); + + static bool IsMatch(const DicomPath& pattern, + const std::vector& prefixTags, + const std::vector& prefixIndexes, + const DicomTag& finalTag); }; } diff -r b51c08bd5c38 -r e17fdc43ef6c OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp --- a/OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp Mon Jul 05 16:12:10 2021 +0200 +++ b/OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp Mon Jul 05 17:03:46 2021 +0200 @@ -2261,6 +2261,31 @@ } +static bool MyIsMatch(const DicomPath& a, + const DicomPath& b) +{ + bool expected = DicomPath::IsMatch(a, b); + + std::vector prefixTags; + std::vector prefixIndexes; + + for (size_t i = 0; i < b.GetPrefixLength(); i++) + { + prefixTags.push_back(b.GetPrefixTag(i)); + prefixIndexes.push_back(b.GetPrefixIndex(i)); + } + + if (expected == DicomPath::IsMatch(a, prefixTags, prefixIndexes, b.GetFinalTag())) + { + return expected; + } + else + { + throw OrthancException(ErrorCode_InternalError); + } +} + + TEST(DicomModification, DicomPath) { // Those are samples inspired by those from "man dcmodify" @@ -2366,30 +2391,30 @@ ASSERT_THROW(DicomPath::Parse("(0010,0010)0].PatientID"), OrthancException); ASSERT_THROW(DicomPath::Parse("(0010,0010)[-1].PatientID"), OrthancException); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)"), - DicomPath::Parse("(0010,0010)"))); - ASSERT_FALSE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)"), - DicomPath::Parse("(0010,0020)"))); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); - ASSERT_FALSE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), - DicomPath::Parse("(0010,0010)"))); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[*].(0010,0020)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); - ASSERT_FALSE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[2].(0010,0020)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); - ASSERT_THROW(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), - DicomPath::Parse("(0010,0010)[*].(0010,0020)")), OrthancException); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[*].(0010,0020)[*].(0010,0030)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); - ASSERT_FALSE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)[3].(0010,0030)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); - ASSERT_FALSE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[2].(0010,0020)[2].(0010,0030)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)"), + DicomPath::Parse("(0010,0010)"))); + ASSERT_FALSE(MyIsMatch(DicomPath::Parse("(0010,0010)"), + DicomPath::Parse("(0010,0020)"))); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); + ASSERT_FALSE(MyIsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), + DicomPath::Parse("(0010,0010)"))); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)[*].(0010,0020)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); + ASSERT_FALSE(MyIsMatch(DicomPath::Parse("(0010,0010)[2].(0010,0020)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); + ASSERT_THROW(MyIsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), + DicomPath::Parse("(0010,0010)[*].(0010,0020)")), OrthancException); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)[*].(0010,0020)[*].(0010,0030)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); + ASSERT_FALSE(MyIsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)[3].(0010,0030)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); + ASSERT_FALSE(MyIsMatch(DicomPath::Parse("(0010,0010)[2].(0010,0020)[2].(0010,0030)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); }