/*
    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 "Distance.h"


namespace smartcomparator
{
	namespace distances
	{

		DistanceFunction::DistanceFunction(Matrices::SuperMatrixPtr m) : m_pMatrix(m)
		{
		}

		DistanceFunction::~DistanceFunction(void)
		{
		}


		void DistanceFunction::SetUnion(std::set<size_t>::const_iterator &b1, std::set<size_t>::const_iterator &e1,
			std::set<size_t>::const_iterator &b2, std::set<size_t>::const_iterator &e2,
			std::set<size_t> &out)
		{
			for(std::set<size_t>::const_iterator iter = b1; iter != e1; iter++)
				out.insert( *iter );

			for(std::set<size_t>::const_iterator iter = b2; iter != e2; iter++)
				out.insert( *iter );

		}

		/**
		 *
		 * @param r1
		 * @param r2
		 * @param s1
		 * @param s2
		 * @param out
		 * @param fillZeros
		 */
		void DistanceFunction::SetUnion(Matrices::CompressedVector &r1, Matrices::CompressedVector &r2,
			const std::set<size_t> &s1, const std::set<size_t> &s2,	std::set<size_t> &out, bool fillZeros)
		{
			std::set<size_t>::const_iterator s1b = s1.begin(), s1e=s1.end(), s2b = s2.begin(), s2e = s2.end();

			SetUnion(s1b, s1e, s2b, s2e, out);

			if(fillZeros)
			{
				for(std::set<size_t>::const_iterator i = s1.begin(); i != s1.end(); ++i)
				{
					if( s2.find( *i ) == s2.end() )
					{
						if( r2[ (*i) ] != 0 )
						{
							r2.SetElement( *i, 0 );
						}
					}
				}

				for(std::set<size_t>::const_iterator i = s2.begin(); i != s2.end(); i++)
				{
					if( s1.find( *i ) == s1.end() )
					{
						if( r1[ (*i) ] != 0 )
							r1.SetElement( *i, 0 );
					}
				}
			}
		}

		bool DistanceFunction::IsSymmetrical() const
		{
			return true;
		}


		void DistanceFunction::SetUnion(std::vector<size_t>::const_iterator &b1, std::vector<size_t>::const_iterator &e1,
			std::vector<size_t>::const_iterator &b2, std::vector<size_t>::const_iterator &e2,
			std::set<size_t> &out)
		{
			for(std::vector<size_t>::const_iterator iter = b1; iter != e1; iter++)
				out.insert( *iter );

			for(std::vector<size_t>::const_iterator iter = b2; iter != e2; iter++)
				out.insert( *iter );
		}


	}
}
