Added templated isInRange, replacing the char-only one. (#458)

This commit is contained in:
Branimir Karadžić
2026-10-12 20:58:22 -07:00
committed by GitHub
parent a9a71f8ef4
commit d4e6ed3cf4
4 changed files with 27 additions and 5 deletions

View File

@@ -251,6 +251,10 @@ namespace bx
template<typename Ty>
constexpr Ty clamp(const Ty& _a, const TypeIdentityType<Ty>& _min, const TypeIdentityType<Ty>& _max);
/// Returns true if value `_a` is in inclusive range [`_min`, `_max`].
template<typename Ty>
constexpr bool isInRange(const Ty& _a, const TypeIdentityType<Ty>& _min, const TypeIdentityType<Ty>& _max);
/// Returns true if value `_a` is power of 2.
template<typename Ty>
constexpr bool isPowerOf2(Ty _a);

View File

@@ -216,6 +216,12 @@ namespace bx
return max(min(_a, _max), _min);
}
template<typename Ty>
inline constexpr bool isInRange(const Ty& _a, const TypeIdentityType<Ty>& _min, const TypeIdentityType<Ty>& _max)
{
return _min <= _a && _a <= _max;
}
template<typename Ty>
inline constexpr bool isPowerOf2(Ty _a)
{

View File

@@ -450,11 +450,6 @@ namespace bx
return StringView(m_ptr, m_len);
}
inline BX_CONSTEXPR_FUNC bool isInRange(char _ch, char _from, char _to)
{
return unsigned(_ch - _from) <= unsigned(_to-_from);
}
inline BX_CONSTEXPR_FUNC bool isSpace(char _ch)
{
return ' ' == _ch // Space.

View File

@@ -36,6 +36,23 @@ TEST_CASE("gcd", "[bx]")
REQUIRE(9 == bx::gcd(18u, 81u) );
}
TEST_CASE("isInRange", "[bx]")
{
REQUIRE( bx::isInRange(5, 0, 10) );
REQUIRE( bx::isInRange(0, 0, 10) );
REQUIRE( bx::isInRange(10, 0, 10) );
REQUIRE(!bx::isInRange(-1, 0, 10) );
REQUIRE(!bx::isInRange(11, 0, 10) );
REQUIRE( bx::isInRange(uint8_t(255), 0, 255) );
REQUIRE(!bx::isInRange(uint8_t(0), 1, 255) );
REQUIRE( bx::isInRange('m', 'a', 'z') );
REQUIRE(!bx::isInRange('M', 'a', 'z') );
REQUIRE( bx::isInRange(0.5f, 0.0f, 1.0f) );
REQUIRE(!bx::isInRange(1.5f, 0.0f, 1.0f) );
STATIC_REQUIRE(bx::isInRange(3, 1, 3) );
}
TEST_CASE("lcm", "[bx]")
{
REQUIRE(1157 == bx::lcm(13u, 89u) );