// file: $isip/class/mmedia/NGramParser/NGramParser.h
// version: $Id: NGramParser.h 10636 2007-01-26 22:18:09Z tm334 $
//

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

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

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

#ifndef ISIP_NGRAM_NODE
#include <NGramNode.h>
#endif

// NGramParser: a class that manages N-gram grammar. It can be used to
// convert NIST_ARPA or other grammar format to ISIP internal data
// structure
//
class NGramParser {
  
  //-------------------------------------------------------------
  //
  // public constants
  //
  //-------------------------------------------------------------
public:

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

  //--------------------------------------------------------------
  //
  // other important constants
  //
  //--------------------------------------------------------------
  static const long DEF_ORDER = (long)0;
  
  // define algorithm choices
  //

  // define file format choices for input ngram file
  //
  enum FORMAT { NGRAM_ARPA = 0, DEF_FORMAT = NGRAM_ARPA };
  
  // define static NameMap objects
  //
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  

  static const long ERR = 50600;  
  static const long ERR_ORDER = 50601;
  static const long ERR_TAG = 506012;
  static const long ERR_SYMBOL = 50603;
  static const long ERR_HASH = 50604;
  static const long ERR_FORMAT = 50605;
  static const long ERR_NUM_GRAM = 50606;
  
  //-------------------------------------------------------------
  //
  // protected data
  //
  //-------------------------------------------------------------

protected:

  // the order of N-gram
  //
  long order_d;

  // define a static debug level
  //
  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);

  // method: debug
  //
  boolean debug(const unichar* msg) const {
    return false;
  }

  // method: setDebug
  //
  static boolean setDebug(Integral::DEBUG debug_level) {
    debug_level_d = debug_level;
    return true;
  }
  
  // method: destructor
  //
  ~NGramParser() {}

  // method: default constructor
  //
  NGramParser(long order = DEF_ORDER) {
    order_d = order;
  }

  // method: assign
  //
  boolean assign(const NGramParser& arg) {
    order_d = arg.order_d;
    return true;
  }
  
  // method: operator=
  //
  NGramParser& operator= (const NGramParser& arg) {
    assign(arg);
    return *this;
  }
  
  // method: sofSize
  //
  long sofSize() const {
    return 0;
  }
  
  // method: read
  //
  boolean read(Sof& sof, const long& tag, const String& cname = CLASS_NAME) {
    return Error::handle(name(), L"read", Error::NOT_IMPLEM,
			 __FILE__, __LINE__);
  }

  // method: write
  //
  boolean write(Sof& sof, long tag, const String& cname = CLASS_NAME) const {
    return Error::handle(name(), L"write", Error::NOT_IMPLEM,
			 __FILE__, __LINE__);
  }

  // method: eq
  //
  boolean eq(const NGramParser& arg) const {
    return (order_d == arg.order_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) {
    order_d = DEF_ORDER;
    return true;
  }
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  set methods
  //
  //---------------------------------------------------------------------------

  // method: setOrder
  //
  boolean setOrder(long order) {
    order_d = order;
    return true;
  }

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

  // method: getOrder
  //
  long getOrder() const {
    return order_d;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  operation related methods
  //
  //---------------------------------------------------------------------------

  // method: load
  //
  boolean load(Sof& sof, HashTable<Long, NGramNode>& gram_hash,
	       const Vector<String>& symbol_table,
	       const long& tag, const String& name);

  boolean store(Sof& file, 
			     const HashTable<Long, NGramNode>& gram_hash,
			     const Vector<String>& symbol_table,
			     const long& tag, const String& name) const;
    
  //-------------------------------------------------------------
  //
  // private methods
  //
  //-------------------------------------------------------------
private:

  // method: storeToHash
  //
  boolean storeToHash(HashTable<Long, NGramNode>& gram_hash, 
		      SingleLinkedList<NGramNode>& symbol_list,
		      NGramNode* prefix_node);
};

// end of include file
//
#endif
