/*
    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 MATRICESLIB_IO_SUPERMATRIXFILTEREDREADER_H
#define MATRICESLIB_IO_SUPERMATRIXFILTEREDREADER_H

#include "../matrices_headers.h"

#include "filesystem_error.h"
#include "../Feature.h"
#include "SuperMatrixReader.h"

namespace Matrices
{
	class SuperMatrix;

	namespace IO
	{
		/**
		 Nie obsluguje znakow konca linii w polach elementow plikow CSV (pliki cech) - uzywamy uproszczonego formatu CSV.
		 \todo Testing of reading by columns
		 */
		class MATRICES_API SuperMatrixFilteredReader
		{
		public:
			/**
			 * Reads SuperMatrix. Only words in file words_file are readed.
             * \todo Combination of filtering columns + removing boundaries may be broken, do not use it (or test it before usage)
			 */
			SuperMatrixFilteredReader(const std::string &words_file, const std::string &dir, const std::string &name,
					bool load_by_columns = true, bool load_by_rows = true, bool filter_rows = true,
					bool filter_columns = true);

			/// returns loaded matrix.
			Matrices::SuperMatrixPtr GetMatrix();

		private:
			void LoadWords();
			void LoadFeatures();
			void LoadByRows();
			void LoadByColumns();
			void LoadFeatures(const std::string &filename, FeatureVectorPtr features, std::set<size_t> &remove_indices,
					std::map<size_t, size_t> &old_to_new, bool filter);
			void LoadInfo();

			std::set<std::string> m_Words;
			std::map<size_t, size_t> m_mOldToNewRows;
			std::map<size_t, size_t> m_mOldToNewColumns;
			std::set<size_t> m_RowIndexToRemove;
			std::set<size_t> m_ColumnIndexToRemove;
			FeatureVectorPtr m_pvRowFeatures;
			FeatureVectorPtr m_pvColumnFeatures;
			Matrices::CRSMatrixPtr m_pRowMatrix;
			Matrices::CCSMatrixPtr m_pColumnMatrix;

			Matrices::SuperMatrixPtr m_pMatrix;

			std::string m_sDir;
			std::string m_sName;
			std::string m_sWordsFile;
			std::string m_sCorpusInfo;
			size_t m_stTokens;

			bool m_bFilterRows;
			bool m_bFilterColumns;

			bool m_bLoadByColumns;
			bool m_bLoadByRows;
		};

		/// just a facade for SuperMatrixFilteredReader class.
		Matrices::SuperMatrixPtr MATRICES_API LoadSuperMatrixFiltered(const std::string &words_file, const std::string &dir,
				const std::string &name, bool load_by_columns = true, bool load_by_rows = true,
				bool filter_rows = true, bool filter_columns = true);
	}
}

#endif // MATRICESLIB_IO_SUPERMATRIXFILTEREDREADER_H
