<commit_before>/*
 * Stack.cpp
 *
 *  Created on: 24 Oct 2015
 *      Author: hieu
 */

#include "Stack.h"
#include "Hypothesis.h"
#include "../Scores.h"

Stack::Stack() {
	// TODO Auto-generated constructor stub

}

Stack::~Stack() {
	// TODO Auto-generated destructor stub
}

StackAdd Stack::Add(const Hypothesis *hypo)
{
  std::pair<iterator, bool> addRet = m_hypos.insert(hypo);
  if (addRet.second) {
    // equiv hypo doesn't exists
	return StackAdd(true, NULL);
  }
  else {
	  const Hypothesis *hypoExisting = *addRet.first;
	  if (hypo->GetScores().GetTotalScore() > hypoExisting->GetScores().GetTotalScore()) {
		  // incoming hypo is better than the one we have
		  m_hypos.erase(addRet.first);

		  // re-add. It better go in
		  std::pair<iterator, bool> addRet = m_hypos.insert(hypo);
		  assert(addRet.second);

		  return StackAdd(true, hypoExisting);
	  }
	  else {
		  // already storing the best hypo. discard incoming hypo
		  return StackAdd(false, hypoExisting);
	  }
  }
}

std::vector<const Hypothesis*> Stack::GetBestHypos(size_t num) const
{
	std::vector<const Hypothesis*> ret(m_hypos.begin(), m_hypos.end());

	if (ret.size() > num) {
		NTH_ELEMENT3(ret.begin(),
				ret.begin() + num,
				ret.end());
		ret.resize(num);
	}

    return ret;
}

std::vector<const Hypothesis*> Stack::GetSortedHypos() const
{
	std::vector<const Hypothesis*> ret(m_hypos.begin(), m_hypos.end());
	std::sort(ret.begin(), ret.end(), HypothesisScoreOrderer());
	return ret;
}<commit_msg>stack pruning<commit_after>/*
 * Stack.cpp
 *
 *  Created on: 24 Oct 2015
 *      Author: hieu
 */
#include <algorithm>
#include <boost/foreach.hpp>
#include "Stack.h"
#include "Hypothesis.h"
#include "../Scores.h"

using namespace std;

Stack::Stack() {
	// TODO Auto-generated constructor stub

}

Stack::~Stack() {
	// TODO Auto-generated destructor stub
}

StackAdd Stack::Add(const Hypothesis *hypo)
{
  std::pair<iterator, bool> addRet = m_hypos.insert(hypo);
  if (addRet.second) {
    // equiv hypo doesn't exists
	return StackAdd(true, NULL);
  }
  else {
	  const Hypothesis *hypoExisting = *addRet.first;
	  if (hypo->GetScores().GetTotalScore() > hypoExisting->GetScores().GetTotalScore()) {
		  // incoming hypo is better than the one we have
		  m_hypos.erase(addRet.first);

		  // re-add. It better go in
		  std::pair<iterator, bool> addRet = m_hypos.insert(hypo);
		  assert(addRet.second);

		  return StackAdd(true, hypoExisting);
	  }
	  else {
		  // already storing the best hypo. discard incoming hypo
		  return StackAdd(false, hypoExisting);
	  }
  }
}

std::vector<const Hypothesis*> Stack::GetBestHypos(size_t num) const
{
	std::vector<const Hypothesis*> ret(m_hypos.begin(), m_hypos.end());

	if (ret.size() > num) {

		NTH_ELEMENT4(ret.begin(),
				ret.begin() + num,
				ret.end(),
				HypothesisScoreOrderer());
		/*
		std::sort(ret.begin(),
				ret.begin() + num,
				HypothesisScoreOrderer());
		cerr << "hypos:" << endl;
		BOOST_FOREACH(const Hypothesis *hypo, ret) {
			cerr << *hypo << endl;
		}
		*/

		ret.resize(num);
	}

    return ret;
}

std::vector<const Hypothesis*> Stack::GetSortedHypos() const
{
	std::vector<const Hypothesis*> ret(m_hypos.begin(), m_hypos.end());
	std::sort(ret.begin(), ret.end(), HypothesisScoreOrderer());
	return ret;
}
{'fraction_non_alphanumeric': 0.16031697653154525, 'fraction_numerical': 0.004266991770801585, 'mean_word_length': 3.7842565597667637, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7239359', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1189, 'n_words': 257}