/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.

    This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. 

    See the LICENSE and COPYING files for more details.
*/

#include "entropy.h"

namespace smartcomparator
{
	namespace filters
	{
		EntropyFilter::EntropyFilter(Matrices::FeatureVectorPtr cf, double precentage_to_remove,
				 bool remove_zeros)
			: FilterFeature(cf),
			  m_bRemoveZeros(remove_zeros),
			  m_dPrecentageToRemove(precentage_to_remove)
		{
			std::multimap<double, size_t> entropy2column;
			for(size_t i = 0; i < m_pColumnFeatures->size(); ++i)
			{
				double entropy = (*m_pColumnFeatures)[i].GetEntropy();
				if(entropy > 0)
					entropy2column.insert( std::make_pair(entropy, i));
				else
					if (m_bRemoveZeros)
						m_setRemoveIndices.insert(i);
			}

			size_t removed = 0;
			size_t to_remove = size_t(double(m_pColumnFeatures->size()) * m_dPrecentageToRemove);
			for(std::multimap<double, size_t>::reverse_iterator riter = entropy2column.rbegin();
				riter != entropy2column.rend()
				&& removed < to_remove; riter++, removed++)
			{
				m_setRemoveIndices.insert(riter->second);
			}

		}

		EntropyFilter::~EntropyFilter()
		{
			// no op?
		}

		void EntropyFilter::Filter(std::set<size_t> &out,const std::set<size_t> *nzind)
		{
			std::set_difference(nzind->begin(), nzind->end(),
								m_setRemoveIndices.begin(),
								m_setRemoveIndices.end(),
								std::inserter(out, out.end()));
		}
	}//namespace filters
}//namespace smartcomparator
