/* This file is part of an open-address hash table implementation.
   Copyright (C) 2005 Martin Dickopp

   This file is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This file 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this file; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
   USA.  */

#ifndef HDR_HASH
#define HDR_HASH 1


/* Hash table.  The members should not be accessed directly.  */
struct htable {
    struct htable_slot *slots;
    size_t size_index;
    size_t occupancy;
};


/* Create a hash table.  On success, zero is returned.  If memory allocation fails, -1 is returned.  */
extern int htable_create (struct htable *table);

/* Destroy a hash table.  Free all associated memory.  */
extern void htable_destroy (struct htable *table);

/* Insert a new key and associated data into a hash table.  The key must be a NUL-terminated string.  The data is
   copied; the location of the copied data is returned, and if `exists_ptr' is not a null pointer, the integer at
   the location pointed to is set to zero.  If the key already exists, the location of the data associated with the
   existing key data is returned, and if `exists_ptr' is not a null pointer, the integer at the location pointed to
   is set to one.  If `data_len' is zero, `data' is not evaluated and the returned pointer must not be dereferenced.
   If memory allocation fails, a null pointer is returned.  */
extern void *htable_insert (struct htable *table, const char *key, const void *data, size_t data_len,
                            int *exists_ptr);

/* Delete a key and associated data.  The `data' parameter must point to the data associated with an existing key.  */
extern void htable_delete (struct htable *table, void *data);

/* Find the data associated with a key.  If the key does not exist, a null pointer is returned.  */
extern void *htable_find (const struct htable *table, const char *key);


#endif
