// -*- c++ -*-

/*
 * Jakelib2 - General purpose C++ library
 * Copyright (C) 2001 Florian Wolff  (florian@donuz.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: Hashtable.h,v 1.23 2003/09/07 08:11:33 florian Exp $
 */

#ifndef INCLUDE_JAKELIB2_UTIL_HASHTABLE_H
# define INCLUDE_JAKELIB2_UTIL_HASHTABLE_H
# include "Dictionary.h"

namespace jakelib {
  namespace util {
    class HashEntry;

    /**
     * Example:
     * \code
     * Hashtable* hash = new Hashtable();
     *
     * hash->put(`"one"`, new Integer(1));
     * hash->put(`"two"`, new Integer(2));
     * hash->put(`"three"`, new Integer(3));
     * hash->put(`"four"`, new Integer(4));
     *
     * Integer* i = (Integer*) hash->get(`"three"`);
     * System::out->println(`"three = "` .. i->intValue());
     * \endcode
     *
     * @todo Synchronization
     *
     * @class Hashtable
     * @version $Id: Hashtable.h,v 1.23 2003/09/07 08:11:33 florian Exp $
     * @author Florian 'Overflo' Wolff (florian@donuz.de)
     */
    class Hashtable : public Dictionary
      {
	friend class HashtableIterator;

      protected:
	/**
	 * @brief The hash table data.
	 */
	HashEntry** table;
  
	/**
	 * @brief The total number of entries in the hash table.
	 */
	int elementCount;
  
	/**
	 * @brief The table is rehashed when its size exceeds this threshold.  (The
	 * value of this field is (int)(capacity * loadFactor).)
	 */
	int threshold;
  
	/**
	 * @brief The table's size.
	 */  
	int capacity;
  
	/**
	 * @brief The load factor for the hashtable.
	 */
	float loadFactor;

      public:
	/**
	 * @brief Constructs a new, empty hashtable with the specified initial 
	 * capacity and the specified load factor.
	 *
	 * @param initialCapacity The initial capacity of the hashtable.
	 * @param loadFactor The load factor of the hashtable.
	 * @exception IllegalArgumentException If the initial capacity is less than zero,
         *            or if the load factor is nonpositive.
	 */
	Hashtable(int initialCapacity = 101, float loadFactor = 0.75F);
  
	/**
	 * @brief Destructor
	 */
	~Hashtable();
  
	/**
	 * @brief Returns the number of keys in this hashtable.
	 *
	 * @return The number of keys in this hashtable.
	 */
	int size();
  
	/**
	 * @brief Tests if this hashtable maps no keys to values.
	 *
	 * @return  <tt>TRUE</tt> if this hashtable maps no keys to values;
	 *          <tt>FALSE</tt> otherwise.
	 */
	jboolean isEmpty();
  
	/**
	 * @brief Returns the value to which the specified key is mapped in this hashtable.
	 *
	 * @param key A key in the hashtable.
	 * @return The value to which the key is mapped in this hashtable; 
         *         \c null if the key is not mapped to any value in this hashtable.
	 */
	jakelib::lang::Object* get(jakelib::lang::Object* key);
  
	/**
	 * @brief Returns the value to which the specified key is mapped in this hashtable.
	 *
	 * @param key A key in the hashtable. (Will be converted to \c String internally.)
	 * @return The value to which the key is mapped in this hashtable;
         *         \c null if the key is not mapped to any value in this hashtable.
	 */
	jakelib::lang::Object* get(char* key);
  
	/**
	 * @brief Removes a specified key from the hash.
	 *
	 * @param key The key to be deleted
	 */
	void remove(jakelib::lang::Object* key);
  
	/**
	 * @brief Removes a specified key from the hash.
	 *
	 * @param key The key to be deleted. (Will be converted to \c String internally.)
	 */
	void remove(char* key);
  
	/**
	 * @brief Increases the capacity of and internally reorganizes this 
	 * hashtable, in order to accommodate and access its entries more 
	 * efficiently.
         *
         * This method is called automatically when the 
	 * number of keys in the hashtable exceeds this hashtable's capacity 
	 * and load factor. 
	 */
	void rehash();
  
	/**
	 * @brief Maps the specified <TT>key</TT> to the specified 
	 * <TT>value</TT> in this hashtable. Neither the key nor the 
	 * value can be \c null.
	 *
	 * The value can be retrieved by calling the <TT>get</TT> method 
	 * with a key that is equal to the original key. 
	 *
	 * @param key The hashtable key.
	 * @param value The value.
	 * @exception NullPointerException If the key or value is \c null.
	 */
	void put(jakelib::lang::Object* key, jakelib::lang::Object* value);
  
	/**
	 * @brief Maps the specified <TT>key</TT> to the specified 
	 * <TT>value</TT> in this hashtable. Neither the key nor the 
	 * value can be \c null.<BR>
	 *
	 * New <TT>String</TT> objects are created for <TT>key</TT> and <TT>value</TT>.
	 *
	 * @param key The hashtable key.
	 * @param value The value.
	 * @exception NullPointerException If the key or value is \c null.
	 */
	void put(char* key, char* value);
  
	/**
	 * @brief Maps the specified <TT>key</TT> to the specified 
	 * <TT>value</TT> in this hashtable. Neither the key nor the 
	 * value can be \c null.<BR>
	 *
	 * A new <TT>String</TT> object is created for <TT>key</TT>.
	 *
	 * @param key The hashtable key.
	 * @param value The value.
	 * @exception NullPointerException If the key or value is \c null.
	 */
	void put(char* key, jakelib::lang::Object* value);
  
	/**
	 * @brief Deletes all items from hash.
	 */
	void clear();
  
	/**
	 * @brief Returns an iterator over all keys in the hash.
	 *
	 * @return keys The iterator.
	 */
	HashtableIterator* keys();

        /**
         * @brief Returns a string representation of this \c Hashtable.
         *
         * 
         */
        jakelib::lang::String* toString();
  
	JAKELIB_DECLARE_CLASS(Hashtable);
      };

  }
}
#endif

