/***************************************************************
 * PrHashTableImpl.h
 *
 * This class provides a hash table interface.
 *
 * A generic hash table collection class with chaining.
 * Should be used with a template.                
 *
 ****************************************************************/

/*
 * modification history
 *----------------------
 *
 * 02/19/98: K.C. Chang: created.
 */

#ifndef _PrHashTableImpl_h
#define _PrHashTableImpl_h

#include "PrGlobalDefn.h"
#include "PrLinkedList.h"

#define PR_HASHTABLEIMPL_SIZE_DEFAULT        511

#define PR_HASHTABLEIMPL_NUM_ITEMS_DEFAULT (PR_HASHTABLEIMPL_SIZE_DEFAULT * PR_LINKEDLIST_SIZE_DEFAULT)

#define PR_HASHTABLEIMPL_NUM_ITEMS_UNLIMITED 0

class PrBitmap;
class PrHashTableEntry;

class PrHashTableImpl 
{
  public:
    PrHashTableImpl(int maxNumItems,int tableSize);

    virtual ~PrHashTableImpl(); // since there are virtual functions

    virtual int equal(void *key1,void *key2) const 
      { return ((*(int *)key1) == (*(int *)key2)); }
    virtual int hash(void *key) const 
      { return (*(int *)key)%tableSize(); }

    void newItem(void *,void * key);
    void *deleteItem(void * key);
    void *item(void * key) const;
    int items() const { return items_; };

    void *firstItem () const;
    void *nextItem () const;

    int tableSize() const { return tableSize_; }

private:
    int maxNumItems_;
    PrBitmap *bitmap_;
    PrHashTableEntry *heap_;

    PrLinkedList<PrHashTableEntry> **buckets_;
    int curListNum_;
    int items_;
    const int tableSize_;

    inline PrHashTableEntry *FindEntry(void * key) const;

    PrHashTableImpl (const PrHashTableImpl &) {}
    PrHashTableImpl &operator= (const PrHashTableImpl &) { return *this; }
};

class PrHashTableEntry 
{
public:
  PrHashTableEntry(void *d=NULL,void * k=NULL) : data_(d), key_(k), id_(0) {}
  void *key() const { return key_;}
  void key(void *k) { key_ = k;}
  void *data() const {return data_;}
  void data(void *d) {data_ = d;}
  void id(int n) { id_ = n; }
  int  id() const { return id_; }
private:
  void *data_;
  void *key_;
  int id_;

  PrHashTableEntry (const PrHashTableEntry &) {}
  PrHashTableEntry &operator= (const PrHashTableEntry &) { return *this; }
};


#endif // _PrHashTableImpl_h


