// file: $isip/class/algo/Algorithm/Algorithm.h
// version: $Id: Algorithm.h 10654 2008-01-04 19:39:43Z dom5 $
//

// make sure definitions are only made once
//
#ifndef ISIP_ALGORITHM
#define ISIP_ALGORITHM

#ifndef ISIP_ALGORITHM_BASE
#include <AlgorithmBase.h>
#endif

#ifndef ISIP_ALGORITHM_UNDEFINED
#include <AlgorithmUndefined.h>
#endif

#ifndef ISIP_COEFFICIENT_LABEL
#include <CoefficientLabel.h>
#endif

#ifndef ISIP_MEMORY_MANAGER
#include <MemoryManager.h>
#endif

#ifndef ISIP_OUTOUT
#include <Output.h>
#endif

#ifndef ISIP_GENERATOR
#include <Generator.h>
#endif

#ifndef ISIP_CONSTANT
#include <Constant.h>
#endif

#ifndef ISIP_NAME_MAP
#include <NameMap.h>
#endif

// Algorithm: a class that transforms data using an abstract base
// class having virtual functions. The virtual functions allow the
// derived class to replace the functionality provided by the base
// class. The class AlgorithmBase defines the interface contract for
// all the Algorithms.
//
class Algorithm : public AlgorithmBase {

  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:

  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------

  // define the enumeration for data type
  //  
  enum TYPES { ALGORITHM_CONTAINER = 0, CALCULUS, CEPSTRUM, COEFFICIENT_LABEL,
	       CONNECTION, CONSTANT, CORRELATION, COVARIANCE,
	       DISPLAY_DATA, DYNAMIC_FILTER, ENERGY, FILTER, FILTER_BANK, FOURIER_TRANSFORM,
	       GENERATOR, LOG_AREA_RATIO, MASK, MATH, OUTPUT,
	       PREDICTION, REFLECTION, SPECTRUM, STATISTICS, WINDOW,
	       RPS, LYAPUNOV, CORRELATION_INTEGRAL,
	       MUTUAL_INFORMATION, CORRELATION_ENTROPY, CORRELATION_DIMENSION,
	       DEF_TYPES = CALCULUS };

  // define static NameMap objects
  //  
  static const NameMap TYPES_MAP;
  
  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------  

  static const String DEF_PARAM;
  static const String PARAM_TYPES;

  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  

  static const long ERR = 70050;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------

protected:

  // pointer to the base class object
  //
  AlgorithmBase* virtual_algo_d;

  // define an empty object used for uninitialized things
  //
  static AlgorithmUndefined NO_ALGORITHM;

