File: ghash_get.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,468 kB
  • sloc: ansic: 15,821; perl: 674; sh: 63; makefile: 29
file content (43 lines) | stat: -rw-r--r-- 953 bytes parent folder | download | duplicates (8)
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
#include "ghash.h"

/** Find an entry in a \c ghash table.
 *
 * \returns A pointer to the entry structure, or \c NULL if the \c key
 * was not found. */
void** ghash_find(struct ghash* d, const void* key)
{
  const adt_hash_t hash = d->hashfn(key);
  const unsigned size = d->size;
  unsigned start;
  unsigned i;
  void** p;
  
  if (size == 0)
    return 0;
  start = hash % size;
  i = start;
  p = d->table + start;

  do {
    void* entry = *p;
    if (entry == 0) return 0;
    if (ghash_entry_hash(entry) == hash &&
	d->keycmp(key, ghash_entry_keyptr(entry)) == 0)
      return p;
    ++p;
    if (++i >= size) i = 0, p = d->table;
  } while (i != start);
  return 0;
}

/** Find an entry in a \c ghash table.
 *
 * \returns A pointer to the data structure or \c NULL if the \c key was
 * not found. */
void* ghash_get(struct ghash* d, const void* key)
{
  void** entry;
  if ((entry = ghash_find(d, key)) == 0)
    return 0;
  return *entry;
}