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

namespace smartcomparator
{
	namespace filters
	{
		FilterVector::FilterVector(Matrices::FeatureVectorPtr cf) : FilterFeature(cf)
		{
			m_pvFilters = new std::vector<FilterFeature*>();
		}

		FilterVector::~FilterVector(void)
		{
			if(m_pvFilters)
			{
				for(std::vector<FilterFeature*>::iterator i = m_pvFilters->begin(); i != m_pvFilters->end(); i++)
				{
					delete *i;
					*i = 0;
				}
				delete m_pvFilters;
			}
		}

		void FilterVector::Filter(std::set<size_t> &out, const std::set<size_t> *nzind)
		{
			std::vector<std::set<size_t> > outSets;
			std::set<size_t>  join;
			for(std::vector<FilterFeature*>::iterator i = m_pvFilters->begin(); i != m_pvFilters->end(); i++)
			{
				std::set<size_t> set;
				(*i)->Filter(set, nzind);
				outSets.push_back(set);
				join.insert(set.begin(), set.end());
			}


			for(std::set<size_t>::iterator i = join.begin(); i != join.end(); i++)
			{
				bool inAll = true;
				for(std::vector<std::set<size_t> >::iterator j = outSets.begin(); j != outSets.end() && inAll; j++)
				{
					if( j->find(*i) == j->end() )
						inAll = false;
				}

				if(inAll)
					out.insert(*i);
			}
		}

		void FilterVector::AddFilter(FilterFeature *f)
		{
			m_pvFilters->push_back(f);
		}
	}
}
