hash.h 2.48 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2009 Michael R. Elkins <me@mutt.org>
Thomas Roessler's avatar
Thomas Roessler committed
3 4 5 6 7
 *
 *     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.
8
 *
Thomas Roessler's avatar
Thomas Roessler committed
9 10 11 12
 *     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.
13
 *
Thomas Roessler's avatar
Thomas Roessler committed
14 15
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
16
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Thomas Roessler's avatar
Thomas Roessler committed
17 18
 */

19 20 21
#ifndef _HASH_H
#define _HASH_H

22 23 24 25 26 27
union hash_key
{
  const char *strkey;
  unsigned int intkey;
};

Thomas Roessler's avatar
Thomas Roessler committed
28 29
struct hash_elem
{
30
  union hash_key key;
Thomas Roessler's avatar
Thomas Roessler committed
31 32 33 34 35 36 37
  void *data;
  struct hash_elem *next;
};

typedef struct
{
  int nelem;
38 39
  unsigned int strdup_keys : 1;      /* if set, the key->strkey is strdup'ed */
  unsigned int allow_dups : 1;       /* if set, duplicate keys are allowed */
Thomas Roessler's avatar
Thomas Roessler committed
40
  struct hash_elem **table;
41 42
  unsigned int (*gen_hash)(union hash_key, unsigned int);
  int (*cmp_key)(union hash_key, union hash_key);
Thomas Roessler's avatar
Thomas Roessler committed
43 44 45
}
HASH;

46 47 48
/* flags for hash_create() */
#define MUTT_HASH_STRCASECMP   (1<<0)   /* use strcasecmp() to compare keys */
#define MUTT_HASH_STRDUP_KEYS  (1<<1)   /* make a copy of the keys */
49
#define MUTT_HASH_ALLOW_DUPS   (1<<2)   /* allow duplicate keys to be inserted */
50

51 52
HASH *hash_create (int nelem, int flags);
HASH *int_hash_create (int nelem, int flags);
53

54 55
int hash_insert (HASH * table, const char *key, void *data);
int int_hash_insert (HASH *table, unsigned int key, void *data);
56 57

void *hash_find (const HASH *table, const char *key);
58
struct hash_elem *hash_find_elem (const HASH *table, const char *strkey);
59 60 61 62 63 64 65
void *int_hash_find (const HASH *table, unsigned int key);

struct hash_elem *hash_find_bucket (const HASH *table, const char *key);

void hash_delete (HASH * table, const char *key, const void *data,
                  void (*destroy) (void *));
void int_hash_delete (HASH * table, unsigned int key, const void *data,
66
                      void (*destroy) (void *));
67

Thomas Roessler's avatar
Thomas Roessler committed
68
void hash_destroy (HASH ** hash, void (*destroy) (void *));
69

70 71 72 73 74 75 76
struct hash_walk_state {
  int index;
  struct hash_elem *last;
};

struct hash_elem *hash_walk(const HASH *table, struct hash_walk_state *state);

77
#endif