Add structure for region iterator

It is not currently enabled as the exact semantics aren't really known,
but as we're likely to want something similar later then we shouldn't
throw the code away.
This commit is contained in:
Danny Robson 2012-06-14 18:29:09 +10:00
parent 23c9bf17e6
commit 9447910061
2 changed files with 87 additions and 3 deletions

View File

@ -26,9 +26,12 @@
#include <cmath>
#include <type_traits>
//-----------------------------------------------------------------------------
using namespace util;
//-----------------------------------------------------------------------------
template <typename T>
region<T>::region (T _x, T _y, size_type _w, size_type _h):
x (_x),
@ -49,6 +52,7 @@ region<T>::operator+= (const vector<2> &rhs) {
}
//-----------------------------------------------------------------------------
template <typename T>
typename region<T>::size_type
region<T>::area (void) const
@ -68,6 +72,7 @@ region<T>::empty (void) const
{ return almost_equal (area (), 0); }
//-----------------------------------------------------------------------------
template <typename T>
point<2>
region<T>::base (void) const {
@ -85,6 +90,7 @@ region<T>::centre (void) const {
}
//-----------------------------------------------------------------------------
template <typename T>
bool
region<T>::includes (const point<2> &p) const {
@ -117,6 +123,7 @@ region<T>::overlaps (const region<T> &rhs) const {
}
//-----------------------------------------------------------------------------
template<typename T>
region<T>
region<T>::overlap (const region<T> &rhs) const {
@ -135,15 +142,17 @@ region<T>::overlap (const region<T> &rhs) const {
}
//-----------------------------------------------------------------------------
template <typename T>
bool
region<T>::operator ==(const region& rhs) const
region<T>::operator== (const region& rhs) const
{ return almost_equal (x, rhs.x) &&
almost_equal (y, rhs.y) &&
almost_equal (w, rhs.w) &&
almost_equal (h, rhs.h); }
//-----------------------------------------------------------------------------
template <typename T>
void
region<T>::sanity (void) const {
@ -152,6 +161,60 @@ region<T>::sanity (void) const {
}
//-----------------------------------------------------------------------------
// The desired iterator semantics have been difficult to nail down; is it
// edge-inclusive, left-bottom inclusive, purely exclusive, integral only?
// The code has been left here because it was a little annoying to write and
// we're likely to need it again some day.
#if 0
template <typename T>
typename region<T>::iterator&
region<T>::iterator::operator++ (void) {
if (++x > static_cast<T> (w)) {
x = a;
++y;
}
return *this;
}
template <typename T>
typename region<T>::iterator&
region<T>::iterator::operator* (void) {
return *this;
}
template <typename T>
bool
region<T>::iterator::operator== (const iterator &rhs) const {
return almost_equal (rhs.x, x) && almost_equal (rhs.y, y);
}
template <typename T>
bool
region<T>::iterator::operator!= (const iterator &rhs) const {
return !(*this == rhs);
}
template <typename T>
typename region<T>::iterator
region<T>::begin (void) {
return { x, y, x, w, h };
}
template <typename T>
typename region<T>::iterator
region<T>::end (void) {
return { x, y + sign_cast<T> (h) + 1, x, w, h };
}
#endif
namespace util {
template <>
void region<double>::sanity (void) const {
@ -166,6 +229,7 @@ namespace util {
}
//-----------------------------------------------------------------------------
template <typename T>
std::ostream&
util::operator<< (std::ostream &os, const region<T> &rhs) {
@ -174,6 +238,7 @@ util::operator<< (std::ostream &os, const region<T> &rhs) {
}
//-----------------------------------------------------------------------------
namespace util {
template struct region<int32_t>;
template struct region<int64_t>;
@ -181,6 +246,9 @@ namespace util {
template struct region<uint64_t>;
template struct region<double>;
template std::ostream& operator<< (std::ostream&, const region<unsigned int>&);
template std::ostream& operator<< (std::ostream&, const region<double>&);
template std::ostream& operator<< (std::ostream&, const region< int32_t>&);
template std::ostream& operator<< (std::ostream&, const region< int64_t>&);
template std::ostream& operator<< (std::ostream&, const region<uint32_t>&);
template std::ostream& operator<< (std::ostream&, const region<uint64_t>&);
template std::ostream& operator<< (std::ostream&, const region< double>&);
}

View File

@ -59,6 +59,22 @@ namespace util {
{ return !(*this == rhs); }
void sanity (void) const;
#if 0
struct iterator {
T x, y;
T a;
size_type w, h;
iterator& operator++ (void);
iterator& operator* (void);
bool operator== (const iterator&) const;
bool operator!= (const iterator&) const;
};
iterator begin (void);
iterator end (void);
#endif
};