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::LOOPBACK (127, 0, 0, 1);
const ipv4::ip ipv4::ip::ANY ( 0, 0, 0, 0); const ipv4::ip ipv4::ip::ANY ( 0, 0, 0, 0);
const range<ipv4::port> ipv4::WELL_KNOWN_PORT ( 0, 1023), const util::range<ipv4::port> ipv4::WELL_KNOWN_PORT ( 0, 1023),
ipv4::REGISTERED_PORT ( 1024, 49151), ipv4::REGISTERED_PORT ( 1024, 49151),
ipv4::PRIVATE_PORT (49152, 65535); ipv4::PRIVATE_PORT (49152, 65535);
ipv4::ip::ip (uint32_t _integer): ipv4::ip::ip (uint32_t _integer):

2
ip.hpp
View File

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

View File

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

View File

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

View File

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