/*
    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.
*/

#ifndef MAIN_CPP
#define MAIN_CPP
#include "matrices_all.h"

void helpMsg(char* av)
{
	std::cout<<"Program filtering matrix 1 by groups made from matrix 2's similarity lists\n"
			<<"Runtime format: "<<av<<" matrix_to_be_filtered matrix_with_groups output_file";
}

int main(int ac, char**av)
{
	Matrices::init_logging();
	if(ac>0 && (strcmp(av[1],"--help")==0))
	{
		helpMsg(av[0]);
		return 1;
	}
	if(ac!=4)
	{
		helpMsg(av[0]);
		throw std::runtime_error("Invalid number of parameters!\n");
		return -1;
	}
	Matrices::CRSMatrixPtr filteredMatrix = Matrices::IO::ReadCRSFromSingleFile(av[1],"filtered_matrix");
	Matrices::CRSMatrixPtr groupMatrix = Matrices::IO::ReadCRSFromSingleFile(av[2],"group_matrix");


	//	std::map<size_t, std::set<size_t> > word2group;
	for(size_t i=0;i<groupMatrix->NumberOfRows();i++)
	{
		//word2group.insert(std::make_pair(i,std::set<size_t>()));
		Matrices::STDoubleMapPtr groupValues = groupMatrix->GetCompressedRow(i)->GetValues();

		Matrices::STDoubleMapPtr values = filteredMatrix->GetCompressedRow(i)->GetValues();
		for(Matrices::STDoubleMapIter iter = values->begin();iter!=values->end();)
		{
			if(groupValues->find(iter->first)==groupValues->end())
			{
				values->erase(iter++);


			}
			else
				iter++;
		}
	}
	Matrices::IO::WriteToSingleFile(filteredMatrix,av[3]);

	return 0;
}

#endif // MAIN_CPP
