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