  // memory manager
  //
  static MemoryManager mgr_d;

  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:
  
  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }
  
  // other static methods
  //
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // method: debug
  //
  boolean debug(const unichar* msg) const {
    return virtual_algo_d->debug(msg);
  }

  // destructor/constructor(s)
  //
  ~Algorithm();
  Algorithm();
  Algorithm(const Algorithm& arg);

  // assign methods
  //
  boolean assign(const Algorithm& arg);
  
  // i/o methods
  //
  long sofSize() const; 
  
  boolean read(Sof& sof, long tag, const String& name = CLASS_NAME);
  boolean write(Sof& sof, long tag, const String& name = CLASS_NAME) const;

  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
		   long size = SofParser::FULL_OBJECT,
		   boolean param = true, boolean nested = false);

  boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const;
  
  // method: eq
  //
  boolean eq(const Algorithm& arg) const {
    return virtual_algo_d->eq(*(arg.virtual_algo_d));
  }

  // method: new
  //
  static void* operator new(size_t size) {
    return mgr_d.get();
  }

  // method: new[]
  //
  static void* operator new[](size_t size) {
    return mgr_d.getBlock(size);
  }

  // method: delete
  //
  static void operator delete(void* ptr) {
    mgr_d.release(ptr);
  }

  // method: delete[]
  //
  static void operator delete[](void* ptr) {
    mgr_d.releaseBlock(ptr);
  }

  // method: setGrowSize
  //
  static boolean setGrowSize(long grow_size) {
    return mgr_d.setGrow(grow_size);
  }

  // method: clear
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE) {
    if (virtual_algo_d != (AlgorithmBase*)&NO_ALGORITHM) {
      delete virtual_algo_d;
      virtual_algo_d = &NO_ALGORITHM;
    }
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  set methods
  //
  //  note: not all the set methods available in AlgorithmBase are
  //  preserved here.
  //
  //---------------------------------------------------------------------------

  // method: setInputFilename
  //
  boolean setInputFilename(Filename& inputname) {
    if (getType() == OUTPUT)
      (dynamic_cast< Output*>(virtual_algo_d))->setInputFilename(inputname);
    return true;
  }
  
  // method: setOutputDirectory
  //
  boolean setOutputDirectory(String& new_dir) {
    if (getType() == OUTPUT)
      dynamic_cast <Output*> (virtual_algo_d)->setOutputDirectory(new_dir);
    return true;
  }

  // method: setOutputBasename
  //
  boolean setOutputBasename(String& new_basename) {
    if (getType() == OUTPUT)
      dynamic_cast <Output*> (virtual_algo_d)->setOutputBasename(new_basename);
    return true;
  }

  // method: setOutputExtension
  //
  boolean setOutputExtension(String& new_ext) {
    if (getType() == OUTPUT)
      dynamic_cast <Output*> (virtual_algo_d)->setOutputExtension(new_ext);
    return true;
  }
  
  // method: setOutputPreserve
  //
  boolean setOutputPreserve(long new_preserve_level) {
    if (getType() == OUTPUT)
      dynamic_cast <Output*> (virtual_algo_d)->setOutputPreserve(new_preserve_level);
    return true;
  }
  
  // method: setOutputSuffix
  //
  boolean setOutputSuffix(String& new_suffix) {
    if (getType() == OUTPUT)
      dynamic_cast <Output*> (virtual_algo_d)->setOutputSuffix(new_suffix);
    return true;
  }

  // method: setOutputType
  //
  boolean setOutputType(File::TYPE type) {
    if (getType() == OUTPUT)
      dynamic_cast <Output*>(virtual_algo_d)->setOutputType(type);
    return true;    
  }
  
  // method: setSampleFrequency
  //
  boolean setSampleFrequency(float sf) {
    return virtual_algo_d->setSampleFrequency(sf);
  }

  // method: setGenSampleFrequency
  //
  boolean setGenSampleFrequency(float sf) {
    if (getType() == GENERATOR)
      dynamic_cast <Generator*> (virtual_algo_d)->setGenSampleFrequency(sf);
    return true;        
  }
  
  // method: setFrameDuration
  //
  boolean setFrameDuration(float dur) {
    return virtual_algo_d->setFrameDuration(dur);
  }

  // method: setSignalDuration
  //
  boolean setSignalDuration(float dur) {
    return virtual_algo_d->setSignalDuration(dur);
  }  

  // method: setLeftoverSamps
  //
  boolean setLeftoverSamps(long samps) {
    return virtual_algo_d->setLeftoverSamps(samps);
  }
    
  // method: setFrameIndex
  //
  boolean setFrameIndex(long index) {
    return virtual_algo_d->setFrameIndex(index);
  }

  // configuration methods
  //
  boolean setType(TYPES type);
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  get methods
  //
  //  note: not all the get methods available in AlgorithmBase are
  //  preserved here.
  //
  //---------------------------------------------------------------------------

  // method: getInputFilename
  //
  boolean getInputFilename(Filename& filename) const {
    if (getType() == OUTPUT)
      dynamic_cast <Output*> (virtual_algo_d)->getInputFilename(filename);
    return true;
  }

  // method: getInputFilename
  //
  boolean getInputFilename(String& filename) const {
    if (getType() == CONSTANT) {
      dynamic_cast <Constant*> (virtual_algo_d)->getInputFilename(filename);
    }
    else
      filename.assign(String::NULL_STRING);
    return true;
  }
  
  // method: getOutputFilename
  //
  boolean getOutputFilename(String& filename) const {
    
    if (getType() == OUTPUT) {
      dynamic_cast <Output*> (virtual_algo_d)->getOutputFilename(filename);
    }
    else if (getType() == CONSTANT) {
      dynamic_cast <Constant*> (virtual_algo_d)->getOutputFilename(filename);
    }
    else
      filename.assign(String::NULL_STRING);
    return true;
  }

  // method: getOutputDirectory
  //
  boolean getOutputDirectory(String& directory) const {
    if (getType() == OUTPUT)
      return dynamic_cast <Output*> (virtual_algo_d)->
	getOutputDirectory(directory);
    else
      return true;
  }

  // method: getOutputBasename
  //
  boolean getOutputBasename(String& basename) const {
    if (getType() == OUTPUT)
      return dynamic_cast <Output*> (virtual_algo_d)->
	getOutputBasename(basename);
    else
      return true;
  }

  // method: getOutputExtension
  //
  boolean getOutputExtension(String& extension) const {
    if (getType() == OUTPUT)
      return dynamic_cast <Output*> (virtual_algo_d)->
	getOutputExtension(extension);
    else
      return true;
  }  

  // method: getOutputPreserve
  //
  long getOutputPreserve() const {
    if (getType() == OUTPUT)
      return dynamic_cast <Output*> (virtual_algo_d)->
	getOutputPreserve();
    else
      return 0;
  }

  // method: getOutputSuffix
  //
  boolean getOutputSuffix(String& suffix) const {
    if (getType() == OUTPUT)
      return dynamic_cast <Output*> (virtual_algo_d)->
	getOutputSuffix(suffix);
    else
      return true;
  }

  // method: getGenSampleFrequency
  //
  boolean getGenSampleFrequency(Float& sf) {
    if (getType() == GENERATOR)
      return dynamic_cast <Generator*> (virtual_algo_d)->
	getGenSampleFrequency(sf);
    return true;
  }
  
  // other miscellaneouos get methods:
  //
  TYPES getType() const;
  const String& getCLabelVariable() const;
  CoefficientLabel::TYPE getCLabelType() const;
  long getContainerGraphTag() const;
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  public methods required by the AlgorithmBase interface contract
  //
  //---------------------------------------------------------------------------

  // assign methods
  //
  boolean assign(const AlgorithmBase& arg);

  // method: eq
  //
  boolean eq(const AlgorithmBase& arg) const {
    return virtual_algo_d->eq(arg);
  }

  // method: className
  //
  const String& className() const {
    return virtual_algo_d->className();
  }

  // method: init
  //
  boolean init() {
    return virtual_algo_d->init();
  }
  
  // method: apply
  //
  boolean apply(Vector<AlgorithmData>& output,
		const Vector< CircularBuffer<AlgorithmData> >& input);
  
  // method: getLeadingPad
  //
  long getLeadingPad() const {
    return virtual_algo_d->getLeadingPad();
  }

  // method: getTrailingPad
  //
  long getTrailingPad() const {
    return virtual_algo_d->getTrailingPad();
  }

  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:
};

// end of include file
// 
#endif
