// file: $isip/class/search/ContextPool/ContextPool.h
//

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

// isip include files
//
#ifndef ISIP_CONTEXT
#include <Context.h>
#endif

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

// ContextPool: A class to store and manage a pool of contexts
//
class ContextPool {

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

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

  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------  

  static const String DEF_PARAM;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------  
  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // default values
  //
  static const long DEF_CAPACITY = 20000;
  
  //---------------------------------------
  //
  // error codes
  //
  //---------------------------------------
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // define the context pool
  //
  HashTable<Context, Context> pool_d;

  // define a static debug level
  //
  static Integral::DEBUG debug_level_d;
  
  // define a static memory manager
  //
  static MemoryManager mgr_d;

  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:

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

  // method: diagnose
  //
  static boolean diagnose(Integral::DEBUG debug_level);

  // method: debug
  //
  boolean debug(const unichar* message) const;

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

    if (debug_level_d >= Integral::ALL) {     
      fprintf(stdout, "Destructor of context_pool: %p\n", this);
      fflush(stdout);
    }

    // free allocated memory
    //
    clear();
  }

  // constructor(s)
  //
  ContextPool();
  ContextPool(const ContextPool& copy_node);
  
  // assign methods
  //
  boolean assign(const ContextPool& copy_node);

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

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

  // method: readData
  //
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
		   long size = SofParser::FULL_OBJECT, boolean param = true,
                   boolean nested = false) {
    return Error::handle(name(), L"readData", Error::ARG, __FILE__, __LINE__);
  }

  // method: writeData
  //
  boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const {
    return Error::handle(name(), L"writeData", Error::ARG, __FILE__, __LINE__);
  }

  // equality method
  //
  boolean eq(const ContextPool& compare_node) 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);
  }

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

  //---------------------------------------------------------------------------
  //
  // class-specific public methods
  //
  //---------------------------------------------------------------------------

  // method: length
  //
  long length() const {
    return pool_d.getNumItems();
  }
  
  // allocate methods
  //  
  Context* get(const Context& obj);
  Context* foldAndAllocate(const Context* obj);
  Context* extendAndAllocate(const Context* obj,
			     GraphVertex<SearchNode>* ver);  

  
  Context* initAndAllocate();
  Context* shiftAndAllocate(const Context* obj,
			    GraphVertex<SearchNode>* ver);
  Context* setCentralAndAllocate(const Context* obj,
				 GraphVertex<SearchNode>* ver);
  Context* setLastAndAllocate(const Context* obj,
			      GraphVertex<SearchNode>* ver);      
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

};

// end of include file
//
#endif
