/*
    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 "../comparator_stdheaders.h"
#include "CosineFilterSimilarity.h"

namespace smartcomparator
{
	namespace simfunctions
	{
		CosineFilterSimilarity::CosineFilterSimilarity(Matrices::SuperMatrixPtr m, double percentInter, size_t minInter)
			: SimilarityFunction(m), 
			m_dPrecentIntersec(percentInter),
			m_stMinIntersec(minInter)

		{

		}

		CosineFilterSimilarity::~CosineFilterSimilarity(void)
		{
		}

		double CosineFilterSimilarity::CountSimilarity(Matrices::CompressedVector& v1, Matrices::CompressedVector& v2, 
			size_t , size_t , const std::set<size_t> &v1Ind, const std::set<size_t> &v2Ind)
		{
			double dot = 0;

			std::set<size_t> filteredIndices;

			std::set_union(v1Ind.begin(), v1Ind.end(), v2Ind.begin(), v2Ind.end(),
				std::insert_iterator< std::set<size_t> >(filteredIndices, filteredIndices.begin()));

			// tutaj filtrujemy
			std::set<size_t> itersec;
			std::insert_iterator< std::set<size_t> > inserter( itersec, itersec.begin() );
			std::set_intersection(v1Ind.begin(), v1Ind.end(), v2Ind.begin(), v2Ind.end(), inserter);
			if(itersec.size() < m_stMinIntersec)
				return 0;
			double precent = (double)(itersec.size()) / (double)(filteredIndices.size());
			if( precent < m_dPrecentIntersec )
				return 0;
			// END: tutaj filtrujemy

			std::set<size_t> commonFilteredIndices;

			std::set_intersection(v1Ind.begin(), v1Ind.end(), v2Ind.begin(), v2Ind.end(),
				std::insert_iterator< std::set<size_t> >(commonFilteredIndices, commonFilteredIndices.begin()));


			for(std::set<size_t>::const_iterator iter = commonFilteredIndices.begin(); iter != commonFilteredIndices.end();iter++)
			{
				double v1Val = (v1)[(*iter)];
				double v2Val = (v2)[(*iter)];
				dot += v1Val*v2Val;
			}

			double v1len = 0;
			for(std::set<size_t>::const_iterator iter = v1Ind.begin(); iter != v1Ind.end(); ++iter)
			{
				double v1Val = (v1)[(*iter)];
				v1len += v1Val * v1Val;
			}

			double v2len = 0;
			for(std::set<size_t>::const_iterator iter = v2Ind.begin(); iter != v2Ind.end(); ++iter)
			{
				double v2Val = (v2)[(*iter)];
				v2len += v2Val * v2Val;
			}

			double d =  sqrt(v1len)*sqrt(v2len);

			if(d == 0)
				return 0;

			return (1.0 + (dot / d) )/2.0;
		}


		double CosineFilterSimilarity::MinValue() const 
		{
			return 0;
		}

		double CosineFilterSimilarity::CountSimilarity(Matrices::CompressedVector& v1, Matrices::CompressedVector& v2,
			const std::set<size_t> &v1Ind, const std::set<size_t> &v2Ind)
		{
			return CountSimilarity(v1,v2,-1,-1, v1Ind, v2Ind);
		}

	}
}
