From 9447910061751e2c369c7aa9e3a1fb6d99505f4e Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 14 Jun 2012 18:29:09 +1000 Subject: [PATCH] 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. --- region.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++--- region.hpp | 16 ++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/region.cpp b/region.cpp index 4d43f88d..dbe7d76d 100644 --- a/region.cpp +++ b/region.cpp @@ -26,9 +26,12 @@ #include #include + +//----------------------------------------------------------------------------- using namespace util; +//----------------------------------------------------------------------------- template region::region (T _x, T _y, size_type _w, size_type _h): x (_x), @@ -49,6 +52,7 @@ region::operator+= (const vector<2> &rhs) { } +//----------------------------------------------------------------------------- template typename region::size_type region::area (void) const @@ -68,6 +72,7 @@ region::empty (void) const { return almost_equal (area (), 0); } +//----------------------------------------------------------------------------- template point<2> region::base (void) const { @@ -85,6 +90,7 @@ region::centre (void) const { } +//----------------------------------------------------------------------------- template bool region::includes (const point<2> &p) const { @@ -117,6 +123,7 @@ region::overlaps (const region &rhs) const { } +//----------------------------------------------------------------------------- template region region::overlap (const region &rhs) const { @@ -135,15 +142,17 @@ region::overlap (const region &rhs) const { } +//----------------------------------------------------------------------------- template bool -region::operator ==(const region& rhs) const +region::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 void region::sanity (void) const { @@ -152,6 +161,60 @@ region::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 region::iterator& +region::iterator::operator++ (void) { + if (++x > static_cast (w)) { + x = a; + ++y; + } + + return *this; +} + + +template +typename region::iterator& +region::iterator::operator* (void) { + return *this; +} + + +template +bool +region::iterator::operator== (const iterator &rhs) const { + return almost_equal (rhs.x, x) && almost_equal (rhs.y, y); +} + + +template +bool +region::iterator::operator!= (const iterator &rhs) const { + return !(*this == rhs); +} + + +template +typename region::iterator +region::begin (void) { + return { x, y, x, w, h }; +} + + +template +typename region::iterator +region::end (void) { + return { x, y + sign_cast (h) + 1, x, w, h }; +} +#endif + + namespace util { template <> void region::sanity (void) const { @@ -166,6 +229,7 @@ namespace util { } +//----------------------------------------------------------------------------- template std::ostream& util::operator<< (std::ostream &os, const region &rhs) { @@ -174,6 +238,7 @@ util::operator<< (std::ostream &os, const region &rhs) { } +//----------------------------------------------------------------------------- namespace util { template struct region; template struct region; @@ -181,6 +246,9 @@ namespace util { template struct region; template struct region; - template std::ostream& operator<< (std::ostream&, const region&); - template std::ostream& operator<< (std::ostream&, const region&); + 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&); + template std::ostream& operator<< (std::ostream&, const region&); + template std::ostream& operator<< (std::ostream&, const region< double>&); } diff --git a/region.hpp b/region.hpp index 398d034b..9c0bdabf 100644 --- a/region.hpp +++ b/region.hpp @@ -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 };