/*
    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 <iostream>
#include <fstream>
#include "matrices_all.h"

typedef boost::shared_ptr<std::vector<size_t> > size_t_vector_ptr;

class BoundaryGrouper
{
public:
	BoundaryGrouper(std::string grfile, std::string mdir,std::string mname,std::string outdir,std::string outname, std::string method);
	void loadGroups();
	void loadSourceMatrix();
	void createResultMatrix();
	void saveResultMatrix();
protected:
	std::string groupFilename;
	std::string matrixDir;
	std::string matrixName;

	std::string outMatrixDir;
	std::string outMatrixName;

	std::string method;

	bool skipOutliers;

	std::map<size_t, size_t_vector_ptr> groups;

	Matrices::SuperMatrixPtr sourceMatrix;
	Matrices::SuperMatrixPtr newMatrix;

	void processRow(std::set<size_t>::iterator rowIt, std::map<size_t, size_t_vector_ptr >::iterator it, size_t colNo);
	void processRowSum(std::set<size_t>::iterator rowIt, std::map<size_t, size_t_vector_ptr >::iterator it, size_t colNo);
	void processRowAverage(std::set<size_t>::iterator rowIt, std::map<size_t, size_t_vector_ptr >::iterator it, size_t colNo);
	void processRowWeightedAverage(std::set<size_t>::iterator rowIt, std::map<size_t, size_t_vector_ptr >::iterator it, size_t colNo);
	void processRowMedian(std::set<size_t>::iterator rowIt, std::map<size_t, size_t_vector_ptr >::iterator it, size_t colNo);

	void processMatrixBounds(Matrices::SuperMatrixPtr outMatrix);
};
