00001 // $Id$ 00002 // vim:tabstop=2 00003 /*********************************************************************** 00004 Moses - factored phrase-based language decoder 00005 Copyright (C) 2010 Hieu Hoang 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 #pragma once 00022 00023 #include <boost/ptr_container/ptr_vector.hpp> 00024 #include "InputType.h" 00025 #include "ChartCell.h" 00026 #include "Range.h" 00027 #include "InputPath.h" 00028 00029 namespace Moses 00030 { 00031 class InputType; 00032 class ChartManager; 00033 class ChartParser; 00034 00035 class ChartCellCollectionBase 00036 { 00037 public: 00038 template <class Factory> ChartCellCollectionBase(const InputType &input, 00039 const Factory &factory, 00040 const ChartParser &parser) 00041 :m_cells(input.GetSize()) { 00042 00043 size_t size = input.GetSize(); 00044 for (size_t startPos = 0; startPos < size; ++startPos) { 00045 std::vector<ChartCellBase*> &inner = m_cells[startPos]; 00046 inner.reserve(size - startPos); 00047 for (size_t endPos = startPos; endPos < size; ++endPos) { 00048 inner.push_back(factory(startPos, endPos)); 00049 } 00050 /* Hack: ChartCellLabel shouldn't need to know its span, but the parser 00051 * gets it from there :-(. The span is actually stored as a reference, 00052 * which needs to point somewhere, so I have it refer to the ChartCell. 00053 */ 00054 const Range &range = inner[0]->GetCoverage(); 00055 00056 m_source.push_back(new ChartCellLabel(range, input.GetWord(startPos))); 00057 } 00058 } 00059 00060 virtual ~ChartCellCollectionBase(); 00061 00062 00063 const ChartCellBase &GetBase(const Range &coverage) const { 00064 return *m_cells[coverage.GetStartPos()][coverage.GetEndPos() - coverage.GetStartPos()]; 00065 } 00066 00067 ChartCellBase &MutableBase(const Range &coverage) { 00068 return *m_cells[coverage.GetStartPos()][coverage.GetEndPos() - coverage.GetStartPos()]; 00069 } 00070 00071 00072 const ChartCellLabel &GetSourceWordLabel(size_t at) const { 00073 return m_source[at]; 00074 } 00075 00076 private: 00077 std::vector<std::vector<ChartCellBase*> > m_cells; 00078 00079 boost::ptr_vector<ChartCellLabel> m_source; 00080 00081 }; 00082 00085 class ChartCellCollection : public ChartCellCollectionBase 00086 { 00087 public: 00088 ChartCellCollection(const InputType &input, ChartManager &manager); 00089 00091 ChartCell &Get(const Range &coverage) { 00092 return static_cast<ChartCell&>(MutableBase(coverage)); 00093 } 00094 00096 const ChartCell &Get(const Range &coverage) const { 00097 return static_cast<const ChartCell&>(GetBase(coverage)); 00098 } 00099 }; 00100 00101 } 00102