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