/*------------------------------------------------*/
void NOMAD::Cache::insert ( const NOMAD::Eval_Point & x )
{
    // check the eval types:
    if ( x.get_eval_type() != _eval_type )
        throw NOMAD::Cache::Cache_Error ( "Cache.cpp" , __LINE__ ,
                                         "NOMAD::Cache:insert(x): x.eval_type != cache.eval_type" );
    
    // insertion in _extern_pts:
    insert_extern_point ( x );
    
    // insertion in _cache2:
    NOMAD::Cache_Point cp  ( &x );
    _cache2.insert  (  cp  );
    x.set_in_cache ( true );
    _sizeof += x.size_of();
}
/*----------------------------------------------------*/
bool NOMAD::Cache::read_points_from_cache_file ( std::ifstream & fin             ,
                                                const int     * p_nb_bb_outputs ,
                                                bool            display           )
{
    try {
        
        NOMAD::Clock c;
        
        // the stream is placed at the first point (after the CACHE_FILE_ID tag):
        fin.seekg ( sizeof ( NOMAD::CACHE_FILE_ID ) , std::ios::beg );
        
        NOMAD::Cache_File_Point   cfp;
        NOMAD::Eval_Point       * cur;
        const NOMAD::Eval_Point * cache_x;
        
        // main loop:
        while ( !fin.eof() ) {
            
            // reading of the Cache_File_Point:
            if ( !cfp.read ( fin ) ) {
                if ( fin.eof() )
                    break;
                return false;
            }
            
            // we ignore this cache file point if it has a different
            // number of blackbox outputs than *p_nb_bb_outputs:
            if ( p_nb_bb_outputs && cfp.get_m() != *p_nb_bb_outputs )
                continue;
            
            // creation of the Eval_Point:
            cur = new NOMAD::Eval_Point ( cfp , _eval_type );
            
            // we look if the current point is already in cache:
            cache_x = find ( *cur );
            
            // the current point is already in cache:
            if ( cache_x ) {
                update ( get_modifiable_point ( *cache_x ) , *cur );
                delete cur;
            }
            
            // point not in cache: insertion:
            else {
                
                // insertion in _extern_pts:
                insert_extern_point ( *cur );
                
                // insertion in _cache1:
                NOMAD::Cache_Point cp ( cur );
                _cache1.insert    ( cp   );
                cur->set_in_cache ( true );
                _sizeof += cur->size_of();
            }
            
        } // end of main loop
        
        // display stats on the cache load:
        if ( display ) {
            _out << "number of points: " << static_cast<int>(_cache1.size()) << std::endl
            << "size            : ";
            _out.display_size_of ( _sizeof );
            _out << std::endl
            << "load time       : " << c.get_real_time() << 's' << std::endl;
        }
    }
    catch ( ... ) {
        return false;
    }
    return true;
}