// file: $isip/class/pr/NGramNode/NGramNode.h
// version: $Id: NGramNode.h 8731 2002-09-29 17:56:46Z huang $
//

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

#ifndef ISIP_SOF
#include <Sof.h>
#endif

#ifndef ISIP_HASH_TABLE
#include <HashTable.h>
#endif

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

// NGramNode: a class that stores N-gram probability.
//
class NGramNode {
  
  //-------------------------------------------------------------
  //
  // public constants
  //
  //-------------------------------------------------------------
public:

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

  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------  
  
  static const String PARAM_INDEX;
  static const String PARAM_LM_SCORE;
  static const String PARAM_BACKOFF; 
  static const String PARAM_NEXT_GRAM;
  static const String PARAM_HASH;
  
  //--------------------------------------------------------------
  //
  // other important constants
  //
  //--------------------------------------------------------------

  // define the default value(s) of the class data
  //
  static const String DEF_PARAM;
  static const long DEF_INDEX;
  static const float DEF_LM_SCORE;
  static const float DEF_BACKOFF;
  
  // define algorithm choices
  //

  // define file format choices for output models
  //
  
  // define static NameMap objects
  //
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  

  static const long ERR = (long)50700;  

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

protected:

  // symbol string represented in this node
  //
  Long index_d;
  
  // language model and backoff score information
  //
  Float lm_score_d;
  Float backoff_d;
  
  // a pointer to the next order ngram hash table
  //
  HashTable<Long, NGramNode>* next_gram_d;

  // declare a static debug level for all class instantiations
  //
  static Integral::DEBUG debug_level_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:
  //
  boolean debug(const unichar* msg) const;
  
  // method: destructor
  //
  ~NGramNode() {
    clear(Integral::FREE);
  }

  // method: default constructor
  //
  NGramNode() {
    next_gram_d = NULL;
    lm_score_d = DEF_LM_SCORE;
    backoff_d = DEF_BACKOFF;
  }

  // method: copy constructor
  //
  NGramNode(const NGramNode& arg) {
    next_gram_d = NULL;
    assign(arg);
  }
  
  // method: assign
  //
  boolean assign(const NGramNode& arg) {
    index_d = arg.index_d;
    lm_score_d = arg.lm_score_d;
    backoff_d = arg.backoff_d;
    if (next_gram_d != NULL) delete next_gram_d;
    if (arg.next_gram_d != NULL) {
      next_gram_d = new HashTable<Long, NGramNode>;
      next_gram_d->assign(*(arg.next_gram_d));
    }
    else next_gram_d = NULL;
    return true;
  }

  // method: operator=
  //
  NGramNode& operator= (const NGramNode& arg) {
    assign(arg);
    return *this;
  }

  // method: sofSize
  //
  long sofSize() const;
  
  // read methods
  //
  boolean read(Sof& sof, const long tag, const String& cname = CLASS_NAME);
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
                   long size = SofParser::FULL_OBJECT,
                   boolean param = true,
                   boolean nested = false);
  
  // write methods
  //
  boolean write(Sof& sof, long tag, const String& cname = CLASS_NAME) const;
  boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const;

  // equality methods
  //
  boolean eq(const NGramNode& 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);
  }
  
  // method: clear
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE) {
    if (next_gram_d != NULL) {
      delete next_gram_d;
      next_gram_d = NULL;
    }
    return true;
  }
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  set methods
  //
  //---------------------------------------------------------------------------

  // method: setIndex
  //
  boolean setIndex(const long index) {
    index_d = index;
    return true;
  }  

  // method: setLmScore
  //
  boolean setLmScore(const float lm_score) {
    lm_score_d = lm_score;
    return true;
  }

  // method: setBackoff
  //
  boolean setBackoff(const float backoff) {
    backoff_d = backoff;
    return true;
  }

  // method: setNextGram
  //
  boolean setNextGram(HashTable<Long, NGramNode>* hash) {
    next_gram_d = hash;
    return true;
  }

  // method: set
  //
  boolean set(const float lm_score, const float backoff) {
    lm_score_d = lm_score;
    backoff_d = backoff;
    return true;
  }

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

  // method: getIndex
  //
  long getIndex() const {
    return index_d;
  }

  // method: getLmScore
  //
  float getLmScore() const {
    return lm_score_d;
  }

  // method: getBackoff
  //
  float getBackoff() const {
    return backoff_d;
  }

  // method: getNextGram
  //
  HashTable<Long, NGramNode>* getNextGram() const {
    return next_gram_d;
  }
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  operation related methods
  //
  //---------------------------------------------------------------------------

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

};

// end of include file
//
#endif
