/******************************************************************************
* NOMAD -  Nonlinear Optimization for Mixed vAriables and Derivatives.        *
* Copyright (C) 2001-2004  Gilles Couture - Ecole Polytechnique,              *
*                          Charles Audet - Ecole Polytechnique,               *
*		           John Dennis - Rice University.                     *
*                                                                             *
* Author: Gilles Couture                                                      *
* École Polytechnique de Montréal - GERAD                                     *
* C.P. 6079, Succ. Centre-ville, Montréal (Québec) H3C 3A7 Canada             *
* Tel:  (514) 340-6053 x6928                                                  *
* Email: Gilles.Couture@gerad.ca                                              *
* Fax: (514) 340-5665                                                         *
*                                                                             *
* This program is free software; you can redistribute it and/or               *
* modify it under the terms of the GNU General Public License                 *
* as published by the Free Software Foundation. You can find the              *
* GNU General Public License at:                                              *
*             http://www.gnu.org/copyleft/gpl.html                            *
*                                                                             *
* 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               *
* GNU General Public License for more details.                                *
*                                                                             *
* You should have received a copy of the GNU General Public License           *
* along with this program; if not, write to the Free Software                 *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
*                                                                             *
* You can find information on the NOMAD software at:                          *
*             http://www.gerad.ca/NOMAD/                                      *
******************************************************************************/

// stlcache.h -- class definition for StlCache. The methods of this class
// are called from CacheControl. This class manages the NOMAD database. The
// heart of the cache is a STL map container.

#ifndef _STLCACHE_H
#define _STLCACHE_H
#include <map>
using namespace std;
#include "database.h"

// This structure defines the comparison between Point objects. It's used by
// the STL map.
struct PointComparison {
  bool operator() (const Point * p1 , const Point * p2) const
  {return (*p1 < *p2);}
};

// StlCache has public inheritance from the Database class.
class StlCache: public Database
{
private:
  static const int VERSION;
  int cache_hits, nb_pairs_in_cache;
  std::map<Point *, Point *, PointComparison> cache;

  void destroyCache();
  // Public methods: described in "stlcache.cpp" and "database.h"
public:
  StlCache(const char * name, const int dim, const int out_dim);
  ~StlCache();
  bool find(AugmentedPoint & ap, const double * key);
  void insert(double * inp, double * utp);
  void operator+=(StlCache & temp);
  void read();
  void reset();
  void show() const;
  void showOutput(ofstream & fout) const;
  void write();

  // Inline methods.
  // ---------------

  // Returns the cache evaluations counter.
  int getHits() const {return cache_hits;}
  // The user wants to continue the run: the cache hits counter is reset.
  void continueRun() {cache_hits = 0;}
};
#endif
