00001
00002
00003
00004
00005
00006
00007 #include "ReorderingStack.h"
00008 #include <vector>
00009
00010 namespace Moses
00011 {
00012 size_t ReorderingStack::hash() const
00013 {
00014 std::size_t ret = boost::hash_range(m_stack.begin(), m_stack.end());
00015 return ret;
00016 }
00017
00018 bool ReorderingStack::operator==(const ReorderingStack& o) const
00019 {
00020 const ReorderingStack& other = static_cast<const ReorderingStack&>(o);
00021 return m_stack == other.m_stack;
00022 }
00023
00024
00025 int ReorderingStack::ShiftReduce(Range input_span)
00026 {
00027 int distance;
00028
00029
00030 if(m_stack.empty()) {
00031 m_stack.push_back(input_span);
00032 return input_span.GetStartPos() + 1;
00033 }
00034
00035
00036 Range prev_span = m_stack.back();
00037
00038
00039 if(input_span.GetStartPos() > prev_span.GetStartPos()) {
00040 distance = input_span.GetStartPos() - prev_span.GetEndPos();
00041 } else {
00042 distance = input_span.GetEndPos() - prev_span.GetStartPos();
00043 }
00044
00045 if(distance == 1) {
00046 m_stack.pop_back();
00047 Range new_span(prev_span.GetStartPos(), input_span.GetEndPos());
00048 Reduce(new_span);
00049 } else if(distance == -1) {
00050 m_stack.pop_back();
00051 Range new_span(input_span.GetStartPos(), prev_span.GetEndPos());
00052 Reduce(new_span);
00053 } else {
00054 m_stack.push_back(input_span);
00055 }
00056
00057 return distance;
00058 }
00059
00060
00061 void ReorderingStack::Reduce(Range current)
00062 {
00063 bool cont_loop = true;
00064
00065 while (cont_loop && m_stack.size() > 0) {
00066
00067 Range previous = m_stack.back();
00068
00069 if(current.GetStartPos() - previous.GetEndPos() == 1) {
00070 m_stack.pop_back();
00071 Range t(previous.GetStartPos(), current.GetEndPos());
00072 current = t;
00073 } else if(previous.GetStartPos() - current.GetEndPos() == 1) {
00074 m_stack.pop_back();
00075 Range t(current.GetStartPos(), previous.GetEndPos());
00076 current = t;
00077 } else {
00078 cont_loop=false;
00079 }
00080 }
00081
00082
00083 m_stack.push_back(current);
00084 }
00085
00086 }
00087