Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgPointArray.h

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


17 Jun 2010 Doxygen 1.4.7