/**
* @file FeaturePool.cpp
* @brief collection of features
* @author Erik Rodner
* @date 04/21/2008

*/
#include <iostream>
#include <stdlib.h>

#include "FeaturePool.h"
#include "vislearning/features/fpfeatures/VectorFeature.h"

using namespace OBJREC;

using namespace std;

using namespace NICE;

FeaturePool::FeaturePool()
{
	conf = NULL;
	//srand48(time(NULL));
}

FeaturePool::FeaturePool(const Config *conf)
{
	this->conf = conf;
}

FeaturePool::~FeaturePool()
{
}

Feature *FeaturePool::getRandomFeature() const
{
	assert(size() == cumsum.size());
	assert(! empty());
	double sum = cumsum[ cumsum.size() - 1 ];
#ifdef WIN32
	double pos = (double( rand() ) / RAND_MAX )* sum;
#else
	double pos = drand48() * sum;
#endif

	vector<double>::const_iterator j = lower_bound(cumsum.begin(), cumsum.end(), pos);
	uint index = distance(cumsum.begin(), j);

	const_iterator i = begin() + index;

	return i->second;
}

void FeaturePool::initRandomFeatureSelection()
{
	double sum = 0.0;

    cumsum.clear();
    cumsum.reserve( size() );
    for ( vector< pair<double, Feature *> >::const_iterator i = begin();
						      i != end();
						      i++ )
    {
		sum += i->first;
		cumsum.push_back ( sum );	
    }
}

void FeaturePool::addFeature(Feature *f, double probability)
{
	assert(NICE::isFinite(probability));
	assert(probability > 10e-12);
	push_back(pair<double, Feature *> (probability, f));
}

void FeaturePool::destroy()
{
	for (vector< pair<double, Feature *> >::iterator i = begin();
						i != end();
						i++)
	{
		assert(i->second != NULL);
		delete i->second;
	}

	vector<pair<double, Feature *> >::clear();
}

void FeaturePool::restore(istream & is, int format)
{
	vector<pair<int, double> > tmpPool;
	clear();

	fprintf(stderr, "FeaturePool::restore: reading plain vector features!\n");
    while ( !is.eof() )
    {
		double weight = 1.0;
		if ( !(is >> weight) ) break;

		std::string feature_tag;
		if ( !(is >> feature_tag) ) break;
		if ( feature_tag != "VECTORFEATURE" ) {
			fthrow(Exception, "FeaturePool::restore: if you want to read features other than VectorFeature, use createFeatures::...");
		}
		int feature_index;
		if ( !(is >> feature_index) ) break;

		tmpPool.push_back ( pair<int, double> ( feature_index, weight ) );
    }
	
	for ( vector<pair<int, double> >::const_iterator i = tmpPool.begin();
		i != tmpPool.end(); i++ )
	{
		int feature_index = i->first;
		double weight = i->second;
		Feature *f = new VectorFeature ( tmpPool.size(), feature_index );
		push_back ( pair<double, Feature *> ( weight, f ) );
	}

}

void FeaturePool::store(ostream & os, int format) const
{
    for ( const_iterator i = begin(); i != end(); i++ )
    {
		const Feature *f = i->second;
		os << i->first << " ";
		f->store ( os, format );
		os << endl;
    }
}

void FeaturePool::clear()
{
	destroy();
}

void FeaturePool::calcFeatureVector ( const Example & pe, NICE::Vector & x ) const
{
    x.resize ( size() );
    int index = 0;
    for ( const_iterator i = begin(); i != end(); i++, index++ )
    {
		const Feature *f = i->second;
		x[index] = f->val ( &pe );
    }
}