Skip to content

Commit

Permalink
BUG: Fix strict weak ordering in itkIPLFileNameList descend compare
Browse files Browse the repository at this point in the history
- Changed type of ascend and descend compare method from int to bool
- Fixed subtraction is not comparison for both ascend and descend comparator
- Fixed strict weak odering for descend comparator which was found using libc++ hardening mode in the debug setting
- Changed name of ascend and descend comparator from qsort to remove confussion as qsort outputs -1,0,1 vs sort 0,1
  • Loading branch information
ctaylo41 authored and dzenanz committed Jan 9, 2025
1 parent 43bdc37 commit 78f14df
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions Modules/IO/IPL/src/itkIPLFileNameList.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,31 @@ namespace itk
struct IPLFileSortInfo_ascend_compare : public std::greater<IPLFileSortInfo *>
{
private:
int
qsort_IPLFileSortInfo_ascend_compar(IPLFileSortInfo * item1, IPLFileSortInfo * item2)
bool
sort_IPLFileSortInfo_ascend_compare(IPLFileSortInfo * item1, IPLFileSortInfo * item2)
{
const int ImageNoDiff = item1->GetImageNumber() - item2->GetImageNumber();

if (ImageNoDiff < 0)
if (item1->GetImageNumber() < item2->GetImageNumber())
{
return true;
}
if (ImageNoDiff > 0)
if (item1->GetImageNumber() > item2->GetImageNumber())
{
return false;
}
const int echoNumDiff = item1->GetEchoNumber() - item2->GetEchoNumber();
if (echoNumDiff < 0)
if (item1->GetEchoNumber() < item2->GetEchoNumber())
{
return true;
}
if (echoNumDiff > 0)
if (item1->GetEchoNumber() > item2->GetEchoNumber())
{
return false;
}
const float sliceGap = item1->GetSliceLocation() - item2->GetSliceLocation();
if (sliceGap < 0.0)
if (item1->GetSliceLocation() < item2->GetSliceLocation())
{
return true;
}
if (sliceGap > 0.0)
if (item1->GetSliceLocation() > item2->GetSliceLocation())
{
return false;
}
Expand All @@ -63,52 +60,48 @@ struct IPLFileSortInfo_ascend_compare : public std::greater<IPLFileSortInfo *>
bool
operator()(IPLFileSortInfo * item1, IPLFileSortInfo * item2)
{
return qsort_IPLFileSortInfo_ascend_compar(item1, item2);
return sort_IPLFileSortInfo_ascend_compare(item1, item2);
}
};

struct IPLFileSortInfo_descend_compare : public std::greater<IPLFileSortInfo *>
{
private:
int
qsort_IPLFileSortInfo_descend_compar(IPLFileSortInfo * item1, IPLFileSortInfo * item2)
bool
sort_IPLFileSortInfo_descend_compare(IPLFileSortInfo * item1, IPLFileSortInfo * item2)
{
const int ImageNoDiff = item1->GetImageNumber() - item2->GetImageNumber();

if (ImageNoDiff < 0)
if (item1->GetImageNumber() < item2->GetImageNumber())
{
return false;
}
if (ImageNoDiff > 0)
if (item1->GetImageNumber() > item2->GetImageNumber())
{
return true;
}
const int echoNumDiff = item1->GetEchoNumber() - item2->GetEchoNumber();
if (echoNumDiff < 0)
if (item1->GetEchoNumber() < item2->GetEchoNumber())
{
return false;
}
if (echoNumDiff > 0)
if (item1->GetEchoNumber() > item2->GetEchoNumber())
{
return true;
}
const float sliceGap = item1->GetSliceLocation() - item2->GetSliceLocation();
if (sliceGap < 0.0)
if (item1->GetSliceLocation() < item2->GetSliceLocation())
{
return false;
}
if (sliceGap > 0.0)
if (item1->GetSliceLocation() > item2->GetSliceLocation())
{
return true;
}
return (item1->GetImageFileName() >= item2->GetImageFileName());
return (item1->GetImageFileName() > item2->GetImageFileName());
}

public:
bool
operator()(IPLFileSortInfo * item1, IPLFileSortInfo * item2)
{
return qsort_IPLFileSortInfo_descend_compar(item1, item2);
return sort_IPLFileSortInfo_descend_compare(item1, item2);
}
};

Expand Down

0 comments on commit 78f14df

Please sign in to comment.