LexicalReorderingState* HierarchicalReorderingForwardState::Expand(const TranslationOption& topt, Scores& scores) const {
  const LexicalReorderingConfiguration::ModelType modelType = m_configuration.GetModelType();
  const WordsRange currWordsRange = topt.GetSourceWordsRange();
  // keep track of the current coverage ourselves so we don't need the hypothesis
  WordsBitmap coverage = m_coverage;
  coverage.SetValue(currWordsRange.GetStartPos(), currWordsRange.GetEndPos(), true);
  
  ReorderingType reoType;
  
  if (m_first) {
      ClearScores(scores);
  } else {
    if (modelType == LexicalReorderingConfiguration::MSD) {
      reoType = GetOrientationTypeMSD(currWordsRange, coverage);
    } else if (modelType == LexicalReorderingConfiguration::MSLR) {
      reoType = GetOrientationTypeMSLR(currWordsRange, coverage);
    } else if (modelType == LexicalReorderingConfiguration::Monotonic) {
      reoType = GetOrientationTypeMonotonic(currWordsRange, coverage);
    } else {
      reoType = GetOrientationTypeLeftRight(currWordsRange, coverage);
    }
  
    CopyScores(scores, topt, reoType);
  }
  
  return new HierarchicalReorderingForwardState(this, topt);
}
LexicalReorderingState* PhraseBasedReorderingState::Expand(const TranslationOption& topt, Scores& scores) const
{
  ReorderingType reoType;
  const WordsRange currWordsRange = topt.GetSourceWordsRange();
  const LexicalReorderingConfiguration::ModelType modelType = m_configuration.GetModelType();

  if (m_direction == LexicalReorderingConfiguration::Forward && m_first) {
    ClearScores(scores);
  } else {
    if (!m_first || m_useFirstBackwardScore) {
      if (modelType == LexicalReorderingConfiguration::MSD) {
        reoType = GetOrientationTypeMSD(currWordsRange);
      } else if (modelType == LexicalReorderingConfiguration::MSLR) {
        reoType = GetOrientationTypeMSLR(currWordsRange);
      } else if (modelType == LexicalReorderingConfiguration::Monotonic) {
        reoType = GetOrientationTypeMonotonic(currWordsRange);
      } else {
        reoType = GetOrientationTypeLeftRight(currWordsRange);
      }
      CopyScores(scores, topt, reoType);
    }
  }

  return new PhraseBasedReorderingState(this, topt);
}
LRState*
HReorderingBackwardState::
Expand(const TranslationOption& topt, const InputType& input,
       ScoreComponentCollection*  scores) const
{
  HReorderingBackwardState* nextState;
  nextState = new HReorderingBackwardState(this, topt, m_reoStack);
  Range swrange = topt.GetSourceWordsRange();
  int reoDistance = nextState->m_reoStack.ShiftReduce(swrange);
  ReorderingType reoType = m_configuration.GetOrientation(reoDistance);
  CopyScores(scores, topt, input, reoType);
  return nextState;
}
LRState*
HReorderingForwardState::
Expand(TranslationOption const& topt, InputType const& input,
       ScoreComponentCollection* scores) const
{
  const Range cur = topt.GetSourceWordsRange();
  // keep track of the current coverage ourselves so we don't need the hypothesis
  Bitmap cov(m_coverage, cur);
  if (!m_first) {
    LRModel::ReorderingType reoType;
    reoType = m_configuration.GetOrientation(m_prevRange,cur,cov);
    CopyScores(scores, topt, input, reoType);
  }
  return new HReorderingForwardState(this, topt);
}
LRState*
PhraseBasedReorderingState::
Expand(const TranslationOption& topt, const InputType& input,
       ScoreComponentCollection* scores) const
{
  // const LRModel::ModelType modelType = m_configuration.GetModelType();

  if ((m_direction != LRModel::Forward && m_useFirstBackwardScore) || !m_first) {
    LRModel const& lrmodel = m_configuration;
    Range const cur = topt.GetSourceWordsRange();
    LRModel::ReorderingType reoType = (m_first ? lrmodel.GetOrientation(cur)
                                       : lrmodel.GetOrientation(m_prevRange,cur));
    CopyScores(scores, topt, input, reoType);
  }
  return new PhraseBasedReorderingState(this, topt);
}
void PhraseBasedReorderingState::Expand(const ManagerBase &mgr,
    const LexicalReordering &ff, const Hypothesis &hypo, size_t phraseTableInd,
    Scores &scores, FFState &state) const
{
  if ((m_direction != LRModel::Forward) || !m_first) {
    LRModel const& lrmodel = m_configuration;
    Range const &cur = hypo.GetInputPath().range;
    LRModel::ReorderingType reoType = (
        m_first ?
            lrmodel.GetOrientation(cur) :
            lrmodel.GetOrientation(prevPath->range, cur));
    CopyScores(mgr.system, scores, hypo.GetTargetPhrase(), reoType);
  }

  PhraseBasedReorderingState &stateCast =
      static_cast<PhraseBasedReorderingState&>(state);
  stateCast.Init(this, hypo.GetTargetPhrase(), hypo.GetInputPath(), false,
      NULL);
}
LexicalReorderingState* HierarchicalReorderingBackwardState::Expand(const TranslationOption& topt, Scores& scores) const {

  HierarchicalReorderingBackwardState* nextState = new HierarchicalReorderingBackwardState(this, topt, m_reoStack);
  ReorderingType reoType;
  const LexicalReorderingConfiguration::ModelType modelType = m_configuration.GetModelType();
  
  int reoDistance = nextState->m_reoStack.ShiftReduce(topt.GetSourceWordsRange());

  if (modelType == LexicalReorderingConfiguration::MSD) {
    reoType = GetOrientationTypeMSD(reoDistance);
  } else if (modelType == LexicalReorderingConfiguration::MSLR) {
    reoType = GetOrientationTypeMSLR(reoDistance);
  } else if (modelType == LexicalReorderingConfiguration::LeftRight) {
    reoType = GetOrientationTypeLeftRight(reoDistance);  
  } else {
    reoType = GetOrientationTypeMonotonic(reoDistance);
  }

  CopyScores(scores, topt, reoType);
  return nextState;
}