00001 // $Id$ 00002 00003 /*********************************************************************** 00004 Moses - factored phrase-based language decoder 00005 Copyright (C) 2006 University of Edinburgh 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2.1 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 ***********************************************************************/ 00021 00022 #ifndef moses_SquareMatrix_h 00023 #define moses_SquareMatrix_h 00024 00025 #include <iostream> 00026 #include "TypeDef.h" 00027 #include "Util.h" 00028 #include "Bitmap.h" 00029 00030 namespace Moses 00031 { 00032 00034 class SquareMatrix 00035 { 00036 friend std::ostream& operator<<(std::ostream &out, const SquareMatrix &matrix); 00037 protected: 00038 const size_t m_size; 00039 float *m_array; 00041 SquareMatrix(); // not implemented 00042 SquareMatrix(const SquareMatrix ©); // not implemented 00043 00044 public: 00045 SquareMatrix(size_t size) 00046 :m_size(size) { 00047 m_array = (float*) malloc(sizeof(float) * size * size); 00048 } 00049 ~SquareMatrix() { 00050 free(m_array); 00051 } 00052 00053 // set upper triangle 00054 void InitTriangle(float val); 00055 00057 inline size_t GetSize() const { 00058 return m_size; 00059 } 00061 inline float GetScore(size_t startPos, size_t endPos) const { 00062 return m_array[startPos * m_size + endPos]; 00063 } 00065 inline void SetScore(size_t startPos, size_t endPos, float value) { 00066 m_array[startPos * m_size + endPos] = value; 00067 } 00068 float CalcEstimatedScore( Bitmap const& ) const; 00069 float CalcEstimatedScore( Bitmap const&, size_t startPos, size_t endPos ) const; 00070 00071 TO_STRING(); 00072 }; 00073 00074 inline std::ostream& operator<<(std::ostream &out, const SquareMatrix &matrix) 00075 { 00076 for (size_t endPos = 0 ; endPos < matrix.GetSize() ; endPos++) { 00077 for (size_t startPos = 0 ; startPos < matrix.GetSize() ; startPos++) 00078 out << matrix.GetScore(startPos, endPos) << " "; 00079 out << std::endl; 00080 } 00081 00082 return out; 00083 } 00084 00085 } 00086 #endif