/* MegaZeux
 *
 * Copyright (c) 2008 Patrick Walton <pcwalton@uchicago.edu>
 *
 * This program 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 program 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 program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef HASHTABLE_H
#define HASHTABLE_H

#include "compat.h"

__M_BEGIN_DECLS

typedef unsigned int (*HashFunc)(const void *key);
typedef int (*EqualFunc)(const void *a, const void *b);
typedef void (*DestroyFunc)(void *ptr);

typedef struct HashEntry HashEntry;

struct HashEntry
{
  void *key;
  void *val;
  HashEntry *next;
};

typedef struct
{
  HashFunc hash_func;
  EqualFunc equal_func;

  int nbuckets;
  HashEntry **buckets;

  DestroyFunc key_destroy;
  DestroyFunc val_destroy;
} HashTable;

typedef struct
{
  HashTable *table;
  int bucket;
  HashEntry *entry;
} HashTableIter;

HashTable *create_hash_table(HashFunc hash_func, EqualFunc equal_func);
HashTable *create_managed_hash_table(HashFunc hash_func, EqualFunc equal_func,
     DestroyFunc key_destroy, DestroyFunc val_destroy);
unsigned int int_hash(const void *ptr);
int int_equal(const void *ap, const void *bp);
void destroy_hash_table(HashTable *tbl);
void insert_into_hash_table(HashTable *tbl, void *key, void *val);
HashEntry *lookup_in_hash_table(HashTable *tbl, void *key);
HashTableIter *create_hash_table_iter(HashTable *table);
HashEntry *hash_table_iter_next(HashTableIter *iter);
void destroy_hash_table_iter(HashTableIter *iter);

#endif
