From 306f852fc746822f319dc56a1adbb696687a3743 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 15 Aug 2011 20:11:42 +1000 Subject: [PATCH] Move range to the util namespace --- ip.cpp.rl | 6 ++--- ip.hpp | 2 +- matrix.cpp | 2 +- range.cpp | 23 +++++++++------- range.hpp | 77 +++++++++++++++++++++++++++--------------------------- 5 files changed, 57 insertions(+), 53 deletions(-) diff --git a/ip.cpp.rl b/ip.cpp.rl index b0e0e696..8e8c343c 100644 --- a/ip.cpp.rl +++ b/ip.cpp.rl @@ -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::WELL_KNOWN_PORT ( 0, 1023), - ipv4::REGISTERED_PORT ( 1024, 49151), - ipv4::PRIVATE_PORT (49152, 65535); +const util::range ipv4::WELL_KNOWN_PORT ( 0, 1023), + ipv4::REGISTERED_PORT ( 1024, 49151), + ipv4::PRIVATE_PORT (49152, 65535); ipv4::ip::ip (uint32_t _integer): diff --git a/ip.hpp b/ip.hpp index feecf3f7..f6788d49 100644 --- a/ip.hpp +++ b/ip.hpp @@ -52,7 +52,7 @@ namespace ipv4 { typedef uint16_t port; typedef uint32_t mask; - extern const range WELL_KNOWN_PORT, + extern const util::range WELL_KNOWN_PORT, REGISTERED_PORT, PRIVATE_PORT; } diff --git a/matrix.cpp b/matrix.cpp index 0a51a4fb..4d8bd0b4 100644 --- a/matrix.cpp +++ b/matrix.cpp @@ -25,9 +25,9 @@ #include +using namespace util; using namespace maths; - matrix::matrix (size_t _rows, size_t _columns): m_rows (_rows), m_columns (_columns), diff --git a/range.cpp b/range.cpp index 035dbea1..c8b9e969 100644 --- a/range.cpp +++ b/range.cpp @@ -8,6 +8,7 @@ using namespace std; +using namespace util; /* @@ -81,18 +82,20 @@ range::rand (void) const { } -template <> -bool -range::operator ==(const range &rhs) const - { return almost_equal (min, rhs.min) && - almost_equal (max, rhs.max); } +namespace util { + template <> + bool + range::operator ==(const range &rhs) const + { return almost_equal (min, rhs.min) && + almost_equal (max, rhs.max); } -template <> -bool -range::operator ==(const range &rhs) const - { return almost_equal (min, rhs.min) && - almost_equal (max, rhs.max); } + template <> + bool + range::operator ==(const range &rhs) const + { return almost_equal (min, rhs.min) && + almost_equal (max, rhs.max); } +} template diff --git a/range.hpp b/range.hpp index baf882a1..204fdf1d 100644 --- a/range.hpp +++ b/range.hpp @@ -23,54 +23,55 @@ #include "json.hpp" +namespace util { + /** + * Represents a continuous range of values. Contains convenience functions + * and debugging checks. + */ + template + struct range { + T min; + T max; -/** - * Represents a continuous range of values. Contains convenience functions - * and debugging checks. - */ -template -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 &r) const; + /// Check whether a range falls partially within (inclusive) this range + //bool includes (const range &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 &r) const; - /// Check whether a range falls partially within (inclusive) this range - //bool includes (const range &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& rhs) const; + bool operator !=(const range& rhs) const + { return !(*this == rhs); } - bool operator ==(const range& rhs) const; - bool operator !=(const range& rhs) const - { return !(*this == rhs); } + /// A range which is guaranteed to contain all elements type T + static const range UNLIMITED; + /// A range which only contains elements between 0 and 1 inclusive + static const range UNIT; - /// A range which is guaranteed to contain all elements type T - static const range UNLIMITED; - /// A range which only contains elements between 0 and 1 inclusive - static const range UNIT; - - void sanity (void) const; -}; + void sanity (void) const; + }; -template -std::ostream& -operator <<(std::ostream &os, const range &rhs) { - os << '[' << rhs.min << ", " << rhs.max << ']'; - return os; + template + std::ostream& + operator <<(std::ostream &os, const range &rhs) { + os << '[' << rhs.min << ", " << rhs.max << ']'; + return os; + } } #endif