00001 /*********************************************************************** 00002 Moses - statistical machine translation system 00003 Copyright (C) 2006-2011 University of Edinburgh 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 ***********************************************************************/ 00019 00020 #pragma once 00021 00022 #include "ChartCellLabel.h" 00023 #include "NonTerminal.h" 00024 #include "moses/FactorCollection.h" 00025 00026 #include <boost/functional/hash.hpp> 00027 #include <boost/unordered_map.hpp> 00028 #include <boost/version.hpp> 00029 00030 namespace Moses 00031 { 00032 00033 class ChartHypothesisCollection; 00034 00037 class ChartCellLabelSet 00038 { 00039 private: 00040 00041 typedef std::vector<ChartCellLabel*> MapType; 00042 00043 public: 00044 typedef MapType::const_iterator const_iterator; 00045 typedef MapType::iterator iterator; 00046 00047 ChartCellLabelSet(const Range &coverage) 00048 : m_coverage(coverage) 00049 , m_map(FactorCollection::Instance().GetNumNonTerminals(), NULL) 00050 , m_size(0) { } 00051 00052 ~ChartCellLabelSet() { 00053 RemoveAllInColl(m_map); 00054 } 00055 00056 // TODO: skip empty elements when iterating, or deprecate this 00057 const_iterator begin() const { 00058 return m_map.begin(); 00059 } 00060 const_iterator end() const { 00061 return m_map.end(); 00062 } 00063 00064 iterator mutable_begin() { 00065 return m_map.begin(); 00066 } 00067 iterator mutable_end() { 00068 return m_map.end(); 00069 } 00070 00071 void AddWord(const Word &w) { 00072 size_t idx = w[0]->GetId(); 00073 if (! ChartCellExists(idx)) { 00074 m_size++; 00075 00076 00077 m_map[idx] = new ChartCellLabel(m_coverage, w); 00078 } 00079 } 00080 00081 // Stack is a HypoList or whatever the search algorithm uses. 00082 void AddConstituent(const Word &w, const HypoList *stack) { 00083 size_t idx = w[0]->GetId(); 00084 if (ChartCellExists(idx)) { 00085 ChartCellLabel::Stack & s = m_map[idx]->MutableStack(); 00086 s.cube = stack; 00087 } else { 00088 ChartCellLabel::Stack s; 00089 s.cube = stack; 00090 m_size++; 00091 m_map[idx] = new ChartCellLabel(m_coverage, w, s); 00092 } 00093 } 00094 00095 // grow vector if necessary 00096 bool ChartCellExists(size_t idx) { 00097 try { 00098 if (m_map.at(idx) != NULL) { 00099 return true; 00100 } 00101 } catch (const std::out_of_range& oor) { 00102 m_map.resize(FactorCollection::Instance().GetNumNonTerminals(), NULL); 00103 } 00104 return false; 00105 } 00106 00107 bool Empty() const { 00108 return m_size == 0; 00109 } 00110 00111 size_t GetSize() const { 00112 return m_size; 00113 } 00114 00115 const ChartCellLabel *Find(const Word &w) const { 00116 size_t idx = w[0]->GetId(); 00117 try { 00118 return m_map.at(idx); 00119 } catch (const std::out_of_range& oor) { 00120 return NULL; 00121 } 00122 } 00123 00124 const ChartCellLabel *Find(size_t idx) const { 00125 try { 00126 return m_map.at(idx); 00127 } catch (const std::out_of_range& oor) { 00128 return NULL; 00129 } 00130 } 00131 00132 ChartCellLabel::Stack &FindOrInsert(const Word &w) { 00133 size_t idx = w[0]->GetId(); 00134 if (! ChartCellExists(idx)) { 00135 m_size++; 00136 m_map[idx] = new ChartCellLabel(m_coverage, w); 00137 } 00138 return m_map[idx]->MutableStack(); 00139 } 00140 00141 private: 00142 const Range &m_coverage; 00143 MapType m_map; 00144 size_t m_size; 00145 }; 00146 00147 }