/*
 * A simple hash
 *
 * Author(s) : Catalin Dumitrescu (catalind@acm.org)
 *
 * File: hash.cc
 * Created: Tue Aug 28 10:27:00 CDT 2001
 *
 */

#ifndef _HASH_
#define _HASH_

#include <vector>
using namespace std;

#include "scheme_exc.hh"

class hashException : public schemeException {
public:
  hashException (string s) : schemeException (s) { }
};

template <class D>
class hash_node {
public:  
  string key; D data;
  hash_node (string k, D d) { key = k; data = d; }
};

template <class D>
class shash {
  int size;
  vector< hash_node<D> >* content;

 public:
  shash ();
  shash (int);
  shash (const shash&);
  ~shash ();

 bool insert (string key, const D data);
 bool remove (string key);
 D& lookup (string key) const throw (schemeException);

 shash& operator = (shash& from);

};

template<class D>
shash<D>::shash ()
{
  size = 5;
  content = new vector< hash_node<D> >[size];
}

template<class D>
shash<D>::shash (int Size)
{
  size = Size;
  content = new vector< hash_node<D> >[size];
}

template<class D>
shash<D>::shash (const shash& sh)
{
  size = sh.size;
  content = new vector< hash_node<D> >[size];
  for (int i = 0; i < size; i++)
    content[i] = sh.content[i]; // ->clone (); // ??
}

template<class D>
shash<D>& shash<D>::operator = (shash& from)
{
  size = from.size;
  if (content) delete[] content;
  content = new vector< hash_node<D> >[size];
  for (int i = 0; i < size; i++)
    content[i] = from.content[i]; // ->clone (); // ??
}

template<class D>
shash<D>::~shash ()
{
  delete[] content;
}

template<class D>
bool shash<D>::insert (string key, const D data)
{
  hash_node<D> nnode (key, data);

  (content[0]).insert (content[0].begin(), nnode);
  return true;
}

template<class D>
bool shash<D>::remove (string key)
{
  
}

template<class D>
D& shash<D>::lookup (string key) const throw (schemeException)
{
  for (int i = 0; i < content[0].size(); i++)
    if (content[0][i].key == key)
      return content[0][i].data;
  throw hashException ("key not found");
}


#endif // _HASH_
