
#pragma once
#include "../comparator_stdheaders.h"
#include "SimilarityFunction.h"
#include <boost/shared_ptr.hpp>

namespace smartcomparator
{
	namespace simfunctions
	{
		class SMART_COMPARATOR_API PartialMSR : public SimilarityFunction
		{
		public:
			typedef std::set<size_t> 						Indices;
			typedef boost::shared_ptr< std::set<size_t> > 	IndicesPtr;
			typedef std::vector<size_t>						SparseIndices;


		public:
			static Matrices::SuperMatrixPtr	LoadFrequencies( std::string const& pDir, std::string const& pName );
			static IndicesPtr				ReadBadFeatures( std::string const& pFile );


		private:
			void CheckFile( std::string const& pFile );

		private:
			IndicesPtr					mBadFeatures;
			Matrices::SuperMatrixPtr	mFrequenciesMatrix;
			SimilarityFunctionPtr		mSimFunction;
			SparseIndices				mFeatureWeightToFreq;

		public:
			PartialMSR(
				Matrices::SuperMatrixPtr&	pWeightsMatrix,
				Matrices::SuperMatrixPtr&	pFrequencies,
				IndicesPtr&					pBadFeaturesIncides,
				SimilarityFunctionPtr&		pSimilarityFunction );
			virtual ~PartialMSR(void);

			virtual void Init();

			virtual double CountSimilarity(
				Matrices::CompressedVector& pWeigts1,						// i think so
				Matrices::CompressedVector& pWeigts2,						// i think so
				size_t 						pLemmaIndex1,					// yup
				size_t 						pLemmaIndex2,					// yup
				const std::set<size_t>&		pNonZeroFeaturesIndices1,		// yup
				const std::set<size_t>&		pNonZeroFeaturesIndices2 );		// yup

			/**
			Wersja lokalna
			*/
			virtual double CountSimilarity(
				Matrices::CompressedVector& pWeights1,
				Matrices::CompressedVector& pWeights2,
				const std::set<size_t>&		pNonZeroIndices1,
				const std::set<size_t>&		pNonZeroIndices2 );

			virtual double MinValue() const;

		protected:
			size_t findWordFreqIndex( size_t pWeightLemmaIndex );
			size_t findFeatureFreqIndex( size_t	pWeightFeatureIndex );

			IndicesPtr 	funZN(
				Indices const&			 	pNonZeroFeaturesIndices,
				Matrices::CompressedVector& pFrequencies,
				Matrices::CompressedVector& pWeights );

			bool		funCZ(
				IndicesPtr& 				pLZ1,
				Matrices::CompressedVector& pFrequencies1,
				Matrices::CompressedVector& pWeights1,
				IndicesPtr& 				pLZ2,
				Matrices::CompressedVector& pFrequencies2,
				Matrices::CompressedVector& pWeights2 );
		};
	}
}
