range: fully qualify util and std

This commit is contained in:
Danny Robson 2015-02-20 20:45:39 +11:00
parent f0ea25ed97
commit ab2996745e

103
range.cpp
View File

@ -29,45 +29,49 @@
#include <cstdint> #include <cstdint>
//-----------------------------------------------------------------------------
using namespace std;
using namespace util;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
range<T>::range (T _min, T _max): util::range<T>::range (T _min, T _max):
min (_min), min (_min),
max (_max) max (_max)
{ sanity (); } {
sanity ();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
bool bool
range<T>::contains (T val) const util::range<T>::contains (T val) const
{ return val >= min && val <= max; } {
return val >= min && val <= max;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
bool bool
range<T>::contains (const range <T> &r) const util::range<T>::contains (const range <T> &r) const
{ return r.min >= min && r.max <= max; } {
return r.min >= min && r.max <= max;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
void void
range<T>::sanity (void) const util::range<T>::sanity (void) const
{ CHECK (min <= max); } {
CHECK (min <= max);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
namespace util { namespace util {
template <> template <>
void void
range<double>::sanity (void) const { range<double>::sanity (void) const
{
if (std::isnan (min) || std::isnan (max)) if (std::isnan (min) || std::isnan (max))
return; return;
CHECK (min <= max); CHECK (min <= max);
@ -78,14 +82,17 @@ namespace util {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
T T
range<T>::clamp (T val) const util::range<T>::clamp (T val) const
{ return std::max (min, std::min (val, max)); } {
return std::max (min, std::min (val, max));
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
void void
range<T>::expand (T val) { util::range<T>::expand (T val)
{
// The arguments to min and max are such that expansion from initial NaN // The arguments to min and max are such that expansion from initial NaN
// values should change both min and max to be that value. // values should change both min and max to be that value.
min = std::min (val, min); min = std::min (val, min);
@ -95,8 +102,9 @@ range<T>::expand (T val) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
range<T>& util::range<T>&
range<T>::operator*= (T val) { util::range<T>::operator*= (T val)
{
min *= val; min *= val;
max *= val; max *= val;
@ -106,9 +114,10 @@ range<T>::operator*= (T val) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
range<T> util::range<T>
range<T>::operator* (T val) const { util::range<T>::operator* (T val) const
return range<T> (min * val, max * val); {
return util::range<T> (min * val, max * val);
} }
@ -116,14 +125,16 @@ range<T>::operator* (T val) const {
namespace util { namespace util {
template <> template <>
double double
range<double>::random (void) const { range<double>::random (void) const
{
double pos = ::rand () / (double)(RAND_MAX); double pos = ::rand () / (double)(RAND_MAX);
return (max - min) * pos + min; return (max - min) * pos + min;
} }
template <> template <>
float float
range<float>::random (void) const { range<float>::random (void) const
{
float pos = ::rand () / (float)(RAND_MAX); float pos = ::rand () / (float)(RAND_MAX);
return (max - min) * pos + min; return (max - min) * pos + min;
} }
@ -133,7 +144,8 @@ namespace util {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
T T
range<T>::random (void) const { util::range<T>::random (void) const
{
return min + (T)::rand () % (max - min); return min + (T)::rand () % (max - min);
} }
@ -143,44 +155,50 @@ namespace util {
template <> template <>
bool bool
range<float>::operator ==(const range<float> &rhs) const range<float>::operator ==(const range<float> &rhs) const
{ return almost_equal (min, rhs.min) && {
almost_equal (max, rhs.max); } return almost_equal (min, rhs.min) &&
almost_equal (max, rhs.max);
}
template <> template <>
bool bool
range<double>::operator ==(const range<double> &rhs) const range<double>::operator ==(const range<double> &rhs) const
{ return almost_equal (min, rhs.min) && {
almost_equal (max, rhs.max); } return almost_equal (min, rhs.min) &&
almost_equal (max, rhs.max);
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
bool bool
range<T>::operator ==(const range<T> &rhs) const util::range<T>::operator ==(const util::range<T> &rhs) const
{ return min == rhs.min && max == rhs.max; } {
return min == rhs.min && max == rhs.max;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
const range<T> const util::range<T>
range<T>::UNLIMITED (numeric_limits<T>::has_infinity ? -numeric_limits<T>::infinity () : util::range<T>::UNLIMITED (std::numeric_limits<T>::has_infinity ? -std::numeric_limits<T>::infinity () :
numeric_limits<T>::lowest (), std::numeric_limits<T>::lowest (),
numeric_limits<T>::has_infinity ? numeric_limits<T>::infinity () : std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity () :
numeric_limits<T>::max ()); std::numeric_limits<T>::max ());
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
const range<T> const util::range<T>
range<T>::MAX (numeric_limits<T>::lowest (), util::range<T>::MAX (std::numeric_limits<T>::lowest (),
numeric_limits<T>::max ()); std::numeric_limits<T>::max ());
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
const range<T> const util::range<T>
range<T>::UNIT (0.0, 1.0); util::range<T>::UNIT (0.0, 1.0);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -198,7 +216,8 @@ namespace util {
namespace json { namespace tree { namespace json { namespace tree {
template <> template <>
util::range<double> util::range<double>
io<util::range<double>>::deserialise (const json::tree::node &node) { io<util::range<double>>::deserialise (const json::tree::node &node)
{
if (node.is_string () && (node == "UNIT" || if (node.is_string () && (node == "UNIT" ||
node == "unit")) { node == "unit")) {
return util::range<double>::UNIT; return util::range<double>::UNIT;