Move range to the util namespace

This commit is contained in:
Danny Robson 2011-08-15 20:11:42 +10:00
parent 6e73c22d91
commit 306f852fc7
5 changed files with 57 additions and 53 deletions

View File

@ -30,9 +30,9 @@ using namespace std;
const ipv4::ip ipv4::ip::LOOPBACK (127, 0, 0, 1);
const ipv4::ip ipv4::ip::ANY ( 0, 0, 0, 0);
const range<ipv4::port> ipv4::WELL_KNOWN_PORT ( 0, 1023),
ipv4::REGISTERED_PORT ( 1024, 49151),
ipv4::PRIVATE_PORT (49152, 65535);
const util::range<ipv4::port> ipv4::WELL_KNOWN_PORT ( 0, 1023),
ipv4::REGISTERED_PORT ( 1024, 49151),
ipv4::PRIVATE_PORT (49152, 65535);
ipv4::ip::ip (uint32_t _integer):

2
ip.hpp
View File

@ -52,7 +52,7 @@ namespace ipv4 {
typedef uint16_t port;
typedef uint32_t mask;
extern const range<port> WELL_KNOWN_PORT,
extern const util::range<port> WELL_KNOWN_PORT,
REGISTERED_PORT,
PRIVATE_PORT;
}

View File

@ -25,9 +25,9 @@
#include <algorithm>
using namespace util;
using namespace maths;
matrix::matrix (size_t _rows, size_t _columns):
m_rows (_rows),
m_columns (_columns),

View File

@ -8,6 +8,7 @@
using namespace std;
using namespace util;
/*
@ -81,18 +82,20 @@ range<T>::rand (void) const {
}
template <>
bool
range<float>::operator ==(const range<float> &rhs) const
{ return almost_equal (min, rhs.min) &&
almost_equal (max, rhs.max); }
namespace util {
template <>
bool
range<float>::operator ==(const range<float> &rhs) const
{ 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); }
template <>
bool
range<double>::operator ==(const range<double> &rhs) const
{ return almost_equal (min, rhs.min) &&
almost_equal (max, rhs.max); }
}
template <typename T>

View File

@ -23,54 +23,55 @@
#include "json.hpp"
namespace util {
/**
* Represents a continuous range of values. Contains convenience functions
* and debugging checks.
*/
template <typename T>
struct range {
T min;
T max;
/**
* Represents a continuous range of values. Contains convenience functions
* and debugging checks.
*/
template <typename T>
struct range {
T min;
T max;
range (const json::node &node);
range (T _min, T _max);
range (const json::node &node);
range (T _min, T _max);
/// Check whether value falls within this range (inclusive)
bool contains (T val) const;
/// Check whether a range falls completely within (inclusive) this range
bool contains (const range <T> &r) const;
/// Check whether a range falls partially within (inclusive) this range
//bool includes (const range <T> &r) const;
/// Check whether value falls within this range (inclusive)
bool contains (T val) const;
/// Check whether a range falls completely within (inclusive) this range
bool contains (const range <T> &r) const;
/// Check whether a range falls partially within (inclusive) this range
//bool includes (const range <T> &r) const;
/// Return the closest number that falls within the range.
T clamp (T val) const;
/// Return the closest number that falls within the range.
T clamp (T val) const;
/// Normalise a number to [0, 1] within the range. Does not check bounds.
double normalise (T val) const;
/// Normalise a number to [0, 1] within the range. Does not check bounds.
double normalise (T val) const;
/// Return a pseudo-random uniformly distributed value within the range.
/// There are no statistical randomness guarantees whatsoever.
T rand (void) const;
/// Return a pseudo-random uniformly distributed value within the range.
/// There are no statistical randomness guarantees whatsoever.
T rand (void) const;
bool operator ==(const range<T>& rhs) const;
bool operator !=(const range<T>& rhs) const
{ return !(*this == rhs); }
bool operator ==(const range<T>& rhs) const;
bool operator !=(const range<T>& rhs) const
{ return !(*this == rhs); }
/// A range which is guaranteed to contain all elements type T
static const range <T> UNLIMITED;
/// A range which only contains elements between 0 and 1 inclusive
static const range <T> UNIT;
/// A range which is guaranteed to contain all elements type T
static const range <T> UNLIMITED;
/// A range which only contains elements between 0 and 1 inclusive
static const range <T> UNIT;
void sanity (void) const;
};
void sanity (void) const;
};
template <typename T>
std::ostream&
operator <<(std::ostream &os, const range<T> &rhs) {
os << '[' << rhs.min << ", " << rhs.max << ']';
return os;
template <typename T>
std::ostream&
operator <<(std::ostream &os, const range<T> &rhs) {
os << '[' << rhs.min << ", " << rhs.max << ']';
return os;
}
}
#endif