/** 
* @file Codebook.cpp
* @brief feature codebook
* @author Erik Rodner, Alexander Freytag
* @date 05-06-2013 (dd-mm-yyyy ) (original: 02/15/2008)
*/
#include <iostream>

#include "vislearning/features/simplefeatures/Codebook.h"

using namespace OBJREC;

using namespace std;
using namespace NICE;

Codebook::Codebook ( )
{
  this->i_noOfNearestClustersToConsidere = 1;
}

Codebook::Codebook ( const int & _noOfNearestClustersToConsidere )
{
  this->i_noOfNearestClustersToConsidere = _noOfNearestClustersToConsidere;
}

Codebook::Codebook( NICE::Config * _conf, const std::string & _section)
{
  this->p_conf = _conf;
  this->s_section = _section;
  
  this->i_noOfNearestClustersToConsidere = this->p_conf->gI( _section, "noOfNearestClustersToConsidere", 1);
}


Codebook::~Codebook ( )
{
  //NOTE the config does not need to be deleted, it is just a pointer to an external data structure  
}

void Codebook::voteVQ (const NICE::Vector &feature, NICE::Vector &histogram, int &codebookEntry, double &weight, double &distance) const
{
    this->voteVQ ( feature, codebookEntry, weight, distance );
    //VQ, but we directly increase the corresponding entry without setting histogram to zero before (useful if we work on multiple images)
    histogram[codebookEntry] += weight;  
}

void Codebook::voteVQ ( const NICE::Vector & feature, NICE::SparseVector & histogram, int &codebookEntry, double &weight, double &distance ) const
{
    this->voteVQ ( feature, codebookEntry, weight, distance );
    
    //is this cluster already non-zero in the histogram?
    NICE::SparseVector::iterator entryIt = histogram.find( codebookEntry ) ;
    if ( entryIt  == histogram.end() )
    {
      //entry does not exist
      histogram.insert ( histogram.begin(), pair<int, double> ( codebookEntry, weight ) );
    }
    else
    {
      //entry already exists, so we increase the weight
      entryIt->second += weight;
    }
}

bool Codebook::allowsMultipleVoting () const
{
  if ( this->i_noOfNearestClustersToConsidere > 1 )
    return true;
  else
    return false;
}

void Codebook::reinit ( int numCodebookEntries )
{
  thresholds.resize ( numCodebookEntries );
  thresholds.set(0.0);
  informativeMeasure.resize ( numCodebookEntries );
  informativeMeasure.set(0.0);
  classnos.resize ( numCodebookEntries );
  classnos.resize(0);
}

void Codebook::clear ()
{
    thresholds.clear();
    informativeMeasure.clear();
    classnos.clear();
}

void Codebook::restore ( istream & is, int format )
{
    if (is.good())
    {
        std::string tmp;
        is >> tmp; //class name

        if ( ! this->isStartTag( tmp, "Codebook" ) )
        {
            std::cerr << " WARNING - attempt to restore Codebook, but start flag " << tmp << " does not match! Aborting... " << std::endl;
            throw;
        }

        bool b_endOfBlock = false;

        while ( !b_endOfBlock )
        {
            is >> tmp; // start of block

            if ( this->isEndTag( tmp, "Codebook" ) )
            {
                b_endOfBlock = true;
                continue;
            }

            tmp = this->removeStartTag ( tmp );
            if ( tmp.compare("thresholds") == 0 )
            {
                is >> thresholds;
                is >> tmp; // end of block
                tmp = this->removeEndTag ( tmp );
            }
            else if ( tmp.compare("informativeMeasure") == 0 )
            {
                is >> informativeMeasure;
                is >> tmp; // end of block
                tmp = this->removeEndTag ( tmp );
            }
            else if ( tmp.compare("classnos") == 0 )
            {
                is >> classnos;
                is >> tmp; // end of block
                tmp = this->removeEndTag ( tmp );
            }


        }
    }

//    is >> classnos;
   
   //TODO use a flag for compatibility with old systems?
/*   try
   {
     
     is >> i_noOfNearestClustersToConsidere;
     is >> s_section;

     this->p_conf->restore( is, format );
   }
   catch( ... )
   {
     std::cerr << "something went wrong while Loading the codebook - use default values instead" << std::endl;
     //TODO use suitable default values
   }  */ 
}

void Codebook::store ( ostream & os, int format ) const
{
    if (os.good())
    {
        // show starting point
        os << this->createStartTag( "Codebook" ) << std::endl;

        os << this->createStartTag( "thresholds" ) << std::endl;
        os << this->thresholds << endl;
        os << this->createEndTag( "thresholds" ) << std::endl;

        os << this->createStartTag( "informativeMeasure" ) << std::endl;
        os << this->informativeMeasure << endl;
        os << this->createEndTag( "informativeMeasure" ) << std::endl;

        os << this->createStartTag( "classnos" ) << std::endl;
        os << this->classnos << endl;
        os << this->createEndTag( "classnos" ) << std::endl;

        // done
        os << this->createEndTag( "Codebook" ) << std::endl;
    }
//     os << this->i_noOfNearestClustersToConsidere << endl;
//     os << this->s_section << endl;
//     
//     this->p_conf->store( os, format );
}

void Codebook::copy ( const Codebook *codebook )
{
    this->reinit ( codebook->thresholds.size() );
    thresholds = codebook->thresholds;
    informativeMeasure = codebook->informativeMeasure;
    classnos = codebook->classnos;
    this->p_conf = codebook->p_conf;
    this->setNoOfNearestClustersToConsidere ( codebook->getNoOfNearestClustersToConsidere() );
}

void Codebook::setNoOfNearestClustersToConsidere ( const int & _noOfNearestClustersToConsidere )
{
  this->i_noOfNearestClustersToConsidere = _noOfNearestClustersToConsidere;
}

int Codebook::getNoOfNearestClustersToConsidere ( ) const
{
  return this->i_noOfNearestClustersToConsidere;
}

void Codebook::setHardAssignment( const bool & _hardAssignment )
{
  this->b_hardAssignment = _hardAssignment;
}

bool Codebook::getHardAssignment ( ) const
{
  return this->b_hardAssignment;
}
