// file: $isip/class/dstr/HashKey/HashKey.h
// version: $Id: HashKey.h 9030 2003-02-18 15:22:39Z alphonso $
//

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

// isip include files
//
#ifndef ISIP_STRING
#include <String.h>
#endif

// HashKey: a generic class the allows the user to use any TObject as
// the key of the HashTable, i.e., the TObject pointer is hashed
//
template<class TObject>
class HashKey {
  
  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------  
  
  static const String DEF_PARAM;
  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------

  // default values
  //
    
  // default arguments to methods
  //
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 42100;
  static const long ERR_NOTDEF = 42101;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // define the array of hash keys
  //
  TObject* obj1_d;
  TObject* obj2_d;

  // define the number of pointers
  //
  long length_d;
  
  // debugging parameters and static memory manager
  //
  static Integral::DEBUG debug_level_d;
  static MemoryManager mgr_d;
  
  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:

  // static methods:
  //  diagnose method is inherited from the Wrapper class
  //
  static const String& name();
  
  // setDebug method in inherited from the Wrapper class
  //

  // debug method in inherited from the Wrapper class
  //
    
  // method: destructor
  //
  ~HashKey() {
    clear();
  }

  // method: default constructor
  //
  HashKey() {
    obj1_d = (TObject*)NULL;
    obj2_d = (TObject*)NULL;
    length_d = 0;
  }

  // method: copy constructor
  //
  HashKey(const HashKey& arg) {
    assign(arg);
  }
  
  // method: assign
  //
  boolean assign(const HashKey<TObject>& arg) {
    length_d = arg.length_d;
    obj1_d = arg.obj1_d;
    obj2_d = arg.obj2_d;
    return true;
  }

  // method: operator=
  //
  HashKey<TObject>& operator=(const HashKey<TObject>& arg) {
    assign(arg);
    return *this;
  }

  // method: eq
  //
  boolean eq(const HashKey<TObject>& arg) const {
    if ((obj1_d == arg.obj1_d) && (obj2_d == arg.obj2_d)) {
      return true;
    }
    return false;
  }

  // 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 cmode = Integral::DEF_CMODE) {
    length_d = 0;
    obj1_d = (TObject*)NULL;
    obj2_d = (TObject*)NULL;
    return true;
  }
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  extensions to required methods
  //
  //---------------------------------------------------------------------------

  // method: assign
  //
  boolean assign(TObject* obj) {  
    if (obj == (TObject*)NULL) {
      return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__);
    }
    clear();
    length_d = 1;
    obj1_d = obj;
    return true;
  }

  // method: assign
  //
  boolean assign(TObject* obj1, TObject* obj2) {  
    if ((obj1 == (TObject*)NULL) || (obj2 == (TObject*)NULL)) {
      return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__);
    }
    clear();
    length_d = 2;    
    obj1_d = obj1;
    obj2_d = obj2;    
    return true;
  }  
  
  // method: hash
  //
  long hash(long capacity) const {
    long index;
    ulong* vals;
    switch (length_d) {
    case 1:
      vals = new ulong[1];
      vals[0] = (ulong)obj1_d;
      index = Integral::hash(vals, length_d, capacity);
      delete [] vals;
      break;
    case 2:	
      vals = new ulong[2];
      vals[0] = (ulong)obj1_d;
      vals[1] = (ulong)obj2_d;      
      index = Integral::hash(vals, length_d, capacity);
      delete [] vals;
      break;      
    default:
      return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__);
    }
    return index;
  }
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

}; 

//-----------------------------------------------------------------------------
//
// we define non-integral constants at the end of class definition for
// templates (for non-templates these are defined in the default constructor)
//      
//-----------------------------------------------------------------------------

// constants: required constants such as the class name
//
template <class TObject>
const String HashKey<TObject>::CLASS_NAME(L"HashKey");

// constants: required constants for i/o methods
//
template <class TObject>
const String HashKey<TObject>::DEF_PARAM(L"item");

// static instantiations: debug level and memory manager
//
template <class TObject>
Integral::DEBUG HashKey<TObject>::debug_level_d = Integral::NONE;

template <class TObject>
MemoryManager HashKey<TObject>::mgr_d(sizeof(HashKey<TObject>), CLASS_NAME);

// below are all the methods for the Node template class
//

//-----------------------------------------------------------------------------
//
// required static methods
//
//-----------------------------------------------------------------------------

// method: name
//
// arguments: none
//
// return: a static String& containing the class name
//
// this method returns the class name
//
template<class TObject>
const String& HashKey<TObject>::name() {

  // create the static name string for this class and return it
  //
  static String cname(CLASS_NAME);
  cname.clear();
  cname.concat(CLASS_NAME);

  // return the name
  //
  return cname;
}

// end of include file
//
#endif

