summaryrefslogtreecommitdiff
path: root/libihash/ihash.h
blob: ae8f0f38b4051ebf95fb32986037d74a139c035c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Integer-keyed hash table functions.

   Copyright (C) 1995 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. */

#ifndef __IHASH_H__
#define __IHASH_H__

/* ---------------------------------------------------------------- */

typedef struct ihash *ihash_t;

struct ihash
{
  /* An array storing the elements in the hash table (each a void *).  */
  void **tab;

  /* An array storing the integer key for each element.  */
  int *ids;

  /* An array storing pointers to the `location pointers' for each element.
     These are used as cookies for quick 'n' easy removal.  */
  void ****locps;		/* four, count them, four stars */

  /* The length of all these arrays.  */
  int size;

  /* When freeing or overwriting an element, this function, if non-NULL, is
     called with the value as the first argument, and CLEANUP_ARG as the
     second argument.  */
  void (*cleanup)(void *element, void *arg);
  void *cleanup_arg;
};

/* Create an integer hash table and return it in HT.  If a memory allocation
   error occurs, ENOMEM is returned, otherwise 0.  */
error_t ihash_create(ihash_t *ht);

/* Free HT and all resources it consumes.  */
void ihash_free(ihash_t ht);

/* Sets HT's element cleanup function to CLEANUP, and its second argument to
   ARG.  CLEANUP will be called on the value of any element to be
   subsequently overwritten or deleted, with ARG as the second argument.  */
void ihash_set_cleanup(ihash_t ht,
		       void (*cleanup)(void *value, void *arg),
		       void *arg);

/* Add ITEM to the hash table HT under the key ID.  LOCP is the address of a
   pointer located in ITEM; If non-NULL, LOCP should point to a variable of
   type void **, and will be filled with a pointer that may be used as an
   argument to ihash_locp_remove() [the variable pointed to by LOCP may be
   written to subsequently between this call and when the element is
   deleted, so you can't stash its value elsewhere and hope to use the
   stashed value with ihash_locp_remove()].  If a memory allocation error
   occurs, ENOMEM is returned, otherwise 0.  */
error_t ihash_add(ihash_t ht, int id, void *item, void ***locp);

/* Find and return the item in hash table HT with key ID, or NULL if it
   doesn't exist.  */
void *ihash_find(ihash_t ht, int id);

/* Call function FUN of one arg for each element of HT.  FUN's only arg is a
   pointer to the value stored in the hash table.  If FUN ever returns
   non-zero, then iteration stops and ihash_iterate returns that value,
   otherwise it (eventually) returns 0.  */
error_t ihash_iterate(ihash_t ht, error_t (*fun)(void *));

/* Remove the entry with a key of ID from HT.  If anything was actually
   removed, 1 is returned, otherwise (if there was no such element), 0.  */
int ihash_remove(ihash_t ht, int id);

/* Remove the entry at LOCP from the hashtable HT.  LOCP is as returned from
   an earlier call to ihash_add().  This call should be faster than
   ihash_remove().  HT can be NULL, in which case the call still succeeds,
   but no cleanup can be done.  */
void ihash_locp_remove(ihash_t ht, void **ht_locp);

#endif /* __IHASH_H__ */