00001 //---------------------------------------------------------------------------- 00002 /** @file SgPointArray.h 00003 Array indexed by points. */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef SG_POINTARRAY_H 00007 #define SG_POINTARRAY_H 00008 00009 #include <iomanip> 00010 #include <iostream> 00011 #include <sstream> 00012 #include "SgArray.h" 00013 #include "SgPoint.h" 00014 00015 //---------------------------------------------------------------------------- 00016 00017 /** An array of SG_MAXPOINT values of type T, indexed by SgPoint. 00018 Also enforces that all elements are initialized in the constructor, 00019 either with T(0), if T can be constructed in such a way or be providing 00020 an initialization value. */ 00021 template<class T> 00022 class SgPointArray 00023 : public SgArray<T,SG_MAXPOINT> 00024 { 00025 public: 00026 /** Constructor; values are initialized by default value. */ 00027 SgPointArray(); 00028 00029 /** Constructor; values are initialized by init value. */ 00030 SgPointArray(const T& value); 00031 00032 /** Constructor; initialized as copy of other point array. */ 00033 SgPointArray(const SgPointArray& pointArray); 00034 }; 00035 00036 template<class T> 00037 inline SgPointArray<T>::SgPointArray() 00038 { 00039 } 00040 00041 template<class T> 00042 inline SgPointArray<T>::SgPointArray(const T& value) 00043 : SgArray<T,SG_MAXPOINT>(value) 00044 { 00045 } 00046 00047 template<class T> 00048 inline SgPointArray<T>::SgPointArray(const SgPointArray& pointArray) 00049 : SgArray<T,SG_MAXPOINT>(pointArray) 00050 { 00051 } 00052 00053 //---------------------------------------------------------------------------- 00054 00055 /** Write a point array. 00056 Computes the maximum string representation length of each element in the 00057 array to write out aligned columns with minimum space in between. */ 00058 template<typename T> 00059 class SgWritePointArray 00060 { 00061 public: 00062 SgWritePointArray(const SgPointArray<T>& array, SgGrid boardSize) 00063 : m_boardSize(boardSize), 00064 m_array(array) 00065 { 00066 } 00067 00068 std::ostream& Write(std::ostream& out) const; 00069 00070 private: 00071 SgGrid m_boardSize; 00072 00073 const SgPointArray<T>& m_array; 00074 }; 00075 00076 template<typename T> 00077 std::ostream& SgWritePointArray<T>::Write(std::ostream& out) const 00078 { 00079 std::ostringstream buffer; 00080 int maxLength = 0; 00081 for (SgGrid row = 1; row <= m_boardSize; ++row) 00082 for (SgGrid col = 1; col <= m_boardSize; ++col) 00083 { 00084 buffer.str(""); 00085 buffer << m_array[SgPointUtil::Pt(col, row)]; 00086 int length = static_cast<int>(buffer.str().length()); 00087 maxLength = std::max(maxLength, length); 00088 } 00089 for (SgGrid row = m_boardSize; row >= 1; --row) 00090 { 00091 for (SgGrid col = 1; col <= m_boardSize; ++col) 00092 { 00093 SgPoint point = SgPointUtil::Pt(col, row); 00094 out << std::setw(maxLength) << m_array[point]; 00095 if (col < m_boardSize) 00096 out << ' '; 00097 } 00098 out << '\n'; 00099 } 00100 return out; 00101 } 00102 00103 /** @relatesalso SgWritePointArray */ 00104 template<typename T> 00105 std::ostream& operator<<(std::ostream& out, 00106 const SgWritePointArray<T>& write) 00107 { 00108 return write.Write(out); 00109 } 00110 00111 //---------------------------------------------------------------------------- 00112 00113 /** Write a float point array. 00114 Enhanced version of SgWritePointArray for float or double types. 00115 Allows to specify some formatting options for floating point numbers. */ 00116 template<typename FLOAT> 00117 class SgWritePointArrayFloat 00118 { 00119 public: 00120 SgWritePointArrayFloat(const SgPointArray<FLOAT>& array, SgGrid boardSize, 00121 bool fixed, int precision) 00122 : m_fixed(fixed), 00123 m_precision(precision), 00124 m_boardSize(boardSize), 00125 m_array(array) 00126 { 00127 } 00128 00129 std::ostream& Write(std::ostream& out) const; 00130 00131 private: 00132 bool m_fixed; 00133 00134 int m_precision; 00135 00136 SgGrid m_boardSize; 00137 00138 const SgPointArray<FLOAT>& m_array; 00139 }; 00140 00141 template<typename FLOAT> 00142 std::ostream& SgWritePointArrayFloat<FLOAT>::Write(std::ostream& out) const 00143 { 00144 SgPointArray<std::string> stringArray; 00145 std::ostringstream buffer; 00146 if (m_fixed) 00147 buffer << std::fixed; 00148 buffer << std::setprecision(m_precision); 00149 for (SgGrid row = 1; row <= m_boardSize; ++row) 00150 for (SgGrid col = 1; col <= m_boardSize; ++col) 00151 { 00152 buffer.str(""); 00153 SgPoint p = SgPointUtil::Pt(col, row); 00154 buffer << m_array[p]; 00155 stringArray[p] = buffer.str(); 00156 } 00157 out << SgWritePointArray<std::string>(stringArray, m_boardSize); 00158 return out; 00159 } 00160 00161 /** @relatesalso SgWritePointArrayFloat */ 00162 template<typename FLOAT> 00163 std::ostream& operator<<(std::ostream& out, 00164 const SgWritePointArrayFloat<FLOAT>& write) 00165 { 00166 return write.Write(out); 00167 } 00168 00169 //---------------------------------------------------------------------------- 00170 00171 #endif // SG_POINTARRAY_H