// file: $isip/class/algo/Mask/Mask.h
// version: $Id: Mask.h 8279 2002-07-03 03:04:17Z picone $
//

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

// isip include files
//
#ifndef ISIP_ALGORITHM_BASE
#include <AlgorithmBase.h>
#endif

#ifndef ISIP_VECTOR_BYTE
#include <VectorByte.h>
#endif

#ifndef ISIP_STRING
#include <String.h>
#endif

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

// Mask: a class to extract or remove selected parts of an input
// vector. The user can specify which features are to be selected
// (or removed) using a variety of methods.
//
class Mask : public AlgorithmBase {

  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // enumerate the algorithm
  //
  enum ALGORITHM { SELECT = 0, REMOVE, DEF_ALGORITHM = SELECT };

  // enumerate the implementation
  //
  enum IMPLEMENTATION { INDEX = 0, DEF_IMPLEMENTATION = INDEX };

  // define internal on/off states
  //
  static const byte OFF = 0;
  static const byte ON = 1;

  // define the static NameMap objects
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;
  
  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------

  static const String DEF_PARAM;
  static const String PARAM_ALGORITHM;
  static const String PARAM_IMPLEMENTATION;  
  static const String PARAM_MASK;

  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define default argument(s)
  //
  static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE =
  AlgorithmData::DEF_CTYPE;  
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 71200;

  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation name
  //
  IMPLEMENTATION implementation_d;
  
  // string indicating the parts of the input to be kept or removed
  //
  String source_d;

  // mask vector: used internally
  //
  VectorByte mask_d;

  // static 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);

  // debug methods:
  //  setDebug method is inherited from the base class
  //
  boolean debug(const unichar* msg) const;

  // method: destructor
  //
  ~Mask() {}
  
  // method: default constructor
  //
  Mask(ALGORITHM algorithm = DEF_ALGORITHM,
       IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;    
  }

  // method: copy constructor
  //
  Mask(const Mask& arg) {
    assign(arg);
  }
  
  // assign methods
  //
  boolean assign(const Mask& arg);
  
  // method: operator=
  //
  Mask& operator= (const Mask& arg) {
    assign(arg);
    return *this;
  }
  
  // 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;
    
  // equality methods
  //
  boolean eq(const Mask& arg) const;
  
  // 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);
  }

  // other memory management methods
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  extensions to required methods
  //
  //---------------------------------------------------------------------------
  
  // method: constructor
  //
  Mask(const String& arg) { 
    algorithm_d = DEF_ALGORITHM;
    implementation_d = DEF_IMPLEMENTATION;    
    assign(arg);
  }   

  // method: constructor
  //
  Mask(const VectorByte& arg) { 
    algorithm_d = DEF_ALGORITHM;
    implementation_d = DEF_IMPLEMENTATION;    
    assign(arg);
  }   

  // method: assign
  //
  boolean assign(const String& source) {
    return set(source);
  }
  
  // method: assign
  //
  boolean assign(const VectorByte& mask) {
    return set(mask);
  }
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  set methods  
  //
  //---------------------------------------------------------------------------

  // method: setAlgorithm
  //
  boolean setAlgorithm(ALGORITHM algorithm) {
    algorithm_d = algorithm;
    is_valid_d = false;    
    return true;  
  }

  // method: setImplementation
  //
  boolean setImplementation(IMPLEMENTATION implementation) {
    implementation_d = implementation;
    is_valid_d = false;    
    return true;  
  }

  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    is_valid_d = false;
    return true;
  }

  
  // other set methods
  //
  boolean set(const String& source);
  boolean set(const VectorByte& source);
  boolean convert(String& out, const VectorByte& in);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  get methods  
  //
  //---------------------------------------------------------------------------

  // method: getAlgorithm
  //
  ALGORITHM getAlgorithm() const {
    return algorithm_d;
  }

  // method: getImplementation
  //
  IMPLEMENTATION getImplementation() const {
    return implementation_d;
  }
  
  // method: getString
  //
  const String& getString() const {
    return source_d;
  }
  
  // method: get
  //
  const VectorByte& get() const {
    return mask_d;
  }

  // method: get
  //
  boolean get(ALGORITHM& algorithm,
	      IMPLEMENTATION& implementation) const {
    algorithm = algorithm_d;
    implementation = implementation_d;
    return true;
  }
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computational methods
  //
  //---------------------------------------------------------------------------

  // compute methods
  //
  boolean compute(VectorFloat& output, const VectorFloat& input,
		  AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);
  boolean compute(VectorComplexFloat& output,
		  const VectorComplexFloat& input,
		  AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  public methods required by the AlgorithmBase interface contract
  //
  //---------------------------------------------------------------------------

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

  // equality method
  //
  boolean eq(const AlgorithmBase& arg) const;

  // method: className
  //
  const String& className() const {
    return CLASS_NAME;
  }

  // apply methods
  //
  boolean apply(Vector<AlgorithmData>& output,
		const Vector< CircularBuffer<AlgorithmData> >& input);
  
  // method to set the parser
  //
  boolean setParser(SofParser* parser);

  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // common i/o methods
  //
  boolean readDataCommon(Sof& sof, const String& pname,
			 long size = SofParser::FULL_OBJECT,
			 boolean param = true, boolean nested = false);
  boolean writeDataCommon(Sof& sof, const String& pname) const;
  
  // algorithm and implementation specific computational methods:
  //  these methods are private because of the AlgorithmBase interface
  //  contract which works on "apply" method's interface
  //  
  boolean computeSelectIndex(VectorFloat& output, const VectorFloat& input);
  boolean computeRemoveIndex(VectorFloat& output, const VectorFloat& input);

  // insert method
  //
  boolean insert(VectorByte& src, long start_index, long num_elem, byte mode);
};

// end of include file
// 
#endif
