Skip to content

Commit

Permalink
Merge pull request #11199 from rouault/gdal_minmax_element
Browse files Browse the repository at this point in the history
Add gdal_minmax_element.hpp public header, that can also be vendored, to find the min/max elements in a buffer
  • Loading branch information
rouault authored Nov 10, 2024
2 parents d107444 + 9164d9d commit 0d8eb30
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
1 change: 1 addition & 0 deletions gcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ target_public_header(
gdal_typetraits.h
gdal_adbc.h
gdal_minmax_element.hpp
gdal_priv_templates.hpp # Required by gdal_minmax_element.hpp
)

set(GDAL_DATA_FILES
Expand Down
31 changes: 8 additions & 23 deletions gcore/gdal_minmax_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,6 @@
#endif

#include "gdal_priv_templates.hpp"
#if GDAL_VERSION < GDAL_COMPUTE_VERSION(3, 10, 0)
// For vendoring in other applications
namespace GDAL_MINMAXELT_NS
{
template <class T> inline bool GDALIsValueExactAs(double dfValue)
{
return GDALIsValueInRange<T>(dfValue) &&
static_cast<double>(static_cast<T>(dfValue)) == dfValue;
}

template <> inline bool GDALIsValueExactAs<float>(double dfValue)
{
return std::isnan(dfValue) ||
(GDALIsValueInRange<float>(dfValue) &&
static_cast<double>(static_cast<float>(dfValue)) == dfValue);
}

template <> inline bool GDALIsValueExactAs<double>(double)
{
return true;
}
} // namespace GDAL_MINMAXELT_NS
#endif

namespace GDAL_MINMAXELT_NS
{
Expand Down Expand Up @@ -992,6 +969,7 @@ size_t extremum_element(const void *buffer, size_t nElts, GDALDataType eDT,
{
switch (eDT)
{
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 7, 0)
case GDT_Int8:
{
using T = int8_t;
Expand All @@ -1000,6 +978,7 @@ size_t extremum_element(const void *buffer, size_t nElts, GDALDataType eDT,
static_cast<const T *>(buffer), nElts, bHasNoData,
bHasNoData ? static_cast<T>(dfNoDataValue) : 0);
}
#endif
case GDT_Byte:
{
using T = uint8_t;
Expand Down Expand Up @@ -1040,6 +1019,7 @@ size_t extremum_element(const void *buffer, size_t nElts, GDALDataType eDT,
static_cast<const T *>(buffer), nElts, bHasNoData,
bHasNoData ? static_cast<T>(dfNoDataValue) : 0);
}
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
case GDT_Int64:
{
using T = int64_t;
Expand All @@ -1056,6 +1036,7 @@ size_t extremum_element(const void *buffer, size_t nElts, GDALDataType eDT,
static_cast<const T *>(buffer), nElts, bHasNoData,
bHasNoData ? static_cast<T>(dfNoDataValue) : 0);
}
#endif
case GDT_Float32:
{
using T = float;
Expand Down Expand Up @@ -1333,6 +1314,7 @@ inline std::pair<size_t, size_t> minmax_element(const void *buffer,
{
switch (eDT)
{
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 7, 0)
case GDT_Int8:
{
using T = int8_t;
Expand All @@ -1341,6 +1323,7 @@ inline std::pair<size_t, size_t> minmax_element(const void *buffer,
static_cast<const T *>(buffer), nElts, bHasNoData,
bHasNoData ? static_cast<T>(dfNoDataValue) : 0);
}
#endif
case GDT_Byte:
{
using T = uint8_t;
Expand Down Expand Up @@ -1381,6 +1364,7 @@ inline std::pair<size_t, size_t> minmax_element(const void *buffer,
static_cast<const T *>(buffer), nElts, bHasNoData,
bHasNoData ? static_cast<T>(dfNoDataValue) : 0);
}
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
case GDT_Int64:
{
using T = int64_t;
Expand All @@ -1397,6 +1381,7 @@ inline std::pair<size_t, size_t> minmax_element(const void *buffer,
static_cast<const T *>(buffer), nElts, bHasNoData,
bHasNoData ? static_cast<T>(dfNoDataValue) : 0);
}
#endif
case GDT_Float32:
{
using T = float;
Expand Down

0 comments on commit 0d8eb30

Please sign in to comment.