Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgNbIterator.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file SgNbIterator.h
00003     Neighbor point iterators
00004 */
00005 //----------------------------------------------------------------------------
00006 
00007 #ifndef SG_NBITERATOR_H
00008 #define SG_NBITERATOR_H
00009 
00010 #include <algorithm>
00011 #include "SgPoint.h"
00012 
00013 //----------------------------------------------------------------------------
00014 
00015 /** Iterator over all 4 neighbor points.
00016     Iterates in sorted order.
00017     See also SgNbIterator for an iterator that filters points off board, but
00018     needs access to current board.
00019 
00020     Vertical and horizontal neighbors (a subset of SgNb8Iterator).
00021     @verbatim
00022        0
00023        |
00024     1 -p- 2    e.g. next[2] = +WE
00025        |
00026        3
00027     @endverbatim
00028 */
00029 class SgNb4Iterator
00030     : public SgArray<SgPoint,4>::Iterator
00031 {
00032 public:
00033     SgNb4Iterator(SgPoint p)
00034         : SgArray<SgPoint,4>::Iterator(s_precomp.m_nb[p])
00035     {
00036         SG_ASSERT(SgPointUtil::InBoardRange(p));
00037     }
00038 
00039 private:
00040     /** Precomputed neighbors. */
00041     struct Precomp
00042     {
00043         Precomp();
00044 
00045         SgArray<SgArray<SgPoint,4>,SG_MAXPOINT> m_nb;
00046     };
00047 
00048     static const Precomp s_precomp;
00049 
00050     /** Not implemented. */
00051     SgNb4Iterator(const SgNb4Iterator&);
00052 
00053     /** Not implemented. */
00054     SgNb4Iterator& operator=(const SgNb4Iterator&);
00055 };
00056 
00057 //----------------------------------------------------------------------------
00058 
00059 /** Iterator over all 4 diagonal neighbor points.
00060     Iterates in sorted order.
00061     Does not filter points off board, that would need access to current board
00062 
00063     Diagonal neighbors (a subset of SgNb8Iterator).
00064     @verbatim
00065     0     1
00066      \   /
00067        p       e.g. next[0] = -NS-WE
00068      /   \
00069     2     3
00070     @endverbatim
00071 */
00072 class SgNb4DiagIterator
00073 {
00074 public:
00075     SgNb4DiagIterator(SgPoint p)
00076         : m_next(0),
00077           m_p(p)
00078     {
00079         SG_ASSERT(SgPointUtil::InBoardRange(p));
00080     }
00081 
00082     /** Advance the state of the iteration to the next element. */
00083     void operator++()
00084     {
00085         SG_ASSERT(m_next < 4);
00086         ++m_next;
00087     }
00088 
00089     /** Return the value of the current element. */
00090     SgPoint operator*() const
00091     {
00092         return m_p + s_diag[m_next];
00093     }
00094 
00095     /** Return true if iteration is valid, otherwise false. */
00096     operator bool() const
00097     {
00098         return m_next < 4;
00099     }
00100 
00101 private:
00102     int m_next;
00103 
00104     SgPoint m_p;
00105 
00106     static const int s_diag[4];
00107 
00108     /** Not implemented. */
00109     SgNb4DiagIterator(const SgNb4DiagIterator&);
00110 
00111     /** Not implemented. */
00112     SgNb4DiagIterator& operator=(const SgNb4DiagIterator&);
00113 };
00114 
00115 //----------------------------------------------------------------------------
00116 
00117 /** Iterator over all 8 neighbor points.
00118     Iterates in sorted order.
00119     Does not filter points off board, that would need access to current board
00120 
00121     Produces the neighbors of a point in all eight compass
00122     directions, in numerically ascending order.
00123     Directions 1, 3, 4 and 6 are vertical/horizontal, other are diagonal.
00124     @verbatim
00125     0  1  2
00126      \ | /
00127     3 -p- 4    e.g. next[1] = -NS
00128      / | \
00129     5  6  7
00130     @endverbatim
00131 */
00132 class SgNb8Iterator
00133 {
00134 public:
00135     SgNb8Iterator(SgPoint p)
00136         : m_next(0),
00137           m_p(p)
00138     {
00139         SG_ASSERT(SgPointUtil::InBoardRange(p));
00140     }
00141 
00142     /** Advance the state of the iteration to the next element. */
00143     void operator++()
00144     {
00145         SG_ASSERT(m_next < 8);
00146         ++m_next;
00147     }
00148 
00149     /** Return the value of the current element. */
00150     SgPoint operator*() const
00151     {
00152         return m_p + s_nb8[m_next];
00153     }
00154 
00155     /** Return true if iteration is valid, otherwise false. */
00156     operator bool() const
00157     {
00158         return m_next < 8;
00159     }
00160 
00161     static int Direction(int i)
00162     {
00163         SG_ASSERTRANGE(i, 0, 7);
00164         return s_nb8[i];
00165     }
00166 
00167 private:
00168     int m_next;
00169 
00170     SgPoint m_p;
00171 
00172     static const int s_nb8[8];
00173 
00174     /** Not implemented. */
00175     SgNb8Iterator(const SgNb8Iterator&);
00176 
00177     /** Not implemented. */
00178     SgNb8Iterator& operator=(const SgNb8Iterator&);
00179 };
00180 
00181 //----------------------------------------------------------------------------
00182 
00183 #endif // SG_NBITERATOR_H


17 Jun 2010 Doxygen 1.4.7